Dept. of Mechanical Engg. Dronacharya College of Engg. Antc
Dept. of Mechanical Engg. Dronacharya College of Engg. Antc
ANTC
List of Experiment:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(x*x*x-4*x-9);
}
void bisect (float *x, float a, float b, int *itr)
{
*x=(a+b)/2;
++(*itr);
printf("Iteration no. %3dx= %7.5f \n", *itr, *x);
}
main( )
{
int itr =0, maxitr;
float x,a,b,aerr,x1;
clrscr( );
printf("Enter the values of a,b,allowed error,maximum iterations \n");
scanf("%f%f%f%d", &a,&b,&aerr,&maxitr);
bisect(&x,a,b,&itr);
do
{
if(f(a)*f(x)<0)
b=x;
else
a=x;
bisect(&x1,a,b,&itr);
if(fabs(x1-x)<aerr)
{
printf("after %d iterations, root =%6.4f \n",itr,x1);
getch( );
return 0;
}
x=x1;
}while(itr<maxitr);
printf("solution does not converage, iterations not sufficient");
getch( );
return 1;
}
OUTPUT (PROGRAM NO:-1)
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return x*log10(x)-1.2;
}
float df(float x)
{
return log10(x)+0.43429;
}
main()
{
int itr, maxitr;
float h,x0,aerr,x1;
clrscr( );
printf("Enter the values of x0,allowed error, maximum iterations \n");
scanf("%f%f%d", &x0,&aerr,&maxitr);
for (itr=1;itr<= maxitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf("iterataions no. %3d x=%9.6f \n",itr,x1);
if (fabs(h)<aerr)
{
printf("after %3d iterations, root x= %8.6f \n",itr,x1);
getch( );
return 0;
}
x0=x1;
}
printf("solution does not converge, iterations not sufficient");
getch( );
return 1;
}
OUTPUT (PROGRAM NO:-2)
0.00001
10
#include<stdio.h>
#define N 4
void main()
{
int i,j,k;
float a[N][N+1],x[N],t,s;
clrscr();
printf("Enter the elements of the augmented matrix row wise \n");
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
scanf("%f",&a[i][j]);
for(j=0;j<N-1;j++)
for(i=j+1;i<N;i++)
{
t=a[i][j]/a[j][j];
for(k=0;k<N+1;k++)
a[i][k]-=a[j][k]*t;
}
printf("The upper triangular matrix is:- \n");
for(i=0;i<N;i++)
{
for(j=0;j<N+1;j++)
printf("%8.4f",a[i][j]);
printf("\n");
}
for(i=N-1;i>=0;i--)
{
s=0;
for(j=i+1;j<N;j++)
s+=a[i][j]*x[j];
x[i]=(a[i][N]-s)/a[i][i];
}
printf("The solution is :- \n");
for(i=0;i<N;i++)
printf("x[%3d]=%7.4f \n",i+1,x[i]);
getch();
}
OUTPUT (PROGRAM NO:-3)
10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7
The solution is :-
x[ 1]= 5.0000
x[ 2]= 4.0000
x[ 3]=-7.0000
x[ 4]= 1.0000
PROGRAM NO:-4
To Solve the system of Linear Equations using Gauss Seidal Iterative Method.
#include<stdio.h>
#include<math.h>
#define N 3
main()
{
int i,j,itr,maxitr;
float a[N][N+1],x[N],aerr,maxerr,t,s,err;
clrscr();
for(i=0;i<N;i++)
x[i]=0;
printf("Enter the elements of the augmented matrix row wise \n");
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
scanf("%f",&a[i][j]);
printf("Enter the allowed errr, maximum iterations \n");
scanf("%f%d",&aerr,&maxitr);
printf("iteration x[1] x[2] x[3] \n");
for(itr=1;itr<=maxitr;itr++)
{ maxerr=0;
for(i=0;i<N;i++)
{
s=0;
for(j=0;j<N;j++)
if(j!=i)
s+=a[i][j]*x[j];
t=(a[i][N]-s)/a[i][i];
err=fabs(x[i]-t);
if(err>maxerr)
maxerr=err;
x[i]=t;
}
printf("%5d",itr);
for(i=0;i<N;i++)
printf("%9.4f",x[i]);
printf("\n");
if(maxerr<aerr)
{
printf("converges in%3d iterations \n",itr);
for(i=0;i<N;i++)
printf("x[%3d]=%7.4f \n",i+1,x[i]);
getch();
return 0;
}
}
printf("Solution does not converge, iterations not sufficient \n");
return 1;
}
20 1 -2 17
3 20 -1 -18
2 -3 20 25
0.0001 10
converges in 4 iterations
x[ 1]= 1.0000
x[ 2]=-1.0000
x[ 3]= 1.0000
PROGRAM NO:-5
To Find the Numerical solution of an Ordinary Differential Equation using the Euler’s
method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float df(float x, float y)
{
return x+y;
}
void main()
{
float x0,y0,h,x,x1,y1;
clrscr();
puts("Enter the values of x0,y0h,x");
scanf("%f%f%f%f",&x0,&y0,&h,&x);
x1=x0;
y1=y0;
while (1)
{
if(x1>x)
return;
y1+=h*df(x1,y1);
x1+=h;
printf("when x=%3.1f, y=%4.2f, \n", x1,y1);
getch();
}
}
OUTPUT (PROGRAM NO:-5)
0.1
To find the Numerical solution of an Ordinary Differential Equation using the Runge-Kutta
Method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x, float y)
{
return x+y*y;
}
void main()
{
float x0,y0,h,xn,x,y,k,k1,k2,k3,k4;
clrscr();
printf("Enter the values of x0,y0,h,xn");
scanf("%f%f%f%f",&x0,&y0,&h,&xn);
x=x0;
y=y0;
while(1)
{
if(x==xn)
break;
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y+k3);
k=(k1+(k2+k3)*2+k4)/6;
x+=h;
y+=k;
printf("when x=%8.4f, y=%8.4f \n",x,y);
}
getch();
}
OUTPUT (PROGRAM NO:-6)
0.0
0.1
0.2
Numerical solution of a system of two ordinary Differential Equation using the Numerical
Integration( Simpson 1/3 rule)
#include<stdio.h>
#include<conio.h>
#include<math.h>
float y(float x)
{
return 1/(1+x*x);
}
void main()
{
float x0,xn,h,s;
int i,n;
clrscr();
puts("Enter the values of x0,xn,no. of subintervals");
scanf("%f%f%d",&x0,&xn,&n);
h=(xn-x0)/n;
s=y(x0)+y(xn)+4*y(x0+h);
for(i=3;i<=n-1;i+=2)
s+=4*y(x0+i*h)+2*y(x0+(i-1)*h);
printf("Value of integral is %6.4f \n",(h/3)*s);
getch();
}
OUTPUT (PROGRAM NO:-7)
0 1 .2 .0001
x predicted, corrected
y f y f