资源简介
包含代码和文档
分别用雅克比迭代法和高斯-赛德尔迭代法求解方程组,精确到小数点后6位,分别就 给出相应的计算结果
分别用雅克比迭代法和高斯-赛德尔迭代法求解方程组,精确到小数点后6位,分别就 给出相应的计算结果
代码片段和文件信息
clc
clear
format long
N=30;
A=zeros(NN);
for i=1:N-1
A(ii)=3;
A(ii+1)=-1;
A(i+1i)=-1;
end
A(NN)=3;
b=ones(N1);
b(1)=2;
b(N)=2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%雅克比迭代
D=diag(diag(A));
L=tril(A-1);
U=triu(A1);
BJ=inv(D)*(L+U);
fJ=inv(D)*b;
x=ones(N1);
newx=x;
while true
newx=BJ*x+fJ;
error=abs(norm(newx1)-norm(x1));%用范数作为终止条件
if error<1e-6
break
end
x=newx;
end
%雅克比迭代结果
fprintf(‘1.雅克比迭代结果为:‘)
Jx=round(newx6);
vpa(Jx6)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%高斯-赛德尔迭代
BG=inv((D-L))*U;
fG=inv((D-L))*b;
while true
newx=BG*x+fG;
error=abs(norm(newx1)-norm(x1));%用范数作为终止条件
if error<1e-6
break
end
x=newx;
end
%高斯-赛德尔迭代结果
fprintf(‘2.高斯-赛德尔迭代结果:‘)
Gx=round(newx6);
vpa(Gx6)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 51152 2019-12-17 11:07 2-7\2-7文档.docx
文件 969 2019-12-17 10:54 2-7\m2_7.m
目录 0 2019-12-26 09:22 2-7\
评论
共有 条评论