comparison toolbox/tridiag.m @ 2:c124219d7bfa draft

Re-add the 1995 toolbox after noticing the statement in the ~higham/mctoolbox/ webpage.
author Antonio Pino Robles <data.script93@gmail.com>
date Thu, 07 May 2015 18:36:24 +0200
parents 8f23314345f4
children
comparison
equal deleted inserted replaced
1:e471a92d17be 2:c124219d7bfa
1 function T = tridiag(n, x, y, z)
2 %TRIDIAG Tridiagonal matrix (sparse).
3 % TRIDIAG(X, Y, Z) is the tridiagonal matrix with subdiagonal X,
4 % diagonal Y, and superdiagonal Z.
5 % X and Z must be vectors of dimension one less than Y.
6 % Alternatively TRIDIAG(N, C, D, E), where C, D, and E are all
7 % scalars, yields the Toeplitz tridiagonal matrix of order N
8 % with subdiagonal elements C, diagonal elements D, and superdiagonal
9 % elements E. This matrix has eigenvalues (Todd 1977)
10 % D + 2*SQRT(C*E)*COS(k*PI/(N+1)), k=1:N.
11 % TRIDIAG(N) is the same as TRIDIAG(N,-1,2,-1), which is
12 % a symmetric positive definite M-matrix (the negative of the
13 % second difference matrix).
14
15 % References:
16 % J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra,
17 % Birkhauser, Basel, and Academic Press, New York, 1977, p. 155.
18 % D.E. Rutherford, Some continuant determinants arising in physics and
19 % chemistry---II, Proc. Royal Soc. Edin., 63, A (1952), pp. 232-241.
20
21 if nargin == 1, x = -1; y = 2; z = -1; end
22 if nargin == 3, z = y; y = x; x = n; end
23
24 x = x(:); y = y(:); z = z(:); % Force column vectors.
25
26 if max( [ size(x) size(y) size(z) ] ) == 1
27 x = x*ones(n-1,1);
28 z = z*ones(n-1,1);
29 y = y*ones(n,1);
30 else
31 [nx, m] = size(x);
32 [ny, m] = size(y);
33 [nz, m] = size(z);
34 if (ny - nx - 1) | (ny - nz -1)
35 error('Dimensions of vector arguments are incorrect.')
36 end
37 end
38
39 % T = diag(x, -1) + diag(y) + diag(z, 1); % For non-sparse matrix.
40 n = max(size(y));
41 T = spdiags([ [x;0] y [0;z] ], -1:1, n, n);