Tuesday, 1 April 2014

Gauss–Seidel method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define X1(x2,x3) ((17 - 20*(x2) + 2*(x3))/20)
#define X2(x1,x3) ((-18 - 3*(x1) + (x3))/20)
#define X3(x1,x2) ((25 - 2*(x1) + 3*(x2))/20)


void main()
{
  double x1=0,x2=0,x3=0,y1,y2,y3;
  int i=0;
  clrscr();
  printf("\n__________________________________________\n");
  printf("\n   x1\t\t   x2\t\t   x3\n");
  printf("\n__________________________________________\n");
  printf("\n%f\t%f\t%f",x1,x2,x3);
  do
  {
   y1=X1(x2,x3);
   y2=X2(y1,x3);
   y3=X3(y1,y2);
   if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP )
   {
     printf("\n__________________________________________\n");
     printf("\n\nx1 = %.3lf",y1);
     printf("\n\nx2 = %.3lf",y2);
     printf("\n\nx3 = %.3lf",y3);
     i = 1;
   }
   else
   {
     x1 = y1;
     x2 = y2;
     x3 = y3;
     printf("\n%f\t%f\t%f",x1,x2,x3);
   }
  }while(i != 1);
getch();
}

3 comments:

  1. #include
    #include
    #include
    #define ESP 0.0001
    #define X1(x2,x3,x4) ((3.41-1.11*x2-0.88*x3-1.05*x4)/6.925)
    #define X2(x1,x3,x4) (-(4.14-1.11*x1+2.29*x3-1.23*x4)/4.855)
    #define X3(x1,x2,x4) ((-5.27-0.88*x1+2.14*x2-1.15*x4)/8.705)
    #define X4(x1,x2,x3) ((2.33-1.05*x1-1.23*x2-1.15*x3)/6.865)


    void main()
    {
    double x1=0,x2=0,x3=0,x4=0,y1,y2,y3,y4;
    int i=0;
    clrscr();
    printf("\n__________________________________________\n");
    printf("\n x1\t\t x2\t\t x3\n");
    printf("\n__________________________________________\n");
    printf("\n%f\t%f\t%f",x1,x2,x3,x4);
    do
    {
    y1=X1(x2,x3,y4);
    y2=X2(y1,x3,y4);
    y3=X3(y1,y2,y4);
    y4=X4(y1,y2,y3);
    if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP && fabs(y4-x4)<ESP )
    {
    printf("\n__________________________________________\n");
    printf("\n\nx1 = %.6lf",y1);
    printf("\n\nx2 = %.6lf",y2);
    printf("\n\nx3 = %.6lf",y3);
    printf("\n\nx3 = %.6lf",y4);
    i = 1;
    }
    else
    {
    x1 = y1;
    x2 = y2;
    x3 = y3;
    x4 = y4;
    printf("\n%0.6f\t%0.6f\t%0.6f/t%0.6f",x1,x2,x3,x4);
    }
    }while(i != 1);
    getch();
    }

    ReplyDelete
  2. Correct Gauss Seidel Program.....

    #include
    #include
    #include
    #define ESP 0.0001
    #define X1(x2,x3,x4) ((3.41-1.11*x2-0.88*x3-1.05*x4)/5.41+p)
    #define X2(x1,x3,x4) (-(4.14-1.11*x1+2.29*x3-1.23*x4)/6.31+p)
    #define X3(x1,x2,x4) ((-5.27-0.88*x1+2.14*x2-1.15*x4)/7.25+p)
    #define X4(x1,x2,x3) ((2.33-1.05*x1-1.23*x2-1.15*x3)/5.41+p)


    void main()
    {
    double x1=0,x2=0,x3=0,x4=0,y1,y2,y3,y4;
    int i=0;
    clrscr();
    printf("\n__________________________________________\n");
    printf("\n x1\t\t x2\t\t x3\n");
    printf("\n__________________________________________\n");
    printf("\n%f\t%f\t%f",x1,x2,x3,x4);
    do
    {
    y1=X1(x2,x3,y4);
    y2=X2(y1,x3,y4);
    y3=X3(y1,y2,y4);
    y4=X4(y1,y2,y3);
    if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP && fabs(y4-x4)<ESP )
    {
    printf("\n__________________________________________\n");
    printf("\n\nx1 = %.6lf",y1);
    printf("\n\nx2 = %.6lf",y2);
    printf("\n\nx3 = %.6lf",y3);
    printf("\n\nx3 = %.6lf",y4);
    i = 1;
    }
    else
    {
    x1 = y1;
    x2 = y2;
    x3 = y3;
    x4 = y4;
    printf("\n%0.6f\t%0.6f\t%0.6f/t%0.6f",x1,x2,x3,x4);
    }
    }while(i != 1);
    getch();
    }

    ReplyDelete
  3. Sir will you please tech me in tuition

    ReplyDelete