hm10
359 palavras
2 páginas
function [ x iter] = Gauss_Seidel_1(A,b,x0)%Gauss-seidel-true interation x10 = x0(1,1); x20=x0(2,1); x30=x0(3,1); iter=0; dx1=0; dx2=0; dx3=0; while(1) x11=x10; x22=x20; x33=x30; x1= (b(1,1) - A(1,3)*x30 -A(1,2)*x20)/A(1,1); x10=x1; x2= (b(2,1) - A(2,3)*x30 - A(2,1)*x10)/A(2,2); x20=x2; x3=(b(3,1) - A(3,1)*x10 - A(3,2)*x20)/A(3,3); x30=x3; if x1~=0 dx1=abs((x1-x11)/x1); end if x2~=0 dx2=abs((x2-x22)/x2); end if x3~=0 dx3=abs((x3-x33)/x3); end dxl=[dx1 dx2 dx3]; dx = max(dxl); if dx < 10^(-4)| iter > 50, break, end iter = iter+1; x = [x1 x2 x3]; fprintf('%5d %10.4f %10.4f %10.4f \n',iter, x ); end >> A=[10 2 -1; -3 -6 2; 1 1 5]
>> b=[27; -61.5; -21.5]
>> x0=[1;1;1]
>> [ x iter] = Gauss_Seidel_1(A,b,x0) 1 2.6000 9.2833 -6.6767 2 0.1757 7.9366 -5.9225 3 0.5204 8.0156 -6.0072 4 0.4962 7.9995 -5.9991 5 0.5002 8.0002 -6.0001 6 0.5000 8.0000 -6.0000 x = 0.5000 8.0000 -6.0000 iter = 6
function [ x iter] = Gauss_Jordan_1(A,b,x0)
%Gauss-Jordan interation x10 = x0(1,1); x20=x0(2,1); x30=x0(3,1); iter=0; dx1=0; dx2=0; dx3=0; while(1) x1= (b(1,1) - A(1,3)*x30 -A(1,2)*x20)/A(1,1); x2= (b(2,1) - A(2,3)*x30 - A(2,1)*x10)/A(2,2); x3=(b(3,1) - A(3,1)*x10 - A(3,2)*x20)/A(3,3); if x1~=0 dx1=abs((x1-x10)/x1); end if x2~=0 dx2=abs((x2-x20)/x2); end if x3~=0 dx3=abs((x3-x30)/x3); end dxl=[dx1 dx2 dx3]; dx = max(dxl); if dx < 10^(-4)| iter > 50, break, end x10 = x1; x20=x2; x30=x3; iter = iter+1; x = [x1 x2 x3]; fprintf('%5d %10.4f %10.4f %10.4f \n',iter, x ); end >> [ x iter] = Gauss_Jordan_1(A,b,x0) 1 2.6000 10.0833 -4.7000 2 0.2133 7.3833 -6.8367 3