Gauss Seidel method in matlab to find the roots
2 views (last 30 days)
Show older comments
Hello everyone, i have a porblem with Gauss Seidel.I have 4 equations and 4 unknown. I am giving the the initial conditions and ı also arranged the equations with respect to diagonal row ( so the diagonal side has to be bigger). If the maximum error will be lower than 1% error, ıt should give me values (x1,x2,x3,x4). May you please find where i am doing wrong. I really appreciate...
clc;clear; format ('long' , 'g'); x1(1)=1;x2(1)=1; x3(1)=1;x4(1)=1; error=1;
while error >=0,01
i=1;
x1(i+1)=(-23+x2(i)-x3(i)+2*x4(i))/4;
x3(i+1)=((2*x1(i+1)-6*x2(i)+3*x4(i)+21));
x2(i+1)=(-x1(i+1)+11+5*x3(i+1)+x4(i))/2;
x4(i+1)=(22+x1(i+1)-2*x2(i+1)+3*x3(i+1))/6;
error_x1(i+1)=abs((x1(i+1)-x1(i)))/x1(i+1);
error_x3(i+1)=abs((x3(i+1)-x3(i)))/x3(i+1);
error_x2(i+1)=abs((x2(i+1)-x2(i)))/x2(i+1);
error_x4(i+1)=abs((x4(i+1)-x4(i)))/x4(i+1);
A=[error_x1 ;error_x3 ;error_x2;error_x4];
error=max(A);
i=i+1;
end
disp ( ' x1(i+1) error(%) ')
disp([ x1 ' error_x1'])
disp ( ' x3(i+1) error(%)')
disp([ x3 'error_x3'])
disp ( ' x2(i+1) error(%)')
disp([ x2 'error_x2'])
disp ( ' x4(i+1) error(%)')
disp([ x4 'error_x4'])
0 Comments
Answers (2)
Paul
on 25 Feb 2014
First of all 0,01 should be 0.01. Secondly, i=1; should be outside the while loop, else the value of i is reset every loop. I also fixed your display commands:
clc;clear; format ('long' , 'g'); x1(1)=1;x2(1)=1; x3(1)=1;x4(1)=1; error=1;
i=1;
while error >=0.01
x1(i+1)=(-23+x2(i)-x3(i)+2*x4(i))/4;
x3(i+1)=((2*x1(i+1)-6*x2(i)+3*x4(i)+21));
x2(i+1)=(-x1(i+1)+11+5*x3(i+1)+x4(i))/2;
x4(i+1)=(22+x1(i+1)-2*x2(i+1)+3*x3(i+1))/6;
error_x1(i+1)=abs((x1(i+1)-x1(i)))/x1(i+1);
error_x3(i+1)=abs((x3(i+1)-x3(i)))/x3(i+1);
error_x2(i+1)=abs((x2(i+1)-x2(i)))/x2(i+1);
error_x4(i+1)=abs((x4(i+1)-x4(i)))/x4(i+1);
A=[error_x1 ;error_x3 ;error_x2;error_x4];
error=max(A);
i=i+1;
end
disp ( ' x1(i+1) error(%) ')
disp([x1(end) error_x1(end)])
disp ( ' x3(i+1) error(%)')
disp([x2(end) error_x2(end)])
disp ( ' x2(i+1) error(%)')
disp([x3(end) error_x3(end)])
disp ( ' x4(i+1) error(%)')
disp([x4(end) error_x4(end)])
0 Comments
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!