1 ## Copyright (C) 1999-2012 Peter Ekberg
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} {} wilkinson (@var{n})
21 ## Return the Wilkinson matrix of order @var{n}. Wilkinson matrices are
22 ## symmetric and tridiagonal with pairs of nearly, but not exactly, equal
23 ## eigenvalues. They are useful in testing the behavior and performance
24 ## of eigenvalue solvers.
26 ## @seealso{rosser, eig}
29 ## Author: Peter Ekberg
32 function retval = wilkinson (n)
38 if (! (isscalar (n) && n >= 0 && (n == fix (n))))
39 error ("wilkinson: N must be a non-negative integer");
43 center = abs (-(n-1)/2:(n-1)/2);
44 retval = diag (side, -1) + diag (center) + diag (side, 1);
49 %!assert (wilkinson (0), [])
50 %!assert (wilkinson (1), 0)
51 %!assert (wilkinson (2), [0.5,1;1,0.5])
52 %!assert (wilkinson (3), [1,1,0;1,0,1;0,1,1])
53 %!assert (wilkinson (4), [1.5,1,0,0;1,0.5,1,0;0,1,0.5,1;0,0,1,1.5])
55 %% Test input validation
57 %!error wilkinson (1,2)
58 %!error <N must be a non-negative integer> wilkinson (ones (2))
59 %!error <N must be a non-negative integer> wilkinson (-1)
60 %!error <N must be a non-negative integer> wilkinson (1.5)