Gauss Seidel method in matlab to find the roots

2 vues (au cours des 30 derniers jours)
Ilke
Ilke le 25 Fév 2014
Commenté : Paul le 25 Fév 2014
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'])

Réponses (2)

Paul
Paul le 25 Fév 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)])

Ilke
Ilke le 25 Fév 2014
Firstly, i am really thankful for your answering. When i run your codes, it worked but it gave me wrong results and error is really high.True results must be [-4 3 -2 1].I think , the problem is with errors. i am looking where it is wrong. x1(i+1) error(%) -5.25 -1.19047619047619
x3(i+1) error(%)
27.375 0.963470319634703
x2(i+1) error(%)
7.5 0.866666666666667
x4(i+1) error(%)
-2.58333333333333 -1.38709677419355
  1 commentaire
Paul
Paul le 25 Fév 2014
A=[error_x1 ;error_x3 ;error_x2;error_x4];
should be:
A=[error_x1(i+1) ;error_x3(i+1) ;error_x2(i+1);error_x4(i+1)];
Still the values do not converge so you probably have some errors in your equations.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by