Gauss Siedel C Program
Gauss Siedel C Program
Gauss Siedel C Program
Prathamesh Manave
Roll Number : 123100003
ME 704
Assignment No.3
printf("\nmatrix a\n\n");
for(i=1;i<=n;i=i+1)
{
for(j=1;j<=n;j=j+1)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
printf("\nmatrix b\n");
for(i=1;i<=n;i=i+1)
{
printf("\n%f",b[i][1]);
}
/*use of for loop for inputting initial guess */
printf("\nenter initial guess \n\n");
for(i=1;i<=n;i=i++)
{
printf("\nenter x(%d,1)\t",i);
scanf("%f",&ini[i][1]);
}
printf("\nTable of solution matrix and error norm\n\n");
/*use of do-while loop for carrying successive itterations*/
do{
/*FORMULA for finding solution by jacobi itteration*/
for(i=1;i<=n;i++)
{
sum1=0,sum2=0;
for(j=1;j<=(i-1);j++)
{
p=a[i][j]*ini[j][1];
sum1=sum1+p;
}
for(j=i+1;j<=n;j++)
{
q=a[i][j]*ini[j][1];
sum2=sum2+q;
}
x[i][1]=(1/(a[i][i]))*(b[i][1]-sum1-sum2);
}
printf("\n%d ",itt);
printf("itteration\t");
for(i=1;i<=n;i++)
{
printf("%f\t",x[i][1]);
/*display of solution*/
}
/*use of for loops for finding out ERROR NORM*/
for(i=1;i<=n;i++)
{
err[i][1]=fabs(x[i][1]-ini[i][1]);
}
max=err[1][1];
for(k=2;k<=(n);k=k+1)
{
if(err[k][1]>max)
{
max=err[k][1];
}
}
/*setting new solution as inital for successive itterations*/
for(i=1;i<=n;i++)
{
ini[i][1]=(x[i][1]);
}
/*display of error norm*/
printf("\t error norm=%f",max);
itt=itt+1;
}/*condition of accuracy*/
while(max>=0.00001);
getch;
}
/*declaration of variables*/
float a[n][n],b[n][1],x[n][1],err[n][1],p,q,sum1,sum2,norm=0,ini[n][1],max=0;
printf("ITTERATIVE PROGRAM- GAUSS SIEDEL METHOD\n\n");
printf("enter the order of matrix\t");
scanf("%d",&n);
/*use of for loops for inputting matrix elements*/
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\nenter a(%d,%d)\t",i,j);
scanf("%f",&a[i][j]);
}
printf("\nenter b(%d,1)\t",i);
scanf("%f",&b[i][1]);
}
}
printf("\n");
}
printf("\nmatrix b\n");
for(i=1;i<=n;i=i+1)
{
printf("\n%f",b[i][1]);
}
/*use of for loop for inputting initial guess */
printf("\nenter initial guess \n\n");
for(i=1;i<=n;i=i++)
{
printf("\nenter x(%d,1)\t",i);
scanf("%f",&ini[i][1]);
}
printf("\nTable of solution matrix and error norm\n\n");
/*use of do-while loop for carrying successive itterations*/
do{
/*FORMULA for finding solution by gauss siedel itteration*/
for(i=1;i<=n;i++)
{
sum1=0,sum2=0;
for(j=1;j<=(i-1);j++)
{
p=a[i][j]*x[j][1];
sum1=sum1+p;
}
for(j=i+1;j<=n;j++)
{
q=a[i][j]*x[j][1];
sum2=sum2+q;
}
x[i][1]=(1/(a[i][i]))*(b[i][1]-sum1-sum2);
}
printf("\n%d ",itt);
printf("itteration \t");
for(i=1;i<=n;i++)
{
printf("%f\t",x[i][1]);
/*display of solution*/
}
/*use of for loops for finding out ERROR NORM*/
for(i=1;i<=n;i++)
{
err[i][1]=fabs(x[i][1]-ini[i][1]);
}
max=err[1][1];
for(k=2;k<=(n);k=k+1)
{
if(err[k][1]>max)
{
max=err[k][1];
}
}
/*setting new solution as inital for successive itterations*/
for(i=1;i<=n;i++)
{
ini[i][1]=(x[i][1]);
}
/*display of error norm*/
printf("norm=%f",max);
itt=itt+1;
}/*condition of accuracy*/
while(max>=0.00001);
getch;
}
OUTPUT OF PROGRAMS
Que:1
ITTERATIVE PROGRAM- JACOBI METHOD
enter the order of matrix
enter a(1,1) 9
enter a(1,2) 1
enter a(1,3) 1
enter b(1,1) 10
enter a(2,1) 2
enter a(2,2) 10
enter a(2,3) 3
enter b(2,1) 19
enter a(3,1) 3
enter a(3,2) 4
enter a(3,3) 11
enter b(3,1) 0
matrix a
9.000000
1.000000
1.000000
2.000000
10.000000
3.000000
3.000000
4.000000
11.000000
matrix b
10.000000
19.000000
0.000000
enter initial guess
enter x(1,1) 50
enter x(2,1) 100
enter x(3,1) 30
-17.100000
-50.000000
error norm=117.099998
2 itteration 8.566667
19.566666
9.854546
error norm=59.854546
3 itteration -2.157912
-2.769697
-9.451514
error norm=22.336363
4 itteration 2.469023
5.167037
1.595684
error norm=11.047198
5 itteration 0.359698
0.927490
-2.552292
error norm=4.239546
6 itteration 1.291645
2.593748
-0.435369
error norm=2.116924
7 itteration 0.871291
1.772282
-1.295448
error norm=0.860079
8 itteration 1.058130
2.114376
-0.882091
error norm=0.413357
9 itteration 0.974191
1.953001
-1.057445
error norm=0.175354
10 itteration 1.011605
2.022395
-0.975871
error norm=0.081574
11 itteration 0.994831
1.990440
-1.011309
error norm=0.035438
12 itteration 1.002319
2.004426
-0.995114
error norm=0.016195
13 itteration 0.998965
1.998070
-1.002242
error norm=0.007128
14 itteration 1.000463
2.000880
-0.999016
error norm=0.003226
15 itteration 0.999793
1.999612
-1.000446
error norm=0.001430
16 itteration 1.000093
2.000175
-0.999803
error norm=0.000644
17 itteration 0.999959
1.999922
-1.000089
error norm=0.000286
18 itteration 1.000018
2.000035
-0.999960
error norm=0.000128
19 itteration 0.999992
1.999985
-1.000018
error norm=0.000057
20 itteration 1.000004
2.000007
-0.999992
error norm=0.000026
21 itteration 0.999998
1.999997
-1.000003
error norm=0.000011
22 itteration 1.000001
2.000001
-0.999998
error norm=0.000005
1.677778
-0.913131
norm=98.322220
2 itteration 1.026150
1.968709
-0.995754
norm=0.290932
3 itteration 1.003005
1.998125
-1.000138
norm=0.029416
4 itteration 1.000224
1.999997
-1.000060
norm=0.002781
5 itteration 1.000007
2.000016
-1.000008
norm=0.000217
6 itteration 0.999999
2.000003
-1.000001
norm=0.000014
7 itteration 1.000000
2.000000
-1.000000
norm=0.000002
[CONCLUSION:
It is seen from number of iterations that gauss seidel method converges much faster than Jacobi ]
Que 2]
Matrix A fails the condition of being diagonally dominant hence solution for
x1 + 4x2 = 1 &
4x1 + x2 = 0.
given simultaneous equations shows NAN result for value of x1 & x2.
After interchanging the equations we get
4x1 + x2 = 0
x1 + 4x2 = 1
OUTPUT :
For initial guess x1=0,x2=0.
ITTERATIVE PROGRAM- JACOBI METHOD
enter the order of matrix
enter a(1,1) 4
enter a(1,2) 1
enter b(1,1) 0
enter a(2,1) 1
enter a(2,2) 4
enter b(2,1) 1
matrix a
4.000000
1.000000
1.000000
4.000000
matrix b
0.000000
1.000000
enter initial guess
enter x(1,1) 0
enter x(2,1) 0
0.250000
error norm=0.250000
2 itteration -0.062500
0.250000
error norm=0.062500
3 itteration -0.062500
0.265625
error norm=0.015625
4 itteration -0.066406
0.265625
error norm=0.003906
5 itteration -0.066406
0.266602
error norm=0.000977
6 itteration -0.066650
0.266602
error norm=0.000244
7 itteration -0.066650
0.266663
error norm=0.000061
8 itteration -0.066666
0.266663
error norm=0.000015
9 itteration -0.066666
0.266666
error norm=0.000004
0.250000
norm=0.250000
2 itteration -0.062500
0.265625
norm=0.062500
3 itteration -0.066406
0.266602
norm=0.003906
4 itteration -0.066650
0.266663
norm=0.000244
5 itteration -0.066666
0.266666
norm=0.000015
6 itteration -0.066667
0.266667
norm=0.000001
OUTPUT :
For initial guess x1=-0.07,x2=0.3.
ITTERAIVE PROGRAM- JACOBI METHOD
enter initial guess
enter x(1,1) -0.07
enter x(2,1) 0.3
0.267500
error norm=0.032500
2 itteration -0.066875
0.268750
error norm=0.008125
3 itteration -0.067188
0.266719
error norm=0.002031
4 itteration -0.066680
0.266797
error norm=0.000508
5 itteration -0.066699
0.266670
error norm=0.000127
6 itteration -0.066667
0.266675
error norm=0.000032
7 itteration -0.066669
0.266667
error norm=0.000008
0.250000
norm=0.070000
2 itteration -0.062500
0.265625
norm=0.062500
3 itteration -0.066406
0.266602
norm=0.003906
4 itteration -0.066650
0.266663
norm=0.000244
5 itteration -0.066666
0.266666
norm=0.000015
6 itteration -0.066667
0.266667
norm=0.000001
[CONCLUSION:
It is seen from number of iterations that gauss seidel method converges much faster than Jacobi ]
enter a(1,1) 4
enter a(1,2) -1
enter a(1,3) -1
enter a(1,4) 0
enter b(1,1) 5
enter a(2,1) -1
enter a(2,2) 4
enter a(2,3) 0
enter a(2,4) -1
enter b(2,1) -3
enter a(3,1) -1
enter a(3,2) 0
enter a(3,3) 4
enter a(3,4) -1
enter b(3,1) -7
enter a(4,1) 0
enter a(4,2) -1
enter a(4,3) -1
enter a(4,4) 4
enter b(4,1) 9
matrix a
4.000000
-1.000000
-1.000000
0.000000
-1.000000
4.000000
0.000000
-1.000000
-1.000000
0.000000
4.000000
-1.000000
0.000000
-1.000000
-1.000000
4.000000
matrix b
5.000000
-3.000000
-7.000000
9.000000
enter initial guess
enter x(1,1) 0
enter x(2,1) 0
enter x(3,1) 0
enter x(4,1) 0
Table of solution matrix and error norm
1 itteration 1.250000
-0.750000
-1.750000
2.250000
error norm=2.250000
2 itteration 0.625000
0.125000
-0.875000
1.625000
error norm=0.875000
3 itteration 1.062500
-0.187500
-1.187500
2.062500
error norm=0.437500
4 itteration 0.906250
0.031250
-0.968750
1.906250
error norm=0.218750
5 itteration 1.015625
-0.046875
-1.046875
2.015625
error norm=0.109375
6 itteration 0.976563
0.007813
-0.992188
1.976563
error norm=0.054688
7 itteration 1.003906
-0.011719
-1.011719
2.003906
error norm=0.027344
8 itteration 0.994141
0.001953
-0.998047
1.994141
error norm=0.013672
9 itteration 1.000977
-0.002930
-1.002930
2.000977
error norm=0.006836
10 itteration 0.998535
0.000488
-0.999512
1.998535
error norm=0.003418
11 itteration 1.000244
-0.000732
-1.000732
2.000244
error norm=0.001709
12 itteration 0.999634
0.000122
-0.999878
1.999634
error norm=0.000854
13 itteration 1.000061
-0.000183
-1.000183
2.000061
error norm=0.000427
14 itteration 0.999908
0.000031
-0.999969
1.999908
error norm=0.000214
15 itteration 1.000015
-0.000046
-1.000046
2.000015
error norm=0.000107
16 itteration 0.999977
0.000008
-0.999992
1.999977
error norm=0.000053
17 itteration 1.000004
-0.000011
-1.000011
2.000004
error norm=0.000027
18 itteration 0.999994
0.000002
-0.999998
1.999994
error norm=0.000013
19 itteration 1.000001
-0.000003
-1.000003
2.000001
error norm=0.000007
M matrix:
-0.9375
0.2500
0.2500
0.2500
-0.9375
0.2500
0.2500
0.9375
0.2500
0.2500
0.2500
-0.9375
II M II=1.4375
-0.437500
-1.437500
1.781250
norm=1.781250
2 itteration 0.781250
-0.109375
-1.109375
1.945313
norm=0.468750
3 itteration 0.945313
-0.027344
-1.027344
1.986328
norm=0.164063
4 itteration 0.986328
-0.006836
-1.006836
1.996582
norm=0.041016
5 itteration 0.996582
-0.001709
-1.001709
1.999146
norm=0.010254
6 itteration 0.999146
-0.000427
-1.000427
1.999786
norm=0.002563
7 itteration 0.999786
-0.000107
-1.000107
1.999947
norm=0.000641
8 itteration 0.999947
-0.000027
-1.000027
1.999987
norm=0.000160
9 itteration 0.999987
-0.000007
-1.000007
1.999997
norm=0.000040
10 itteration 0.999997
-0.000002
-1.000002
1.999999
norm=0.000010
11 itteration 0.999999
-0.000000
-1.000000
2.000000
norm=0.000003
[CONCLUSION:
It is seen from number of iterations that gauss seidel method converges much faster than Jacobi ]
M matrix
-0.9375
0.2500
0.2500
0.0313
-0.8750
0.0625
0.2500
0.0313
0.0625
-0.8750
0.2500
0.0234
0.0625
0.0625
-0.8125
II M II= 1.4375