Sunday 18 August 2019

Program - Gauss seidel method | convergence test


/*gauss seidel method*/
#include<stdio.h>
#include<math.h>
void
main()
{
float a[20][20],b[20],x[20],error,sum=0.0,e,s=0.0;
int n,i,j,c=0,f=0;
/*taking inputs*/
printf("\n enter the number of variables:");
 scanf("%d",&n);
 printf("\n enter the coefficient matrix rowwise\n");
 for(j=0;j<n;j++)
 {
                 for(i=0;i<n;i++)
                 {
                                 printf("\n\t a[%d][%d]=",j,i);
                                 scanf("%f",&a[j][i]);
                 }
 }
 printf("\n enter the constants:\n");
for(i=0;i<n;i++)
{
                printf("\n\t b[%d]=",i);
                scanf("%f",&b[i]);
}
/*convergence test*/
for(j=0;j<n;j++)

{
                sum=0;
                for(i=0;i<n;i++)
                {
                                if(i!=j)
                                                sum=sum+fabs(a[i][j]);
                }

if(fabs(a[j][j]>sum))
{
                f++;
}
}
if(f==0)
{
                printf("\n the solution does not converge");
                goto end;
}
else
{
                printf("\n\t %d equations satisfy the conditions",f);
}
/*iteration process*/
printf("\n enter the guess value of the solution \n");
for(i=0;i<n;i++)
{
                printf("\n\t x[%d]=",i);
                scanf("%f",&x[i]);
}
f=0;
printf("\n enter the required amount of accuracy");
scanf("%f",&error);
/*iteration loop*/
label:
{
                e=error;
                for(j=0;j<n;j++)
                {
                                s=0;
                                for(i=0;i<n;i++)
                                {
                                                if(i!=j)
                                                {
                                                                s=s+a[j][i]*x[i];
                                                }
                                }
                                sum=(b[j]-s)/a[j][j];
                                if(fabs(sum-x[j]<error))
                                {
                                                x[j]=sum;
                                }
                                else
                                {
                                                e=fabs(sum-x[j]);
                                                x[j]=sum;
                                }
                }
                c++;
                printf("\n number of iteration is %d \n",c);
                for(i=0;i<n;i++)
                {
                                printf("\n\t x[%d]=%f\n",i,x[i]);
                }
}
if(e>error)
goto label;
end:;
}
RESULT1:
 enter the number of variables:3
 enter the coefficient matrix rowwise
         a[0][0]=12
         a[0][1]=3
         a[0][2]=-5
         a[1][0]=1
         a[1][1]=5
         a[1][2]=3
         a[2][0]=3
         a[2][1]=7
         a[2][2]=13
 enter the constants:
         b[0]=1
         b[1]=28
         b[2]=76
         2 equations satisfy the conditions
 enter the guess value of the solution
         x[0]=1
         x[1]=1
         x[2]=1
 enter the required amount of accuracy0.001
 number of iteration is 1
         x[0]=0.250000
         x[1]=4.950000
         x[2]=3.123077
 number of iteration is 2
         x[0]=0.147116
         x[1]=3.696731
         x[2]=3.821657
 number of iteration is 3
         x[0]=0.751508
         x[1]=3.156704
         x[2]=3.972965
 number of iteration is 4
         x[0]=0.949559
         x[1]=3.026309
         x[2]=3.997474
 number of iteration is 5
         x[0]=0.992370
         x[1]=3.003042
         x[2]=4.000123
 number of iteration is 6
         x[0]=0.999291
         x[1]=3.000068
         x[2]=4.000127

 number of iteration is 7
         x[0]=1.000036
         x[1]=2.999917
         x[2]=4.000037
Press any key to continue
RESULT2:
 enter the number of variables:4
 enter the coefficient matrix rowwise
         a[0][0]=10
         a[0][1]=-2
         a[0][2]=-1
         a[0][3]=-1
         a[1][0]=-2
         a[1][1]=10
         a[1][2]=-1
         a[1][3]=-1
         a[2][0]=-1
         a[2][1]=-1
         a[2][2]=10
         a[2][3]=-2
         a[3][0]=-1
         a[3][1]=-1
         a[3][2]=-2
         a[3][3]=10
 enter the constants:
         b[0]=3
         b[1]=15
         b[2]=27
         b[3]=-9
         4 equations satisfy the conditions
 enter the guess value of the solution
         x[0]=1
         x[1]=1
         x[2]=1
         x[3]=1
 enter the required amount of accuracy0.001
 number of iteration is 1
         x[0]=0.700000
         x[1]=1.840000
         x[2]=3.154000
         x[3]=-0.015200
 number of iteration is 2
         x[0]=0.981880
         x[1]=2.010256
         x[2]=2.996174
         x[3]=-0.001552
 number of iteration is 3
         x[0]=1.001513
         x[1]=1.999765
         x[2]=2.999818
         x[3]=0.000091
 number of iteration is 4
         x[0]=0.999944
         x[1]=1.999980
         x[2]=3.000011
         x[3]=-0.000006
Press any key to continue

No comments:

Post a Comment

Thanks for comment.