Numerical Calculation Method-Solving Linear Equations

Requirements:
① Write a function to solve AX=b, that is, the input is A, b, but A can only be an upper or lower triangular matrix.
②Write a function, solve AX=b, use Gaussian elimination method (including selecting pivot and not selecting pivot), and call ①function to solve X.
③Write a function to perform doolittle decomposition of matrix A, and output the unit lower triangular matrix L and upper triangular matrix U; if the principal element is selected, the permutation matrix P will be output at the same time.
④Write a function to perform cholesky decomposition on the positive definite matrix A and output the lower triangular matrix L;
Theory:
①There are two general solving methods for solving linear equations, one is the direct solution method, and the other is the iterative method.
② The direct method is a method to accurately solve the equations with finite steps and four operations. But there must be rounding errors in actual calculations, so this method can only get an approximate solution.
③Iterative method is to give an initial approximate value of a solution first, and then find a more accurate solution according to a certain rule, that is, a method of gradually approaching an accurate solution with a certain limit process.
matlab code:
1. Solve for AX=b (A is the upper triangle)

function x=slo(A,b)
    n = length(b);
    x = zeros(n,1);
    x(n) = b(n)/A(n,n);
if istriu(A)
    for i = n-1:-1:1
        x(i) = (b(i)-A(i,i+1:n)*x(i+1:n))/A(i,i);
    end  
else
    disp("输入错误,请输入上三角")
end
end

2. Using Gaussian elimination method for solving AX = b (including primary cells, and not selected from the group selected from PCA)
is not selected from the pivot

%不选主元
digits(9);
A = vpa([10^-8,2,3;-1,3.712,4.623;-2,1.072,5.643]);
b = [1;2;3]';
[m,n]=size(A);
 
%检查方程组是否存在唯一解
if rank(A)~=rank([A,b])
    error('矩阵A的秩和增广矩阵的秩不相同,方程不存在唯一解');
    return;
end
 
%增广矩阵行变换求解
c=n+1;
A(:,c)=b; 

%高斯消元
for k=1:n-1
    A(k+1:n, k:c) = A(k+1:n, k:c)-(A(k+1:n,k)/ A(k,k))*A(k, k:c)
end
 
%回代求结果
x=zeros(length(b),1);  
x(n)=A(n,c)/A(n,n);
for k=n-1:-1:1
x(k)=(A(k,c)-A(k,k+1:n)*x(k+1:n))/A(k,k);
end
 
%计算结果
disp('x=');
disp(x);

Running result:
Insert picture description here
choose the pivot

%选主元
clc;clear all;
 
A = [10^-8,2,3;-1,3.712,4.623;-2,1.072,5.643];
b = [1;2;3];
 
n = length(A);
m = zeros(n);%存放行乘数
 
format long
for k = 1:n-1
    temp = abs(A(k:n,k));%比较绝对值的最大值
    [value index] = max(temp);%选主元
    u = index + k - 1;%调整为正确的最大行的索引
    if u ~= k %选主元,换行
        temp1 = A(u,k:n);
        temp2 = b(u);
        A(u,k:n) = A(k,k:n);
        b(u) = b(k);
        A(k,k:n) = temp1;
        b(k) = temp2;
    end 
    m(k+1:n,k) = A( k+1:n,k)/A(k,k);%计算行乘数
    A(k+1:n,k:n) = A(k+1:n,k:n) - m(k+1:n,k)*A(k,k:n);
    b(k+1:n) = b(k+1:n) - m(k+1:n,k)*b(k);
end
slo(A,b);   %调用回代求解过程

Operation result:
Insert picture description here
3. Doolittle decomposition of matrix A (including selection of pivotal elements and non-selection of pivotal elements)

function doolittle_1(A)
[row,list] = size(A); 
 
P = eye(row);%置换矩阵P
for k = 1:row-1
    A(k:row,k) = A(k:row,k) - A(k:row,1:k-1) * A(1:k-1,k);
    temp = abs(A(k:row,k));
    [value index] = max(temp);%选主元
    u = index + k - 1;
    if u ~= k %换行
        temp1 = A(u,:);
        A(u,:) = A(k,:);
        A(k,:) = temp1;
        temp1 = P(u,:);
        P(u,:) = P(k,:);
        P(k,:) = temp1;
    end 
    A(k+1:row,k) = A(k+1:row,k) / A(k,k);
    A(k,k+1:list) = A(k,k+1:list) - A(k,1:k-1)*A(1:k-1,k+1);        
end
k = row;
A(k,k:list) = A(k,k:list) - A(k,1:k-1)*A(1:k-1,k:list);
 
L = eye(row);
for i = 2:row
    L(i,1:i-1) = A(i,1:i-1);
end
U = zeros(row);
for i = 1:row
    U(i,i:row) = A(i,i:row);
end
P = vpa(P)
L = vpa(L)
U = vpa(U)

Operation result:
Insert picture description here
Insert picture description here
4. cholesky decomposition

%cholesky分解
function Cholesky(A)
    [N,N]=size(A);
    X=zeros(N,1);
    Y=zeros(N,1);
    for i=1:N
        A(i,i)=sqrt(A(i,i)-A(i,1:i-1)*A(i,1:i-1)');
        for j=i+1:N
            A(j,i)=(A(j,i)-A(j,1:i-1)*A(i,1:i-1)')/A(i,i);
        end
    end
for x=1:N
    for y=1:N
        B(x,y)=A(x,y);      
        if x<y
            B(x,y)=0;
            break;
        end
    end
end
L=vpa(B) 

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46837674/article/details/113031515