【matlab】【数值分析】针对特殊矩阵的追赶法的matlab实现

【matlab】【数值分析】针对特殊矩阵的追赶法的matlab实现

原文链接:
点我!这就是本人的博客喵,快来看喵!

下面的追赶法算法原理不予介绍,在参考文献中有原文可参照。

考察线性方程组:
A x = y Ax=y Ax=y

其中系数矩阵A为如下形式:

  1. 常对角矩阵(Toeplitz矩阵)
    A = [ a 0 a − 1 a − 2 ... ... a − n + 1 a 1 a 0 a − 1 ⋱ ⋮ a 2 a 1 ⋱ ⋱ ⋱ ⋮ ⋮ ⋱ ⋱ ⋱ a − 1 a − 2 ⋮ ⋱ a 1 a 0 a − 1 a n − 1 ... ... a 2 a 1 a 0 ] A = \begin{bmatrix} a_{0} & a_{-1} & a_{-2} & \ldots & \ldots & a_{-n+1} \\ a_{1} & a_{0} & a_{-1} & \ddots & & \vdots \\ a_{2} & a_{1} & \ddots & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & a_{-1} & a_{-2} \\ \vdots & & \ddots & a_{1} & a_{0} & a_{-1} \\ a_{n-1} & \ldots & \ldots & a_{2} & a_{1} & a_{0} \end{bmatrix} A= a0a1a2⋮⋮an−1a−1a0a1⋱...a−2a−1⋱⋱⋱......⋱⋱⋱a1a2...⋱a−1a0a1a−n+1⋮⋮a−2a−1a0
  2. 循环矩阵
    A = [ a 0 a n − 1 a n − 2 ... ... a 1 a 1 a 0 a n − 1 a n − 2 ⋮ a 2 a 1 a 0 a n − 1 ⋱ ⋮ ⋮ ⋱ ⋱ ⋱ a 1 a n − 2 ⋮ ⋱ a 1 a 0 a n − 1 a n − 1 a n − 2 a n − 3 ... ... a 0 ] A = \begin{bmatrix} a_{0} & a_{n-1} & a_{n-2} & \ldots & \ldots & a_{1} \\ a_{1} & a_{0} & a_{n-1} & a_{n-2} & & \vdots \\ a_{2} & a_{1} & a_{0} & a_{n-1} & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & a_{1} & a_{n-2} \\ \vdots & & \ddots & a_{1} & a_{0} & a_{n-1} \\ a_{n-1} & a_{n-2} & a_{n-3} & \ldots & \ldots & a_{0} \end{bmatrix} A= a0a1a2⋮⋮an−1an−1a0a1⋱an−2an−2an−1a0⋱⋱an−3...an−2an−1⋱a1......⋱a1a0...a1⋮⋮an−2an−1a0
    为特殊的Toeplitz矩阵。
  3. 三对角矩阵
    A = [ b 1 c 1 a 2 b 2 c 2 a 3 b 3 ⋱ ⋱ ⋱ c n − 1 a n b n ] A = \begin{bmatrix} b_{1} & c_{1} & & & \\ a_{2} & b_{2} & c_{2} & & \\ & a_{3} & b_{3} & \ddots & \\ & & \ddots & \ddots & c_{n-1}\\ & & & a_{n} & b_{n} \\ \end{bmatrix} A= b1a2c1b2a3c2b3⋱⋱⋱ancn−1bn
  4. 循环Toeplitz三对角线性方程组
    A = [ b 1 c 1 a 1 a 2 b 2 c 2 a 3 b 3 ⋱ ⋱ ⋱ c n − 1 c n a n b n ] A = \begin{bmatrix} b_{1} & c_{1} & & & a_{1}\\ a_{2} & b_{2} & c_{2} & & \\ & a_{3} & b_{3} & \ddots & \\ & & \ddots & \ddots & c_{n-1}\\ c_{n} & & & a_{n} & b_{n} \\ \end{bmatrix} A= b1a2cnc1b2a3c2b3⋱⋱⋱ana1cn−1bn
  5. 五对角矩阵
    A = [ c 1 d 1 e 1 b 2 c 2 d 2 e 2 a 3 b 3 c 3 d 3 e 3 a 4 b 4 c 4 d 4 e 4 ⋱ ⋱ ⋱ ⋱ ⋱ a n − 2 b n − 2 c n − 2 d n − 2 e n − 2 a n − 1 b n − 1 c n − 1 d n − 1 a n b n c n ] A = \begin{bmatrix} c_{1} & d_{1} & e_{1} & & & & \\ b_{2} & c_{2} & d_{2} & e_{2} & & & \\ a_{3} & b_{3} & c_{3} & d_{3} & e_{3} & & \\ & a_{4} & b_{4} & c_{4} & d_{4} & e_{4} & \\ & & \ddots & \ddots & \ddots & \ddots & \ddots \\ & & & a_{n-2} & b_{n-2} & c_{n-2} & d_{n-2} & e_{n-2} \\ & & & & a_{n-1} & b_{n-1} & c_{n-1} & d_{n-1} \\ & & & & & a_{n} & b_{n} & c_{n} \end{bmatrix} A= c1b2a3d1c2b3a4e1d2c3b4⋱e2d3c4⋱an−2e3d4⋱bn−2an−1e4⋱cn−2bn−1an⋱dn−2cn−1bnen−2dn−1cn

本文将使用matlab(R2022a)(win11 64位操作系统)逐一实现上列线性方程组的追赶法。详细原理及算法分析来源于最后的参考资料,文中并不提及。

三对角

算法步骤

一、替换(*角标表示替换后的元素,下文同理)
c i ∗ = { c i b i if i = 1 , c i b i − a i c i ∗ if i = 2 , 3 , ... , n − 1. c^{*}{i} = \begin{cases} \frac{c{i}}{b_{i}} & \text{if }i = 1, \\ \frac{c_{i}}{b_{i}-a_{i}c^{*}{i}} & \text{if }i = 2,3,\ldots,n-1. \end{cases} ci∗={bicibi−aici∗ciif i=1,if i=2,3,...,n−1.
d i ∗ = { d i b i if i = 1 , d i − a i d i − 1 ∗ b i − a i c i − 1 ∗ if i = 2 , 3 , ... , n . d^{*}
{i} =\begin{cases} \frac{d_{i}}{b_{i}} & \text{if }i = 1, \\ \frac{d_{i}-a_{i}d^{*}{i-1}}{b{i}-a_{i}c^{*}_{i-1}} & \text{if }i = 2,3,\ldots,n. \end{cases} di∗={bidibi−aici−1∗di−aidi−1∗if i=1,if i=2,3,...,n.

其中 d i ∈ y d_{i} \in y di∈y.

二、计算解向量(追与赶)
x n = d n ∗ , x i = d i ∗ − c i x i + 1 for i = n − 1 , n − 2 , ... , 1. x_{n} = d^{*}{n}, x{i} = d^{*}{i}-c{i}x_{i+1} \text{ for } i = n-1,n-2,\ldots,1. xn=dn∗,xi=di∗−cixi+1 for i=n−1,n−2,...,1.

代码实现

matlab 复制代码
function x = Tridiagonal(a, b, c, d)
% a、b、c 分别为三对角线的下对角线、主对角线和上对角线
% d 为常数向量
n = length (d); % 矩阵维度
% 追赶法的第一步:消元
c(1) = c(1)/b(1);
d(1) = d(1)/b(1);
for i = 2 : n-1
    c(i) = c(i) / (b(i)-a(i)*c(i-1));
    d(i) = (d(i)-a(i-1)*d(i-1)) / (b(i)-a(i-1)*c(i-1));
end
d(n) = (d(n)-a(n-1)*d(n-1)) / (b(n)-a(n-1)*c(n-1));
% 追赶法的第二步:回代
x(n) = d (n);
for i = n-1:-1:1
    x(i) = d(i)-c(i)*x(i+1);
end
end

循环Toeplitz三对角

算法步骤

一、 A = L U A = LU A=LU, 其中

L = [ d 1 a 2 d 2 ⋱ ⋱ a n − 2 d n − 2 a n − 1 d n − 1 α 1 α 2 ⋯ α n − 2 α n − 1 d n ] , U = [ 1 u 1 β 1 1 u 2 β 2 ⋱ ⋱ ⋮ 1 u n − 2 β n − 2 1 β n − 1 1 ] L = \begin{bmatrix} d_1 & & & & & & \\ a_2 & d_2 & & & & & \\ & \ddots & \ddots & & & & \\ & & a_{n-2} & d_{n-2} & & & \\ & & & a_{n-1} & d_{n-1} & & \\ \alpha_1 & \alpha_2 & \cdots & \alpha_{n-2} & \alpha_{n-1} & d_n & \end{bmatrix}, \\ U = \begin{bmatrix} 1 & u_1 & & & & \beta_1 \\ & 1 & u_2 & & & \beta_2 \\ & & \ddots & \ddots & & \vdots \\ & & & 1 & u_{n-2} & \beta_{n-2} \\ & & & & 1 &\beta_{n-1}\\ & & & & & 1 \end{bmatrix} L= d1a2α1d2⋱α2⋱an−2⋯dn−2an−1αn−2dn−1αn−1dn ,U= 1u11u2⋱⋱1un−21β1β2⋮βn−2βn−11

其中
d 1 = b 1 , u 1 = c 1 / d 1 , α 1 = c n , β 1 = α 1 / d 1 , d_1 = b_1, \quad u_1 = c_1/d_1, \quad \alpha_1 = c_n, \quad \beta_1 = \alpha_1/d_1, d1=b1,u1=c1/d1,α1=cn,β1=α1/d1,
{ d i = b i − a i u i − 1 u i = c i / d i , i = 2 , 3 , ... , n − 2 α i = − α i − 1 u i − 1 \begin{cases} d_i &= b_i - a_i u_{i-1} \\ u_i &= c_i/d_i , \quad i = 2,3,\dots,n-2 \\ \alpha_i &= -\alpha_{i-1}u_{i-1} \end{cases} \\ ⎩ ⎨ ⎧diuiαi=bi−aiui−1=ci/di,i=2,3,...,n−2=−αi−1ui−1
d n − 1 = b n − 1 − α n − 2 u n − 2 , α n − 1 = a n − α n − 2 u n − 2 , β n − 1 = ( c n − 1 − a n − 1 β n − 2 ) / d n − 1 , d n = b n − ∑ i = 1 n − 1 α i β i . d_{n-1} = b_{n-1} - \alpha_{n-2}u_{n-2}, \\ \alpha_{n-1} = a_n - \alpha_{n-2}u_{n-2}, \\ \beta_{n-1} = (c_{n-1} - a_{n-1}\beta_{n-2})/d_{n-1}, \\ d_n = b_n - \sum_{i=1}^{n-1}\alpha_i\beta_i. dn−1=bn−1−αn−2un−2,αn−1=an−αn−2un−2,βn−1=(cn−1−an−1βn−2)/dn−1,dn=bn−i=1∑n−1αiβi.

二、首先求解方程 L y = f Ly=f Ly=f
{ y 1 = f 1 / d 1 , y i = ( f i − a i y i − 1 ) / d i , i = 2 , 3 , ... , n − 1 , y n = ( f n − ∑ i = 1 n − 1 α i y i ) / d n . \begin{aligned} \begin{cases} y_1 &= f_1/d_1, \\ y_i &= (f_i - a_i y_{i-1})/d_i, \quad i = 2,3,\dots,n-1, \\ y_n &= (f_n - \sum_{i=1}^{n-1}\alpha_i y_i)/d_n. \end{cases} \end{aligned} ⎩ ⎨ ⎧y1yiyn=f1/d1,=(fi−aiyi−1)/di,i=2,3,...,n−1,=(fn−∑i=1n−1αiyi)/dn.

三、然后求解方程 U x = y Ux=y Ux=y
{ x n = y n , x n − 1 = y n − 1 − β n − 1 x n , x i = y i − u i x i + 1 − β i x n , i = n − 2 , n − 3 , ... , 2 , 1. \begin{aligned} \begin{cases} x_n &= y_n, \\ x_{n-1} &= y_{n-1} - \beta_{n-1}x_n, \\ x_i &= y_i - u_ix_{i+1} - \beta_ix_n, \quad i = n-2,n-3,\dots,2,1. \end{cases} \end{aligned} ⎩ ⎨ ⎧xnxn−1xi=yn,=yn−1−βn−1xn,=yi−uixi+1−βixn,i=n−2,n−3,...,2,1.

代码实现

matlab 复制代码
function x = CTTChase(a,b,c,f)
n = length (f); % 矩阵维度
c(1) = c(1)/b(1);
alpha = zeros(n-1,1);
alpha(1) = c(n); 
beta = zeros(n-1,1);
beta(1) = alpha(1)/b(1);
for i = 2:n-2
    b(i) = b(i) - a(i)*c(i-1);
    c(i) = c(i)/b(i);
    alpha(i) = - alpha(i-1)*c(i-1);
    beta(i) = - (beta(i-1)*a(i))/b(i);
end
b(n-1) = b(n-1) - alpha(n-2)*c(n-2);
alpha(n-1) = a(n) - alpha(n-2)*c(n-2);
beta(n-1) = (c(n-1)-a(n-1)*beta(n-2))/b(n-1);
b(n) = b(n) - sum(alpha.*beta);

f(1) = f(1)/b(1);
for i = 2:n-1
    f(i) = (f(i)-a(i)*f(i-1))/b(i);
end
sum1 = 0;
for i = 1:n-1
    sum1 = sum1 + alpha(i)*f(i);
end
f(n) = (f(n)-sum1)/b(n);

x(n) = f(n);
x(n-1) = f(n) - beta(n-1)*x(n);
for i = n-2:-1:1
    x(i) = f(i) - c(i)*x(i+1) - beta(i)*x(n);
end
end

五对角

算法步骤

一、替换
b i ∗ = { b i if i = 2 , b i − a i d i − 2 ∗ if i = 3 , 4 , ... , n . b^{*}{i} = \begin{cases} b{i} & \text{if }i = 2, \\ b_{i} - a_{i}d^{*}{i-2} & \text{if }i = 3,4,\ldots,n. \end{cases} \\ bi∗={bibi−aidi−2∗if i=2,if i=3,4,...,n.
c i ∗ = { c i if i = 1 , c i − b i ∗ d i − 1 c i − 1 if i = 2 , c i − a i e i − 2 ∗ − b i ∗ d i − 1 ∗ if i = 3 , 4 , ... , n . c^{*}
{i} = \begin{cases} c_{i} & \text{if }i = 1, \\ c_{i} - \frac{b^{*}{i}d{i-1}}{c_{i-1}} & \text{if }i = 2, \\ c_{i} - a_{i}e^{*}{i-2} - b^{*}{i}d^{*}{i-1} & \text{if }i = 3,4,\ldots,n. \end{cases} \\ ci∗=⎩ ⎨ ⎧cici−ci−1bi∗di−1ci−aiei−2∗−bi∗di−1∗if i=1,if i=2,if i=3,4,...,n.
d i ∗ = { d i c i if i = 1 , d i − b i ∗ d i − 1 ∗ c i ∗ if i = 2 , 3 , ... , n − 1. d^{*}
{i} = \begin{cases} \frac{d_{i}}{c_{i}} & \text{if }i = 1, \\ \frac{d_{i} - b^{*}{i}d^{*}{i-1}}{c^{*}{i}} & \text{if }i = 2,3,\ldots,n-1. \end{cases} \\ di∗={cidici∗di−bi∗di−1∗if i=1,if i=2,3,...,n−1.
e i ∗ = e i c i ∗ i = 1 , 2 , ... , n − 2. e^{*}
{i} = \frac{e_{i}}{c^{*}_{i}} \quad i = 1,2,\ldots,n-2. ei∗=ci∗eii=1,2,...,n−2.

二、设 L y = f Ly = f Ly=f,求 y y y.
y i ∗ = { y i c i if i = 1 , y i − b i ∗ y i − 1 c i ∗ if i = 2 y i − a i y i − 2 ∗ − b i ∗ y i − 1 ∗ c i ∗ if i = 3 , 4 , ... , n . y^{*}{i} = \begin{cases} \frac{y{i}}{c_{i}} & \text{if }i = 1, \\ \frac{y_{i} - b^{*}{i}y{i-1}}{c^{*}{i}} & \text{if }i = 2 \\ \frac{y{i} - a_{i}y^{*}{i-2} - b^{*}{i}y^{*}{i-1}}{c^{*}{i}} & \text{if }i = 3,4,\ldots,n. \end{cases} yi∗=⎩ ⎨ ⎧ciyici∗yi−bi∗yi−1ci∗yi−aiyi−2∗−bi∗yi−1∗if i=1,if i=2if i=3,4,...,n.

三、计算解向量 x x x
{ x n = y n ∗ x n − 1 = y n − 1 ∗ − d n − 1 ∗ x n x i = y i ∗ − d i ∗ x i + 1 − e i ∗ x i + 2 for i = n − 2 , n − 3 , ... , 1. \begin{cases} x_{n} = y^{*}{n} \\ x{n-1} = y^{*}{n-1} - d^{*}{n-1}x_{n} \\ x_{i} = y^{*}{i} - d^{*}{i}x_{i+1} - e^{*}{i}x{i+2} \text{ for } i = n-2,n-3,\ldots,1. \end{cases} ⎩ ⎨ ⎧xn=yn∗xn−1=yn−1∗−dn−1∗xnxi=yi∗−di∗xi+1−ei∗xi+2 for i=n−2,n−3,...,1.

代码实现

matlab 复制代码
function x = Pentadiagonal(a, b, c, d, e, f)
a=[0,0,a];
b=[0,b];
d=[d,0];
e=[e,0,0];
n = length (f); % 矩阵维度
alpha=zeros(n,1); %1~n
ganma=zeros(n,1);%2~n
z=zeros(n,1);%3~n

beta=zeros(n,1);%1~n-1
q=zeros(n,1);%1~n-2

alpha(1)=c(1);
beta(1)=d(1)/alpha(1);
% beta(1)=d(1)/d(2);

ganma(2)=b(2);
alpha(2)=c(2)-ganma(2)*beta(1);

for i=1:2
    q(i)=e(i)/alpha(i);
end

beta(2)=(d(2)-ganma(2)*q(1))/alpha(2);

for i=3:n
    if i<=n-2
        z(i)=a(i);
        ganma(i)=b(i)-z(i)*beta(i-2);
        alpha(i)=c(i)-z(i)*q(i-2)-ganma(i)*beta(i-1);
        q(i)=e(i)/alpha(i);
        beta(i)=(d(i)-ganma(i)*q(i-1))/alpha(i);
    elseif i<=n-1
        z(i)=a(i);
        ganma(i)=b(i)-z(i)*beta(i-2);
        alpha(i)=c(i)-z(i)*q(i-2)-ganma(i)*beta(i-1);
        beta(i)=(d(i)-ganma(i)*q(i-1))/alpha(i);
    else
        z(i)=a(i);
        ganma(i)=b(i)-z(i)*beta(i-2);
        alpha(i)=c(i)-z(i)*q(i-2)-ganma(i)*beta(i-1);
    end
end

y=zeros(n,1);
y(1)=f(1)/alpha(1);
y(2)=(f(2)-ganma(2)*y(1))/alpha(2);
for i=3:n
    y(i)=(f(i)-z(i)*y(i-2)-ganma(i)*y(i-1))/alpha(i);
end

x=zeros(n,1);
x(n)=y(n);
x(n-1)=y(n-1)-beta(n-1)*x(n);
for i=n-2:-1:1
    x(i)=y(i)-q(i)*x(i+2)-beta(i)*x(i+1);
end
end

Latex公式源码文件

下载地址:点我

觉得有帮助的话,关注汐新酱喵,关注汐新酱喵谢谢喵!

参考资料

[1] 金一庆、陈越、王冬梅,数值方法,机械工业出版社,2000

[2] 李青、王能超,解循环三对角线性方程组的追赶法,小型微型计算机系统,第23卷第11期,2002年11月

[3] 王礼广,蔡放,熊岳山,五对角线性方程组追赶法,南华大学学报(自然科学版),第22卷第1期,2008年3月

相关推荐
荒古前几秒前
龟兔赛跑 PTA
c语言·算法
Colinnian4 分钟前
Codeforces Round 994 (Div. 2)-D题
算法·动态规划
用户0099383143019 分钟前
代码随想录算法训练营第十三天 | 二叉树part01
数据结构·算法
shinelord明13 分钟前
【再谈设计模式】享元模式~对象共享的优化妙手
开发语言·数据结构·算法·设计模式·软件工程
დ旧言~19 分钟前
专题八:背包问题
算法·leetcode·动态规划·推荐算法
_WndProc37 分钟前
C++ 日志输出
开发语言·c++·算法
努力学习编程的伍大侠1 小时前
基础排序算法
数据结构·c++·算法
XiaoLeisj1 小时前
【递归,搜索与回溯算法 & 综合练习】深入理解暴搜决策树:递归,搜索与回溯算法综合小专题(二)
数据结构·算法·leetcode·决策树·深度优先·剪枝