1 ## Copyright (C) 1993-2012 John W. Eaton
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} hilb (@var{n})
21 ## Return the Hilbert matrix of order @var{n}. The @math{i,j} element
22 ## of a Hilbert matrix is defined as
25 ## H(i, j) = {1 \over (i + j - 1)}
31 ## H(i, j) = 1 / (i + j - 1)
36 ## Hilbert matrices are close to being singular which make them difficult to
37 ## invert with numerical routines.
38 ## Comparing the condition number of a random matrix 5x5 matrix with that of
39 ## a Hilbert matrix of order 5 reveals just how difficult the problem is.
46 ## @result{} 4.7661e+05
55 function retval = hilb (n)
59 elseif (! isscalar (n))
60 error ("hilb: N must be a scalar integer");
66 retval(i, :) = 1.0 ./ tmp;
73 %!assert (hilb (2), [1, 1/2; 1/2, 1/3])
74 %!assert (hilb (3), [1, 1/2, 1/3; 1/2, 1/3, 1/4; 1/3, 1/4, 1/5])
78 %!error <N must be a scalar integer> hilb (ones(2))