comparison scripts/special-matrix/wilkinson.m @ 11098:dcde7c5a1d29

new tests for special-matrix functions
author John W. Eaton <jwe@octave.org>
date Thu, 14 Oct 2010 14:43:59 -0400
parents 3140cb7a05a1
children fd0a3ac60b0e
comparison
equal deleted inserted replaced
11097:ffb2f1ef2097 11098:dcde7c5a1d29
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} wilkinson (@var{n}) 20 ## @deftypefn {Function File} {} wilkinson (@var{n})
21 ##
22 ## Return the Wilkinson matrix of order @var{n}. Wilkinson matrices are 21 ## Return the Wilkinson matrix of order @var{n}. Wilkinson matrices are
23 ## symmetric and tridiagonal with pairs of nearly, but not exactly, equal 22 ## symmetric and tridiagonal with pairs of nearly, but not exactly, equal
24 ## eigenvalues. 23 ## eigenvalues.
25 ## 24 ##
26 ## @seealso{hankel, vander, sylvester_matrix, hilb, invhilb, toeplitz 25 ## @seealso{hankel, vander, sylvester_matrix, hilb, invhilb, toeplitz
35 if (nargin != 1) 34 if (nargin != 1)
36 print_usage (); 35 print_usage ();
37 endif 36 endif
38 37
39 if (! (isscalar (n) && (n == fix (n)) && n > 0)) 38 if (! (isscalar (n) && (n == fix (n)) && n > 0))
40 error ("wilkinson: N must be an integer greater than 1"); 39 error ("wilkinson: N must be an integer greater than 0");
41 endif 40 endif
42 41
43 side = ones (n-1, 1); 42 side = ones (n-1, 1);
44 center = abs (-(n-1)/2:(n-1)/2); 43 center = abs (-(n-1)/2:(n-1)/2);
45 retval = diag (side, -1) + diag (center) + diag (side, 1); 44 retval = diag (side, -1) + diag (center) + diag (side, 1);
46 45
47 endfunction 46 endfunction
47
48 %!assert (wilkinson(1), [])
49 %!assert (wilkinson(2), [0.5,1;1,0.5])
50 %!assert (wilkinson(3), [1,1,0;1,0,1;0,1,1])
51 %!assert (wilkinson(4), [1.5,1,0,0;1,0.5,1,0;0,1,0.5,1;0,0,1,1.5])
52 %!error (wilkinson())
53 %!error (wilkinson(1,2))