Experimental purpose and requirements: 1. Understand the basic principles and characteristics of each iteration method; 2. Judging the convergence of Jacobi iteration and Gauss-Seidel iteration to any initial vector; 3. Complete the program realization of Jacobi iteration and Gauss-Seidel iteration algorithm. |
||||||||||||
Experiment content: 1. Write a general subroutine for the Jacobi iterative method and the Gauss-Seidel iterative method to solve the following linear equations , and examine the convergence of the iterative process. |
||||||||||||
Experimental steps and procedures: Jacobi iteration method theory: Flow chart of Jacobi iteration method:
MATLAB main program of Jacobi iteration method The called Jacobi.m file function [x,k,index]=Jacobi(A,b,ep,N) n=length(A);k=1;index=1; x=zeros(n,1);y=zeros(n,1); while k<N for i=1:n y(i)=b(i); for j=1:n if j~=i y(i)=y(i)-A(i,j)*x(j); end end if abs(A(i,i))<1e-10 || k==N index=0; return end y(i)=y(i)/A(i,i); end if norm(y-x,inf)<ep break end k=k+1; x=y; end 运行的Jacobi_1.m文件 A=[9,-1,-1;-1,8,0;-1,0,9]; b=[7;7;8]; ep=1e-4; N=100; [x,k,index]=Jacobi(A,b,ep,N) G-S迭代法理论:
G-S迭代法的流程图: G-S迭代法的MATLAB主程序 被调用的Gao_Si_sai_de_er.m文件 function [x,k,index]= Gao_Si_sai_de_er(A,b,ep,N) n=length(A);k=1; x=zeros(n,1);y=zeros(n,1);index=1; while k<N y=x; for i=1:n z=b(i); for j=1:n if j~=i z=z-A(i,j)*x(j); end end if abs(A(i,i))<1e-10 || k==N index=0; return end z=z/A(i,i); x(i)=z; end if norm(y-x,inf)<ep break end k=k+1; end 运行的Gao_Si_sai_de_er_1.m文件
A=[9,-1,-1;-1,8,0;-1,0,9]; b=[7;7;8]; ep=1e-4; N=100; [x,k,index]=Gao_Si_sai_de_er(A,b,ep,N) |
||||||||||||
运行结果:
|
||||||||||||
结果分析与讨论:
雅克比迭代和高斯-赛德尔迭代都是迭代法求解线性方程组的算法,它们的基本思想都是通过迭代来逼近方程组的精确解。具体来说,两种算法都是将系数矩阵分解为三个部分,并针对每个未知数分别解出一个迭代更新公式。
|
Iterative method (Jacobi iteration, Gauss-Seidel iteration) of linear equations in mathematical modeling algorithm (based on matlab and python) (7/10)
Guess you like
Origin blog.csdn.net/qq_59819866/article/details/131436321
Recommended
Ranking