upsidedown

jacobi

Sep 16th, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. #define error 0.0001
  5. void main()
  6. {
  7.     double a[50][50],b[50],y[50],z[50],s=0,sum;
  8.     int f,k=1,n,i,j,key=0;
  9.     printf("enter the number of equations to enter\n");
  10.     scanf("%d",&n);
  11.     do
  12.     {
  13.         f=1;
  14.         for(i=0;i<n;i++)
  15.         {
  16.             printf("enter the coefficients of equation:%d\n",i+1);
  17.             for(j=0;j<n;j++)
  18.                 scanf("%lf",&a[i][j]);
  19.             scanf("%lf",&b[i]);
  20.             for(j=0;j<(n-1);j++)
  21.                 s=s+a[i][j];
  22.             s=s-a[i][i];
  23.             if(fabs(a[i][i])<fabs(s))
  24.             {
  25.                 printf("the equation doesnt converge re-enter equations\n");
  26.                 f=0;
  27.                 break;
  28.             }
  29.         }
  30.     }while(f==0);
  31.     for(i=0;i<n;i++)
  32.         y[i]=(b[i]/a[i][i]);
  33.     do
  34.     {
  35.         k=1;
  36.         key=0;
  37.         for(i=0;i<n;i++)
  38.         {
  39.             sum=b[i];
  40.             for(j=0;j<n;j++)
  41.             {
  42.                 if(j==i)
  43.                     break;
  44.                 sum=sum-(a[i][j]*y[j]);
  45.             }
  46.             z[i]=(sum/a[i][i]);
  47.             if(fabs((z[i]-y[i])/z[i])>error)
  48.                 key=1;
  49.         }
  50.         if(key==1)
  51.         {
  52.             for(i=0;i<n;i++)
  53.                 y[i]=z[i];
  54.             k=0;
  55.        
  56.         }
  57.     }while(k==0);  
  58.     printf("the values of the unknowns are:\n");
  59.     for(i=0;i<n;i++)
  60.         printf("x %d  %.4lf\n",i+1,z[i]);
  61. }
  62.  
  63. OUTPUT:
  64. enter the number of equations to enter
  65. 3
  66. enter the coefficients of equation:1
  67. 2 1 1 5
  68. enter the coefficients of equation:2
  69. 3 5 2 15
  70. enter the coefficients of equation:3
  71. 2 1 4 8
  72. the values of the unknowns are:
  73. x 1  2.5000
  74. x 2  1.5000
  75. x 3  0.3750
  76. Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment