/* To find the solutions of simultaneous linear equations by Gauss-Siedel method */
#include<stdio.h>
#include<math.h>
float X1(float x2,float x3)
{
return(14-5*x2+2*x3)/20;
}
float X2(float x1,float x3)
{
return(17-3*x1-x3)/10;
}
float X3(float x2,float x1)
{
return(23-x1+4*x2)/10;
}
int main()
{
float x1=0,x2=0,x3=0,acc,y1,y2,y3;
int i=0;
printf("Enter desired accuracy:");
scanf("%f",&acc);
printf("\n x1 \t x2 \t x3 \n");
printf("\n %f %f %f",x1,x2,x3);
do
{
y1=X1(x2,x3);
y2=X2(y1,x3);
y3=X3(y1,y2);
if(fabs(y1-x1)>=acc && fabs(y2-x2)>=acc && fabs(y3-x3)>=acc)
{
x1=y1;
x2=y2;
x3=y3;
printf("\n %f %f %f",x1,x2,x3);
}
else
{
printf("\n Required solution:");
printf("\nx1=%f",y1);
printf("\nx2=%f",y2);
printf("\nx3=%f\n",y3);
i=1;
}
}
while(i!=1);
}
#include<stdio.h>
#include<math.h>
float X1(float x2,float x3)
{
return(14-5*x2+2*x3)/20;
}
float X2(float x1,float x3)
{
return(17-3*x1-x3)/10;
}
float X3(float x2,float x1)
{
return(23-x1+4*x2)/10;
}
int main()
{
float x1=0,x2=0,x3=0,acc,y1,y2,y3;
int i=0;
printf("Enter desired accuracy:");
scanf("%f",&acc);
printf("\n x1 \t x2 \t x3 \n");
printf("\n %f %f %f",x1,x2,x3);
do
{
y1=X1(x2,x3);
y2=X2(y1,x3);
y3=X3(y1,y2);
if(fabs(y1-x1)>=acc && fabs(y2-x2)>=acc && fabs(y3-x3)>=acc)
{
x1=y1;
x2=y2;
x3=y3;
printf("\n %f %f %f",x1,x2,x3);
}
else
{
printf("\n Required solution:");
printf("\nx1=%f",y1);
printf("\nx2=%f",y2);
printf("\nx3=%f\n",y3);
i=1;
}
}
while(i!=1);
}
Comments
Post a Comment