追赶法解三对角线性方程组(Matlab)

 

%追赶法解三对角线性方程组

clear;clc

A=[1,2,0,0,0;2,3,1,0,0;

  0,-3,4,2,0;0,0,4,7,1;0,0,0,-5,6];

d=[5 9 2 19 -4]';

n=length(d);

U=zeros(n);

L=eye(n);

y=zeros(n,1);x=y;

%---------追--------

U(1,1)=A(1,1);y(1)=d(1);

for i=2:n

    L(i,i-1)=A(i,i-1)/U(i-1,i-1);

    U(i,i)=A(i,i)-L(i,i-1)*A(i-1,i);

    y(i)=d(i)-L(i,i-1)*y(i-1);

    U(i-1,i)=A(i-1,i);%U的上次对角线即为A的上次对角线

end

L

U

y

%------赶--------

x(n)=y(n);

for i=n-1:-1:1

    x(i)=(y(i)-A(i,i+1)*x(i+1))/U(i,i);

end

x

你可能感兴趣的:(matlab)