10分钟理解托马斯算法(tridiagonal matrix algorithm,Thomas algorithm)

这里写图片描述
遇到隐式格式,我们需要求解一个线性方程组。怎么办呢?当然是Thomas algorithm


这里写图片描述
注意到第一处箭头。
接着是矩阵化
这里写图片描述
对角占有就可以LU分解
这里写图片描述


重头戏:Thomas algorithm
这里写图片描述

这里写图片描述

A=b1a2c1b2 c2aN1 bN1aNcN1bN,L=1e21 eN11eN1 A = [ b 1 − c 1 − a 2 b 2 − c 2 ⋱   ⋱ ⋱   − a N − 1 b N − 1 c N − 1 − a N b N ] , L = [ 1 e 2 1 ⋱   ⋱ e N − 1 1 e N 1 ]

U=fc1f2c2fN1cN1fN U = [ f − c 1 f 2 − c 2 ⋱ ⋱ f N − 1 − c N − 1 f N ]

1.首先是计算 f1 f 1
L的第一行乘以 U的第一列,得到 A的第一行第一列的元素,即
1f1=b1 1 ∗ f 1 = b 1

2.接着计算出每个 ei,fi,i=2,3...N e i , f i , i = 2 , 3... N
计算 e2 e 2 :
L的第二行乘以 U的第一列,得到 A第二行第一列的元素,即
e2f1=a2 e 2 ∗ f 1 = − a 2

得到 e2=a2/f1 e 2 = − a 2 / f 1
计算 e3 e 3 :
L的第三行乘以 U的第二列(乘以第一列为零),得到 A第三行第二列的元素,即
0c1+e3f2=a3 0 ∗ − c 1 + e 3 ∗ f 2 = − a 3

得到 e3=a3/f2 e 3 = − a 3 / f 2
依次类推~~~
3.计算 fi,i=2,3..N f i , i = 2 , 3.. N
L的第二行乘以 U的第二列,得到 A的第二行第二列的元素,即
e2c1+f2=b2 e 2 ∗ − c 1 + f 2 = b 2

得到 f2=b2+e2c1 f 2 = b 2 + e 2 ∗ c 1
计算 f3 f 3 :
L的第三行乘以 U的第三列,得到 A的第三行第三列的元素,即
e3c2+f3=b3 e 3 ∗ − c 2 + f 3 = b 3

得到 f3=b3+e3c2 f 3 = b 3 + e 3 ∗ c 2

依次类推


计算 yi,i=1,2,3,...,N y i , i = 1 , 2 , 3 , . . . , N

U=fc1f2c2fN1cN1fN,X=x1x2xN U = [ f − c 1 f 2 − c 2 ⋱ ⋱ f N − 1 − c N − 1 f N ] , X = [ x 1 x 2 ⋮ ⋮ x N ]

Y=fc1f2c2fN1cN1fNx1x2xN=f1x1c1x2f2x2c2x3fNxN=y1y2yN Y = [ f − c 1 f 2 − c 2 ⋱ ⋱ f N − 1 − c N − 1 f N ] ∗ [ x 1 x 2 ⋮ ⋮ x N ] = [ f 1 ∗ x 1 − c 1 x 2 f 2 ∗ x 2 − c 2 x 3 ⋮ ⋮ f N ∗ x N ] = [ y 1 y 2 ⋮ ⋮ y N ]

d=d1d2dN d = [ d 1 d 2 ⋮ ⋮ d N ]

这里写图片描述
L
L=1e21 eN11eN1 L = [ 1 e 2 1 ⋱   ⋱ e N − 1 1 e N 1 ]

LY=d L ∗ Y = d
L的第一行乘以Y,即 1y1=d1 1 ∗ y 1 = d 1 , y1=d1 y 1 = d 1
L的第二行乘以Y,即 e2y1+1y2=d2 e 2 ∗ y 1 + 1 ∗ y 2 = d 2 , y2=d2e2y1 y 2 = d 2 − e 2 ∗ y 1
L的第三行乘以Y,即 e3y2+1y3=d3 e 3 ∗ y 2 + 1 ∗ y 3 = d 3 , y3=d2e2y2 y 3 = d 2 − e 2 ∗ y 2
依次类推,
yi=didiyi1,i=2,3,...,N y i = d i − d i ∗ y i − 1 , i = 2 , 3 , . . . , N


这里写图片描述
计算 xi,i=N,N1,...,1 x i , i = N , N − 1 , . . . , 1
由上面计算出 yi y i

Y=fc1f2c2fN1cN1fNx1x2xN=f1x1c1x2f2x2c2x3fNxN=y1y2yN Y = [ f − c 1 f 2 − c 2 ⋱ ⋱ f N − 1 − c N − 1 f N ] ∗ [ x 1 x 2 ⋮ ⋮ x N ] = [ f 1 ∗ x 1 − c 1 x 2 f 2 ∗ x 2 − c 2 x 3 ⋮ ⋮ f N ∗ x N ] = [ y 1 y 2 ⋮ ⋮ y N ]

从底往上算,
xNfN=yN x N ∗ f N = y N
xN1fN1cN1xN x N − 1 ∗ f N − 1 − c N − 1 ∗ x N
…..
x2f2c2x3 x 2 ∗ f 2 − c 2 ∗ x 3
x1f1c1x2 x 1 ∗ f 1 − c 1 ∗ x 2

计算 xi x i
则有:
xi=(yi+cixi+1)/fi,i=N1,N2,...,1 x i = ( y i + c i ∗ x i + 1 ) / f i , i = N − 1 , N − 2 , . . . , 1
这里写图片描述
这里写图片描述

matlab代码:

function x = thomas(N, a, b, c, d)
e = zeros(N,1);
f = zeros(N,1);
x = zeros(N,1);
y = zeros(N,1);
% compute f_i and e_i
f(1) = b(1);
for i = 2:1:N
e(i) = - a(i)/f(i-1);
f(i) = b(i) + e(i)*c(i-1);
end
% compute y_i
y(1) = d(1);
for i = 2:1:N
y(i) = d(i) - e(i)*y(i-1);
end
% compute x_i
x(N) = y(N)/f(N);
for i = N-1:-1:1
x(i) = (y(i) + c(i)*x(i+1))/f(i);
end
  • 19
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是带 Wilkinson 位移的隐式对称 QR 算法的 MATLAB 代码: ```matlab function [Q, H] = symmqr(A, tol) % SYMMQR Implicit symmetric QR algorithm with Wilkinson shift for eigenvalue computation. % [Q, H] = SYMMQR(A, TOL) computes the eigenvalues of the input matrix A % using the implicit symmetric QR algorithm with Wilkinson shift, % where TOL is the tolerance for convergence. % Q is the orthogonal matrix satisfying A = Q*H*Q', where H is the % tridiagonal matrix with the eigenvalues on the diagonal. n = size(A, 1); H = hessenberg(A); Q = eye(n); while norm(H(n, 1:n-1)) > tol % Compute Wilkinson shift mu = H(n, n); a = H(n-1, n-1); b = H(n-1, n); c = H(n, n-1); delta = (a - mu)*(a - mu) + b*b; if abs(delta) < eps t = 1; else t = (a - mu + sign(a - mu)*sqrt(delta)) / b; end % QR step with Wilkinson shift for k = 1:n-1 if k == n-1 || abs(H(k+1, k)) <= tol % Single QR iteration x = [H(k, k) - t; H(k+1, k)]; if abs(x(1)) < abs(x(2)) c = x(1); s = x(2); else tau = x(2) / x(1); c = 1 / sqrt(1 + tau*tau); s = tau*c; end % Apply Givens rotation to H and Q G = [c, s; -s, c]; H(k:k+1, k:n) = G'*H(k:k+1, k:n); H(1:k+1, k:k+1) = H(1:k+1, k:k+1)*G; Q(:, k:k+1) = Q(:, k:k+1)*G; else % Double QR iteration x = [H(k, k) - t; H(k+1, k); H(k+2, k)]; if abs(x(2)) < abs(x(3)) tau = x(2) / x(3); s = sqrt(1 / (1 + tau*tau)); c = tau*s; else tau = x(3) / x(2); c = sqrt(1 / (1 + tau*tau)); s = tau*c; end % Apply Givens rotation to eliminate subdiagonal element G = [c, s, 0; -s, c, 0; 0, 0, 1]; H(k:k+2, k:n) = G'*H(k:k+2, k:n); H(1:k+2, k:k+2) = H(1:k+2, k:k+2)*G; Q(:, k:k+2) = Q(:, k:k+2)*G; end end end end function H = hessenberg(A) % Hessenberg reduction of matrix A using Householder transformations. n = size(A, 1); for k = 1:n-2 % Compute the Householder reflection vector x = A(k+1:n, k); e1 = zeros(length(x), 1); e1(1) = 1; v = sign(x(1))*norm(x)*e1 + x; v = v / norm(v); % Apply the Householder reflection to A A(k+1:n, k:n) = A(k+1:n, k:n) - 2*v*(v'*A(k+1:n, k:n)); A(1:n, k+1:n) = A(1:n, k+1:n) - 2*(A(1:n, k+1:n)*v)*v'; end H = triu(A, -1); end ``` 其中,`A` 是输入的矩阵,`tol` 是算法收敛的阈值,`Q` 是正交矩阵,满足 $A=QHQ^T$,`H` 是带有特征值的对称三对角矩阵。该算法使用 Hessenberg 约化和隐式对称 QR 算法,以及 Wilkinson 位移来计算特征值。具体实现细节可以参考代码中的注释。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值