高斯-赛德尔迭代法

高斯-赛德尔迭代法的代码实现

function x=agui_GS(a,b)
n=length(b);
N=100;
e=10^(-4);
x0=zeros(n,1);
x=x0;
x0=x+2*e;
k=0;
a1=tril(a);
a2=inv(a1);
while norm(x0-x,inf)>e&k

 如求以下方程高斯-赛德尔迭代法_第1张图片

 

输入代码 

a=[5 2 1;-1 4 2;2 -3 10]
b=[-12;20;3]
x=agui_GS(a,b)

 

得到如下结果  

a =

     5     2     1
    -1     4     2
     2    -3    10

b =

   -12
    20
     3
k =

     1

  -2.40000000000000   4.40000000000000   2.10000000000000


k =

     2

  -4.58000000000000   2.80500000000000   2.05750000000000


k =

     3

  -3.93350000000000   2.98787500000000   1.98306250000000


k =

     4

  -3.99176250000000   3.01052812500000   2.00151093750000


k =

     5

  -4.00451343750000   2.99811617187500   2.00033753906250


k =

     6

  -3.99931397656250   3.00000273632813   1.99986361621094


k =

     7

  -3.99997381777344   3.00007473745117   2.00001718479004


k =

     8

  -4.00003333193848   2.99998307462036   2.00000158877380


x =

  -4.00003333193848
   2.99998307462036
   2.00000158877380

 

你可能感兴趣的:(matlab)