Nov 18, 2008

0

Guass Elimination Method

#include<stdio.h>
#include<math.h>
int main()
{
int n=3,i,j,k;
float a[10][10],c[10],x[10];
printf("Enter the no. of equations :\n");
scanf("%d", &n);
printf("Enter the right hand side of equation ");
for(i=0;i<=n-1;i++)
{
scanf("%f",&c[i]);
}

for(i=0;i<=n-1;i++)
{
printf("Enter the coefficient of equation %d.\n",i+1);
for(j=0;j<=n-1;j++)
{
scanf("%f",&a[i][j]);
}

}

for(k=0;k<=n-2;k++)
{
for(i=k+1;i<=n-1;i++)
{
for(j=k+1;j<=n-1;j++)
{
a[i][j]=a[i][j]-((a[i][k]/a[k][k])*a[k][j]);
}
c[i]=c[i]-((a[i][k]/a[k][k])*c[k]);
}

}

for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%f ",a[i][j]);
}
printf("\n");
}
printf("\n");

x[n-1]=c[n-1]/a[n-1][n-1];
printf("The solution is :\n");
printf("x[%d] = %f\n",n-1,x[n-1]);
for(k=0;k<=n-2;k++)
{
i=n-k-2;
for(j=i+1;j<=n-1;j++)
{
c[i]=c[i]-(a[i][j]*x[j]);
}
x[i]=c[i]/a[i][i];
printf("x[%d] = %f\n",i,x[i]);
}
return 0;
}

0

Gauss Seidal Method

#include
#include

int main()
{
int i,j,flag,n;
float a[10][10],y[10];
float sum,temp;

printf("Enter the no.s of equations.\n");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter the coefficent & value of equations %d.",i+1);
for(j=0;j<=n;j++)
{
scanf("%f",&a[i][j]);
}
}

for(i=0;i<=n-1;i++)
{
x[i]=0;
y[i]=9999;
}

do
{
for(i=0;i<=n-1;i++)
{
sum=0;
for(j=0;j<=n-1;j++)
{
if(j!=i)
{
sum=sum+a[i][j]*x[j];
}
}
temp=(a[i][n]-sum)/a[i][i];
x[i]=temp;
}

for(i=0;i<=n-1;i++)
{
if(y[i]==x[i]) flag=1;
else flag =0;
}

for(i=0;i<=n-1;i++)
{
y[i]=x[i];
printf("x[%d]=%f\t",i,x[i]);
}
printf("\n");
}while(flag==0);

return 0;
}

0

Bisection Method

#include
#include
#include

float f(float x)
{
float y;
y=x*x-25;
return y;
}

int main()
{
int i;
float x0,x1,x2,y0,y1,y2,e;
printf("Enter the value of x0,x1,error allowed\n");
scanf("%f\n %f\n %f",&x0, &x1,&e);
printf("x0=%f, x1=%f,e=%f\n",x0,x1,e);
y0=f(x0);
y1=f(x1);
i=0;
if((y0*y1)>0)
{
printf("Starting value unsuitable\n");
printf("x0=%f, x1=%f, y0=%f, y1=%f",x0,x1,y0,y1);
exit(1);
}
printf(" Iteration x0 x1 x2 f(x0) f(x1) f(x2)\n");
while((fabs((x1-x0)/x1))>e)
{
x2=(x0+x1)/2;
y2=f(x2);
i++;
if((y0*y2)>0)
{
x0=x2;
}
else
{
x1=x2;
}
printf(" %d %f %f %f %f %f %f\n",i,x0,x1,x2,y0,y1,y2);
}
printf("Solution is convergent to a root.\n");
printf("No. of iteration= %d & x=%f\n\n",i,x2);

return 0;

}

0

Newton Raphson Method

Q.This program is to solve y=x2-25.

#include<stdio.h>
#include<stdlib.h
#include<>

float x0,x1,f0,f1,f01,epsilon,delta,m;
int i,n;

float f(float a)
{
float b;
b=a*a-25;
return b;
}

float g(float c)
{
float d;
d=2*c;
return d;
}

float fun11()
{
printf("\nSlope too small. x0=%f, f0 =%f, f0'=%f0,i=%d\n",x0,f0,f01,i);
exit(1);
}

float fun13()
{
printf("\n%d %f %f %f %f %f",i+1,x0,x1,f0,f01,m);
printf("\n\nConvergent solution at %d iteration as x= %f\n",i,x1);
exit(1);
}
int main()
{
printf("Enter the values of x0,epsilon, delta, n\n");
scanf("%f%f%f%d",&x0,&epsilon,&delta,&n);
printf("Iteration x0 x1 f(x0) f'(x0) |(x1-x0)/x1|\n");
for(i=0;i<=n-1;i++)
{
f0=f(x0);
f01=g(x0);
if((fabs(f01))<=delta)
{
fun11();
}
x1=(x0-(f0/f01));
m=fabs((x1-x0)/x1);
if(m<=epsilon)
{
fun13();
}
printf("\n%d %f %f %f %f %f",i+1,x0,x1,f0,f01,m);
x0=x1;
}
printf("Slope too small x0= %f,f0=%f,f0'=%f,i=%d\n",x0,f0,f01,i);
return 0;
}

0

Regula Falsi on False Position Method

#include
#include
#include

float f(float m)
{
float n;
n=m*m-25;
return n;
}

int main()
{
int n,i;
float x0,x1,x2,e,f0,f1,f2;
printf("Enter the values of x0,x1,e,n\n");
scanf("%f %f %f %d",&x0,&x1,&e,&n);
f0=f(x0);
f1=f(x1);
printf("Iteration x0 x1 x2 f0 f1 f2\n");
for(i=1;i<=n;i++)
{
x2=(x0*f1-x1*f0)/(f1-f0);
f2=f(x2);
printf("%d %f %f %f %f %f %f\n",i,x0,x1,x2,f0,f1,f2);

if(fabs(f2)<=e)
{
printf("\n\nConvergent solution at %d iteration and x= %f\n",i,x2);
exit(1);
}
if((f0*f2)<0)
{
x1=x2;
f1=f2;
}
else
{
x0=x2;
f0=f2;
}
}
printf("\n\n Does not converge in %d iteration, x2= %f, f2= %f",i,x2,f2);
}

1

Method of Successive Approximation

Q.This program is for f(x)=x2+4x+4


#include<stdio.h>
#include<math.h>
#include<stdlib.h>

float g(float m)
{
float n;
n=-4/(m+4);
return n;
}

int main()
{
int n,i;
float x0,x1,e;
printf("Enter the value of x0,e,n");
scanf("%f%f%d",&x0,&e,&n);
x1=g(x0);
for(i=1;i<=n;i++)
{
x0=x1;
x1=g(x0);
printf(" i=%d x1=%f\n",i,x1);
if(fabs((x1-x0)/x1)<=e)
{
printf("\nConverge to root at iteration %d and x=%f\n",i,x1);
exit(1);
}
}
printf("Doesnot converge to a root\n");
return 0;
}

1

Newton's Forward Interpolation Formula

#include<stdio.h>

int main()
{
int i,j,n,m;
float x[20],y[20],d[20][20],a,h,u,b,z=1.0,k=0.0;

printf("Enter the no.s of entries i.e n:\n");
scanf("%d",&n);

printf("Enter the value of x.\n");
for(i=0;i<=n-1;i++)
{
scanf("%f",&x[i]);
}

printf("Enter the value of y.\n");
for(i=0;i<=n-1;i++)
{
scanf("%f",&y[i]);
}

for(i=0;i<=n-1;i++)
{
d[i][0]=y[i+1]-y[i];
}

for(j=1;j<=n-2;j++)
{
for(i=0;i<=n-j-2;i++)
{
d[i][j]=d[i+1][j-1]-d[i][j-1];
}
}

for(i=0;i<=n-1;i++)
{
printf("%f %f",x[i],y[i]);
for(j=0;j<=n-i-2;j++)
{
printf(" %f",d[i][j]);
}
printf("\n");
}

printf("Enter the value of x for which y is required\n");
scanf("%f",&a);
for(i=0;a>x[i];i++)
{
m=i;
}

h=x[1]-x[0];
u=(a-x[m])/h;

for(i=0;i<=n-m-2;i++)
{
z*=(u-i)/(i+1);
k+=(z*d[m][i]);
}
b=y[m]+k;
printf("\n The value of y at %f = %f\n",a,b);
return 0;
}

0

Newton's Backward Interpolation Formula

#include<stdio.h>

int main()
{
int i,j,n,m;
float x[20],y[20],d[20][20],a,h,u,b,z=1.0,k=0.0;

printf("Enter the no.s of entries i.e n:\n");
scanf("%d",&n);

printf("Enter the value of x.\n");
for(i=0;i<=n-1;i++)
{
scanf("%f",&x[i]);
}

printf("Enter the value of y.\n");
for(i=0;i<=n-1;i++)
{
scanf("%f",&y[i]);
}

for(i=1;i<=n;i++)
{
d[i][0]=y[i]-y[i-1];
}

for(j=1;j<=n-2;j++)
{
for(i=j+1;i<=n-1;i++)
{
d[i][j]=d[i][j-1]-d[i-1][j-1];
}
}

printf("%f %f\n",x[0],y[0]);
for(i=1;i<=n-1;i++)
{
printf("%f %f",x[i],y[i]);
for(j=0;j<=i-1;j++)
{
printf(" %f",d[i][j]);
}
printf("\n");
}

printf("Enter the value of x for which y is required\n");
scanf("%f",&a);
for(i=0;a>x[i];i++)
{
m=i;
}
m++;
h=x[1]-x[0];
u=(a-x[m])/h;
for(i=0;i<=m-1;i++)
{
z*=(u+i)/(i+1);
k+=(z*d[m][i]);
}
b=y[m]+k;
printf("\n The value of y at %f = %f\n",a,b);
return 0;
}

0

Trapezoidal Rule

Q.Integration of 1/(1+x^2) within the limit (1,6)


#include<stdio.h>

float f(float a)
{
float b;
b=(1/(1+(a*a)));
return b;
}

int main()
{
int i,m=1,n=6;
float y[7],z=0.0,w,h=1.0;
for(i=m;i<=n;i++)
{
y[i]=f(i);
printf("x=%d,y[%d]=%f\n",i,i,y[i]);
}

for(i=1;i<=n-1;i++)
{
z=z+y[i];
}
w=(((y[0]+y[n])+(2*z)));
printf("\nAns= %f\n",w);
return 0;
}

0

Simpson's 1/3 Rule

Q.Integration of 1/(1+x^2) within the limit (1,6) assuming h=1



#include<stdio.h>

float f(float a)
{
float b;
b=(1/(1+(a*a)));
return b;
}

int main()
{
int i,m=0,n=6;
float y[7],e=0.0,o=0.0,w,h=1.0;
for(i=m;i<=n;i++)
{
y[i]=f(i);
printf("x=%d,y[%d]=%f\n",i,i,y[i]);
}

for(i=1;i<=n-1;i=i+2)
{
o=o+y[i];
}

for(i=2;i<=n-1;i=i+2)
{
e=e+y[i];
}
printf("E=%f, O=%f",e,o);
w=((y[0]+y[n])+4*o+2*e)*(h/3);
printf("\nAns= %f\n",w);
return 0;
}

0

Simpson's 3/8 rule

Q.Integration of 1/(1+x^2) within the limit (1,6) assuming h=1


#include<stdio.h>

float f(float a)
{
float b;
b=(1/(1+(a*a)));
return b;
}

int main()
{
int i,m=0,n=6;
float y[7],t=0.0,o=0.0,w,h=1.0;
for(i=m;i<=n;i++)
{
y[i]=f(i);
printf("x=%d,y[%d]=%f\n",i,i,y[i]);
}

for(i=1;i<=n-1;i++)
{
if(i%3==0) continue;
else o=o+y[i];
}

for(i=3;i<=n-1;i=i+3)
{
t=t+y[i];
}
printf("t=%f, O=%f",t,o);
w=((y[0]+y[n])+3*o+2*t)*((3*h)/8);
printf("\nAns= %f\n",w);
return 0;
}

0

Forward Difference Formula

#include

int main()
{
int i,j,n,m;
float x[20],e,y[20],d[20][20],a,h,b1,b2,b3,z,k=0.0;
printf("Enter the no.s of entries i.e n:\n");
scanf("%d",&n);
printf("Enter the value of x.\n");
for(i=0;i<=n-1;i++)
{
scanf("%f",&x[i]);
}

printf("Enter the value of y.\n");
for(i=0;i<=n-1;i++)
{
scanf("%f",&y[i]);
}

for(i=0;i<=n-1;i++)
{
d[i][0]=y[i+1]-y[i];
}

for(j=1;j<=n-2;j++)
{
for(i=0;i<=n-j;i++)
{
d[i][j]=d[i+1][j-1]-d[i][j-1];
}
}

for(i=0;i<=n-1;i++)
{
printf("%f %f",x[i],y[i]);
for(j=0;j {
printf(" %3.3f",d[i][j]);
}
printf("\n");
}

printf("Enter the value of x for which y is required\n");
scanf("%f",&a);
for(i=0;a>=x[i];i++)
{
m=i;
}

h=x[1]-x[0];

for(i=0;i< n-m-1;i++)
{
e=i+1;
z=(1/e);
if(i%2!=0) z=-z;
k+=(z*d[m][i]);
}
b1=k/h;
printf(" dy/dx at x=%3.3f =%3.3f\n",a,b1);
k=0;z=0;
for(i=1;i<=n-m;i++)
{
if(i==1){ z=d[m][i];}
if(i==2){ z=-d[m][i];}
if(i==3){ z=0.9166*d[m][i];}
if(i==4){ z=-0.8333*d[m][i];}
k+=z;
}
b2=k/(h*h);
printf("d2y/dx2 at x=%3.3f =%3.3f\n",a,b2);
k=0;z=0;
for(i=2;i<=n-m;i++)
{
if(i==2){z=d[m][i];}
if(i==3){z=-1.5*d[m][i];}
k+=z;
}
b3=k/(h*h*h);
printf("d3y/dx3 at x=%3.3f =%3.3f\n",a,b3);
return 0;
}

Nov 17, 2008

0

Backward Difference Formula

#include<stdio.h>

int main()
{
int i,j,n,m;
float x[20],y[5],e,d[20][20],a,h,b,z,k=0.0,b1,b2,b3;

printf("Enter the no.s of entries i.e n:\n");
scanf("%d",&n);

printf("Enter the value of x.\n");
for(i=0;i<=n-1;i++)
{
scanf("%f",&x[i]);
}

printf("Enter the value of y.\n");
for(i=0;i<=n-1;i++)
{
scanf("%f",&y[i]);
}

for(i=1;i<=n;i++)
{
d[i][0]=y[i]-y[i-1];
}

for(j=1;j<=n;j++)
{
for(i=j+1;i<=n-1;i++)
{
d[i][j]=d[i][j-1]-d[i-1][j-1];
}
}



printf("%f %f\n",x[0],y[0]);
for(i=1;i<=n-1;i++)
{
printf("%f %f",x[i],y[i]);
for(j=0;j {
printf(" %f",d[i][j]);
}
printf("\n");
}

printf("Enter the value of x for which y is required\n");
scanf("%f",&a);
for(i=0;a>x[i];i++)
{
m=i;
}
m++;
h=x[1]-x[0];

for(i=0;i<=m-1;i++)
{
e=i+1;
z=(1/e);
if(i%2!=0) z=-z;
k+=(z*d[m][i]);
}
b1=k/h;
printf(" dy/dx at x=%3.3f =%3.3f\n",a,b1);
k=0;z=0;
for(i=1;i<=m-1;i++)
{
if(i==1){ z=d[m][i];}
if(i==2){ z=-d[m][i];}
if(i==3){ z=0.9166*d[m][i];}
if(i==4){ z=-0.8333*d[m][i];}
k+=z;
}
b2=k/(h*h);
printf("d2y/dx2 at x=%3.3f =%3.3f\n",a,b2);
k=0;z=0;
for(i=2;i<=m-1;i++)
{
if(i==2){z=d[m][i];}
if(i==3){z=-1.5*d[m][i];}
k+=z;
}
b3=k/(h*h*h);
printf("d3y/dx3 at x=%3.3f =%3.3f\n",a,b3);
return 0;
}

0

Cramers' Rule

#include

int i,j;
char display(i)
{
if(j==0) return 'x';
if(j==1) return 'y';
if(j==2) return 'z';
return 0;
}

int main()
{
float a[3][4],d,x,y,z;
system("clear");

for(i=0;i<3;i++)
{
printf("\n\tEnter the values of equation %d\n",i+1);
for(j=0;j<4;j++)
{
if(j!=3) printf("\tEnter the coefficent of %c = ",display(j));
else printf("\tEnter the value of equat = ");
scanf("%f",&a[i][j]);
}
}

d=((a[0][0]*(a[1][1]*a[2][2]-a[1][2]*a[2][1]))-(a[0][1]*(a[1][0]*a[2][2]-a[2][0]*a[1][2]))+(a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0])));
x=((((a[0][3]*(a[1][1]*a[2][2]-a[1][2]*a[2][1]))-(a[0][1]*(a[1][3]*a[2][2]-a[2][3]*a[1][2]))+(a[0][2]*(a[1][3]*a[2][1]-a[1][1]*a[2][3]))))/d);
y=((((a[0][0]*(a[1][3]*a[2][2]-a[1][2]*a[2][3]))-(a[0][3]*(a[1][0]*a[2][2]-a[2][0]*a[1][2]))+(a[0][2]*(a[1][0]*a[2][3]-a[1][3]*a[2][0]))))/d);
z=((((a[0][0]*(a[1][1]*a[2][3]-a[1][3]*a[2][1]))-(a[0][1]*(a[1][0]*a[2][3]-a[2][0]*a[1][3]))+(a[0][3]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]))))/d);
printf("\tThe solution is\n\tDelta = %f,\n\tX= %f,\n\tY= %f,\n\tZ= %f\n",d,x,y,z);

return 0;
}

Nov 8, 2008

0

Method of False Position.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

float f(float a)
{

float b
b=a*a-25;
return b;
}

float abso(float c)
{
if (c<0) return -c;
else return c;
}

int main()
{
float x0,x1,x2,f0,f1,f2,e,m;
int i,n,a=0;

printf("Enter the value of x0,x1,e and n\n");
scanf("%f%f%f%d",&x0,&x1,&e,&n);
f0=f(x0);
f1=f(x1);
printf("Iteration x0,x1,x2,f0,f1,f2\n");
for(i=0;i<=n;i++)
{
x2=((x0*f1-x1*f0)/(f1-f0));
f2=f(x2);
printf("%d %f %f %f %f %f %f\n",i,x0,x1,x2,f0,f1,f2);
if(abso(f2)<=e)
{
a=1;
printf("Convergent in %dth iteration at root %f\n", i,x2);
if (a==1)
{
exit(1);
}
}
if((f2*f0)<0)
{
x1=x2;
f1=f2;
}
else
{
x0=x2;
f0=f2;
}
}
printf("Does not converge in %d itetations.\n",i-1);
printf("x2=%f, f2=%f",x2,f2);
return 0;
}