一、高斯法和Doolitle法
高斯法和Doolitle法都比较简单,其实Doolitle可以直接用maltab里面的lu命令来求,下面提一下迭代法
二、Jacobi迭代
1.算法实现
2.收敛条件

3.matlab实现
根据以上的算法可以写出一个简单的jacobi迭代如下:
复制代码
1
2
3
4
5
6
7
8function x = jacobi(a,b,k) n=length(b); d=diag(a); r=a-diag(d); x=zeros(n,1); for j = 1:k x = (b-r*x)./d; end
以上代码是最简单的写法,但是做题的时候一般要求把迭代过程给显示,这样写的话就不够完善,所以有以下拓展版。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39function r = jacobi(A,B,varargin) sizeA=size(A); sizev=size(varargin); if sizev(2) == 0 rol = 0.000001; n = 1000; x = zeros(sizeA(1),1); elseif sizev(2) == 1 rol = varargin{1}; n = 1000; x = zeros(sizeA(1),1); elseif sizev(2) == 2 rol = varargin{1}; n = varargin{2}; x = zeros(sizeA(1),1000); elseif sizev(2) == 3 rol = varargin{1}; n = varargin{2}; x = varargin{3}; else error("输入参数过多"); end for i = 2:n for j = 1:sizeA(2) sum1=0; for k = 1:sizeA(1) if j == k continue; end sum1 = sum1 - x(k,i-1)*A(j,k)/A(j,j); end x(j,i)=B(j)/A(j,j)+sum1; end if any(abs(x(:,i)-x(:,i-1))>rol) == 0 break; end end r = x; end
三、Gauss-Seidel迭代法
1.算法实现
2.收敛条件
3.matlab实现
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42function r = Gauss_Seidel(A,B,varargin) sizeA=size(A); sizev=size(varargin); if sizev(2) == 0 rol = 0.000001; n = 1000; x = zeros(sizeA(1),1); elseif sizev(2) == 1 rol = varargin{1}; n = 1000; x = zeros(sizeA(1),1); elseif sizev(2) == 2 rol = varargin{1}; n = varargin{2}; x = zeros(sizeA(1),1000); elseif sizev(2) == 3 rol = varargin{1}; n = varargin{2}; x = varargin{3}; else error("输入参数过多"); end for i = 2:n for j = 1:sizeA(2) sum1=0; for k = 1:j if j == k continue; end sum1 = sum1 - x(k,i)*A(j,k)/A(j,j); end for k = j+1:sizeA(1) sum1 = sum1 - x(k,i-1)*A(j,k)/A(j,j); end x(j,i)=B(j)/A(j,j)+sum1; end if any(abs(x(:,i)-x(:,i-1))>rol) == 0 break; end end r = x; end
四、逐次超松弛(SOR)迭代法
1.算法实现
2.收敛条件
3.matlab实现
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43function r = SOR(A,B,w,varargin) sizeA=size(A); sizev=size(varargin); if sizev(2) == 0 rol = 0.000001; n = 1000; x = zeros(sizeA(1),1); elseif sizev(2) == 1 rol = varargin{1}; n = 1000; x = zeros(sizeA(1),1); elseif sizev(2) == 2 rol = varargin{1}; n = varargin{2}; x = zeros(sizeA(1),1000); elseif sizev(2) == 3 rol = varargin{1}; n = varargin{2}; x = varargin{3}; else error("输入参数过多"); end for i = 2:n for j = 1:sizeA(2) sum1=0; for k = 1:j if j == k sum1 = sum1 - w*(1-1/w)*x(k,i-1); continue; end sum1 = sum1 - w*x(k,i)*A(j,k)/A(j,j); end for k = j+1:sizeA(1) sum1 = sum1 - w*x(k,i-1)*A(j,k)/A(j,j); end x(j,i)=w*B(j)/A(j,j)+sum1; end if any(abs(x(:,i)-x(:,i-1))>rol) == 0 break; end end r = x; end
最后
以上就是高挑小懒猪最近收集整理的关于matlab简单迭代法_MATLAB学数值分析(二) 迭代法解线性方程组的全部内容,更多相关matlab简单迭代法_MATLAB学数值分析(二)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复