有关雅克比迭代的问题
各位前辈,小弟分别用VB.NET和matlab编了雅克比迭代计算程序,可是运行出来的结果却有偏差,已知matlab的结果是正确的,想请教各位帮忙给看看VB.NET版的程序哪里有问题。谢谢各位了!!
matlab版的:
clear allclca=[2028.977 0 0 0 0 0 0 0;... 214.036 2126.041 0 0 0 0 0 0;... 117.6175 117.6175 2535.178 0 0 0 0 0;... 107.6837 107.6837 107.6837 2467.399 0 0 0 0;... 92.44788 92.44788 92.44788 92.44788 2375.027 0 0 0;... 156.3591 156.3591 156.3591 156.3591 83.33392 2343.634 0 0;... 152.769 152.769 152.769 152.769 303.2207 303.2207 2444.594 0;... 95.47997 95.47997 95.47997 95.47997 95.47997 95.47997 95.47997 2237.363;]t=[98.78857 215.5781 119.2549 62.34533 94.91241 158.5648 154.924 96.82684]d=[0 0 0 31.94817 0 0 9.221128 3.941186]c=(t-d)'zz=a\cA=a;b=c;D=0;n=1;%-------------------------Jacobi迭代------------------------%for i=1:8 x(i)=0;endfor i=1:8 y(i)=b(i); for j=1:i-1 y(i)=y(i)-A(i,j)*x(j); end for j=i+1:8 y(i)=y(i)-A(i,j)*x(j); end y(i)=y(i)/A(i,i); if abs(x(i)-y(i))>0 D=abs(x(i)-y(i)); endendfor i=1:8 x(i)=y(i);endwhile D>=0.001 %循环条件,误差小于0.001 D=0; n=n+1; %总循环次数for i=1:8 y(i)=b(i); for j=1:i-1 y(i)=y(i)-A(i,j)*x(j); end for j=i+1:8 y(i)=y(i)-A(i,j)*x(j); end y(i)=y(i)/A(i,i); if abs(x(i)-y(i))>0 D=abs(x(i)-y(i)); endendfor i=1:8 x(i)=y(i);endendn;x
Public Function Jacobi(ByVal a(,) As Double, ByVal b() As Double, ByVal n As Integer) Dim x(), D As Double Dim y() As Double Dim i, j As Integer ReDim Preserve x(n), y(n) For i = 0 To n x(i) = 0 Next For i = 0 To n y(i) = b(i) For j = 0 To (i - 1) y(i) = y(i) - a(i, j) * x(j) Next For j = (i + 1) To n y(i) = y(i) - a(i, j) * x(j) Next y(i) = y(i) / a(i, i) If Math.Abs(x(i) - y(i)) > 0 Then D = Math.Abs(x(i) - y(i)) End If Next For i = 0 To n x(i) = y(i) Next Do While D >= 0.001 D = 0 For i = 0 To n y(i) = b(i) For j = 0 To (i - 1) y(i) = y(i) - a(i, j) * x(j) Next For j = (i + 1) To n y(i) = y(i) - a(i, j) * x(j) Next y(i) = y(i) / a(i, i) If Math.Abs(x(i) - y(i)) > 0 Then D = Math.Abs(x(i) - y(i)) End If Next For i = 0 To n x(i) = y(i) Next Loop Jacobi = x End Function