Sie sind auf Seite 1von 7

Write a program to implement Gauss Elimination method:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a[3][3],x[5],d[3];
int i,j;
clrscr();
printf("enter the matrix");
for(i=0;i<=2;i++)
{ for(j=0;j<=2;j++)
scanf("%f",&a[i][j]);
}
printf("enter the matrix d");
for(i=0;i<=2;i++)
scanf("%f",&d[i]);
d[1]=d[1]-(a[1][0]/a[0][0])*d[0];
d[2]=d[2]-(a[2][0]/a[0][0])*d[0];
for(i=2;i>=0;i--)
{ a[1][i]=a[1][i]-(a[1][0]/a[0][0])*a[0][i];
a[2][i]=a[2][i]-(a[2][0]/a[0][0])*a[0][i];
}
d[2]=d[2]-(a[2][1]/a[1][1])*d[1];
for(i=2;i>=1;i--)
a[2][i]=a[2][i]-(a[2][1]/a[1][1])*a[1][i];
printf("the resulting matrix is=\n");
for(i=0;i<=2;i++)
{ for(j=0;j<=2;j++)
printf("%f\t",a[i][j]);
printf("%f",d[i]);
printf("\n");
}
x[3]=0;
x[4]=0;
for(i=2;i>=0;i--)
{
x[i]=(d[i]-(a[i][i+1]*x[i+1])-(a[i][i+2]*x[i+2]))/a[i][i];
}
for(i=0;i<=2;i++)
{printf("value of x[%d]=%f\n",i,x[i]);}
getch();
}

OUTPUT:
enter the matrix1
4
-1
1
1
-6
3
-1
-1

enter the matrix d-5


-12
4
the resulting matrix is=
1.000000
4.000000
0.000000
-3.000000
0.000000
0.000000
value of x[0]=1.647887
value of x[1]=-1.140845
value of x[2]=2.084507

-1.000000
-5.000000
23.666666

-5.000000
-7.000000
49.333332

Write a program to implement Gauss Seidal Iteration method.


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{float a[3][3],d[3],x,y,z,u,v,w,err,m;
int i,j;
clrscr();
printf("enter the matrix");
for(i=0;i<=2;i++)
{for(j=0;j<=2;j++)
scanf("%f",&a[i][j]);
}
printf("enter the matrix d");
for(i=0;i<=2;i++)
{scanf("%f",&d[i]);}
printf("the error is");
scanf("%f",&err );
x=0;y=0;z=0;
for(i=0;i<10;i++)
{
u=(d[0]-a[0][1]*y-a[0][2]*z)/a[0][0];
v=(d[1]-a[1][0]*u-a[1][2]*z)/a[1][1];
w=(d[2]-a[2][0]*u-a[2][1]*v)/a[2][2];
printf("\nvalue of x on iteration=%f",u);
printf("\nvalue of y on iteration=%f",v);
printf("\nvalue of z on iteration=%f",w);
m=x;
x=u;
y=v;
z=w;
if(fabs(m-x)<err)
{break;}
}
printf("\nvalue of x=%f",u);
printf("\nvalue of y=%f",v);
printf("\nvalue of z is=%f",w);
getch();
}

Output:
enter the matrix83
11
-4
7
52
13
3
8
29
enter the matrix d95
104
71
the error is.0001
value of x on iteration=1.144578
value of y on iteration=1.845922
value of z on iteration=1.820651
value of x on iteration=0.987680
value of y on iteration=1.411880
value of z on iteration=1.956618
value of x on iteration=1.051757
value of y on iteration=1.369263
value of z on iteration=1.961746
value of x on iteration=1.057652
value of y on iteration=1.367187
value of z on iteration=1.961708
value of x on iteration=1.057925
value of y on iteration=1.367160
value of z on iteration=1.961688
value of x on iteration=1.057928
value of y on iteration=1.367165
value of z on iteration=1.961686
value of x=1.057928
value of y=1.367165
value of z is=1.961686

Write a program to implement Jacobi Iteration method.


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{float a[3][3],d[3],x,y,z,u,v,w,err,m;
int i,j;

printf("enter the matrix");


for(i=0;i<=2;i++)
{for(j=0;j<=2;j++)
scanf("%f",&a[i][j]);
}
printf("enter the matrix d");
for(i=0;i<=2;i++)
{scanf("%f",&d[i]);}
printf("the error is");
scanf("%f",&err );
x=0;y=0;z=0;
for(i=0;i<10;i++)
{
u=(d[0]-a[0][1]*y-a[0][2]*z)/a[0][0];
v=(d[1]-a[1][0]*x-a[1][2]*z)/a[1][1];
w=(d[2]-a[2][0]*x-a[2][1]*y)/a[2][2];
printf("\nvalue of x on iteration=%f",u);
printf("\nvalue of y on iteration=%f",v);
printf("\nvalue of z on iteration=%f",w);
m=x;
x=u;
y=v;
z=w;
if(fabs(m-x)<err)
{break;}
}
printf("\nvalue of x=%f",u);
printf("\nvalue of y=%f",v);
printf("\nvalue of z is=%f",w);
getch();
}
OUTPUT:
enter the matrix20
1
-2
3
20
-1
2
-3
20
enter the matrix d17
-18
25
the error is.0001
value of x on iteration=0.850000
value of y on iteration=-0.900000
value of z on iteration=1.250000
value of x on iteration=1.020000
value of y on iteration=-0.965000
value of z on iteration=1.030000
value of x on iteration=1.001250
value of y on iteration=-1.001500
value of z on iteration=1.003250
value of x on iteration=1.000400
value of y on iteration=-1.000025
value of z on iteration=0.999650
value of x on iteration=0.999966
value of y on iteration=-1.000077

value
value
value
value
value
value
value

of
of
of
of
of
of
of

z on iteration=0.999956
x on iteration=1.000000
y on iteration=-0.999997
z on iteration=0.999992
x=1.000000
y=-0.999997
z is=0.999992

Write a program to implement Power method.


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{float a[3][3],d[3],x[3],max,z[3],c[3],err;
int i,j,k;
clrscr();
printf("enter the matrix");
for(i=0;i<=2;i++)
{for(j=0;j<=2;j++)
scanf("%f",&a[i][j]);
}
printf("enter the matrix d");
for(i=0;i<=2;i++)
{scanf("%f",&d[i]);}
printf("the error is");
scanf("%f",&err);
for(i=0;i<10;i++)
{
for(j=0;j<=2;j++)
{x[j]=a[j][0]*d[0]+a[j][1]*d[1]+a[j][2]*d[2];
}
max=fabs(x[0]);
for(k=1;k<=2;k++)
{if(fabs(x[k])>max)
{max=fabs(x[k]); } }
printf("\neigen value=%f",max);
for(j=0;j<=2;j++)
{
x[j]=x[j]/max;
c[j]=x[j];
printf("\nx[%d]=%f",j,c[j]);
z[j]=d[j];
d[j]=c[j]; }
if(fabs(z[j]-d[j])<=err)
{break;}
}
printf("\nthe resulting eigen value is=%f",max);

printf("\nthe resulting eigen vector ");


for(i=0;i<=2;i++)
{printf("%f\n",d[i]);
}
getch();
}

OUTPUT:
enter the matrix2
-1
0
-1
2
-1
0
-1
2
enter the matrix d1
0
0
the error is.0001
eigen value=2.000000
x[0]=1.000000
x[1]=-0.500000
x[2]=0.000000
eigen value=2.500000
x[0]=1.000000
x[1]=-0.800000
x[2]=0.200000
eigen value=2.800000
x[0]=1.000000
x[1]=-1.000000
x[2]=0.428571
eigen value=3.428571
x[0]=0.875000
x[1]=-1.000000
x[2]=0.541667
eigen value=3.416667
x[0]=0.804878
x[1]=-1.000000
x[2]=0.609756
eigen value=3.414634
x[0]=0.764286
x[1]=-1.000000
x[2]=0.650000
eigen value=3.414286
x[0]=0.740586
x[1]=-1.000000
x[2]=0.673640
eigen value=3.414226
x[0]=0.726716
x[1]=-1.000000
x[2]=0.687500
eigen value=3.414216
x[0]=0.718593
x[1]=-1.000000

x[2]=0.695621
eigen value=3.414214
x[0]=0.713835
x[1]=-1.000000
x[2]=0.700378
the resulting eigen value is=3.414214
the resulting eigen vector 0.713835
-1.000000
0.700378

Das könnte Ihnen auch gefallen