Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 25

KIIT COLLEGE OF ENGINEERING

[Type the document titl


e]INDEX

S.
no

Topic

1.

Bisection Method

2.

Newton Raphson Method

3.

Gauss Elimination Method

4.

Gauss Jordan Method

5.

Gauss Seidal Method

6.

Trapezoidal Method

7.

Simpsons Rule

8.

Runge Kutta Method

9.

Eulars Method

10.

Lagranges Interpolation

Date

/* Program 1:-Bisection
AMIT KUMAR

Signatur
e

Method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>Figure 1
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("itration no. %3d X=%7.5f\n",*itr, *x);
}
main()
{
int itr=0,matrix;
clrscr();
float x,a,b,aree,x1;
printf("enter the value of a,b","allowed error, maximum itration\n");
scanf("%f%f%f%d", & a,& b,& aree,& matrix);
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)<aree)
{
printf("after %d iteration, root<169>= %6.4\n", itr, x1);

AMIT KUMAR

return 0;
}
x=x1;
}
while (itr<matrix);
printf("solution does not converge","itration not sufficient");
return 1;
getch();
}

AMIT KUMAR

/* Output*/
enter the value of a,b3
2
0.0001
20
itration no. 1 X=2.50000
after 1 iteration, root<169>= %6.4

AMIT KUMAR

/*Program 2:- Newton


Raphson Method*/
#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)+.43429;
}
main()
{
int itr,maxitr;
float h,x0,x1,aerr;
clrscr();
printf("enter 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("iteration no. %3d,x=%9.6f\n",itr,x1);
if(fabs(h)<aerr)
{
printf("after %d iterations root =%8.4f\n",itr,x1);
getch();
return 0;
}
x0=x1;
}
printf("iteration not sufficient solution does not converge\n");
return 1;
}

AMIT KUMAR

/*output*/
Enter x0,allowed error, maximum iterations
2.7 0.000001 18
Iteration no. 1, x= 2.740799
Iteration no. 2, x= 2.740646
Iteration no. 3, x= 2.740646
After 3 iterations root = 2.7406

AMIT KUMAR

/* Program 3:- Gauss


Elimination Method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define N 3
main()
{
float a[N][N+1],t;
int i,j,k;
clrscr();
printf("enter the elements of the augmented matrix rowise\n");
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
scanf("%f",&a[i][j]);
/*now calculating the values of x1, x2, x3,...,xN*/
for(j=0;j<N;j++)
for(i=0;i<N;i++)
if(i!=j)
{
t=a[i][j]/a[j][j];
for(k=0;k<N+1;k++)
a[i][k]-=a[j][k]*t;
}
/*now printing the diagonal matrix*/
printf("the diagonal matrix is:-\n");
for(i=0;i<N;i++)
{
for(j=0;j<N+1;j++)
printf("%9.4f",a[i][j]);
printf("\n");
}

AMIT KUMAR

/*Output*/
Enter the elements of augmented matrix row wise
10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7
The upper triangular matrix is:10.000 -7.0000 3.0000 5.0000 6.0000
0.0000 3.8000 0.8000 -1.0000 8.6000
-0.0000 -0.0000 2.4474 10.3158 -6.8158
0.0000 -0.0000 -0.0000 9.9247 9.9247
The solution is: X [1] = 5.0000
X [2] = 4.0000
X [3] = -7.0000
X [4] = 1.0000

AMIT KUMAR

/*Program 4:- Gauss Jordon


Method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define N 3
main()
{
float a[N][N+1],t;
int i,j,k;
clrscr();
printf("enter the elements of the augmented matrix rowise\n");
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
scanf("%f",&a[i][j]);
/*now calculating the values of x1, x2, x3,...,xN*/
for(j=0;j<N;j++)
for(i=0;i<N;i++)
if(i!=j)
{
t=a[i][j]/a[j][j];
for(k=0;k<N+1;k++)
a[i][k]-=a[j][k]*t;
}
/*now printing the diagonal matrix*/
printf("the diagonal matrix is:-\n");
for(i=0;i<N;i++)
{
for(j=0;j<N+1;j++)
printf("%9.4f",a[i][j]);
printf("\n");
}
/*now printing the result*/

AMIT KUMAR

printf("the solution is:-\n");


for(i=0;i<N;i++)
printf("x[%d]=%7.4f\n",i+1,a[i][N]/a[i][i]);
getch();
}

AMIT KUMAR

/*Output*/
Enter the elements of augmented matrix row wise
-10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7
The diagonal matrix is:10.0000 -0.0000 -0.0000 -0.0000 50.0000
0.0000
3.8000
-0.0000
0.0000
15.2000
-0.0000 0.0000
2.4474
0.0000
-17.1316
0.0000
-0.0000
0.0000
9.9247
9.9247
The solution is: X [1] = 5.0000
X [2] = 4.0000
X [3] = -7.0000
X [4] = 1.0000

AMIT KUMAR

/*Program 5:- Gauss-Seidal


Method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define N 3
main()
{
float a[N][N+1],x[N],aerr,maxerr,t,s,err;
int i,j,itr,maxitr;
clrscr();
printf("ENTER the elements of the augmented matrix rowwise\n");
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
scanf("%f",&a[i][j]);
printf("Enter the allowed error, 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("%d",itr);

AMIT KUMAR

for(i=0;i<N;i++)
printf("%9.4f",x[i]);
printf("\n");
if(maxerr<aerr)
{
printf("Convergesin %3d iterations\n",itr);
for(i=0;i<N;i++)
printf("x[%3d]=%7.4f\n",i+1,x[i]);
return 0;
}
}
printf("Solution does not converge iterations not suffcient\n");
return 1;
}

AMIT KUMAR

/*Output*/
Enter the elements of augmented matrix row wise
20 1 -2 17
3 20 -1 -18
2 -3 20 25
Enter the allowed error, maximum iteration
.0001 10
Iteration X(1)
X(2)
X(3)
1
0.8500 -1.02375 1.0109
2
1.0025 -0.9998 0.9998
3
1.0000 -1.0000 1.0000
4
1.0000 -1.0000 1.0000
Converges in 4 iterations
X [1] = 1.0000
X [2] = -1.0000
X [3] = 1.0000

AMIT KUMAR

/*Program 6:-Trapezoidal
Method*/
#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;
printf("enter the lower limit, upper limit, no. of intervals\n");
scanf("%f%f%d",&x0,&xn,&n);
h=(xn-x0)/n;
s=y(x0)+y(xn);
for(i=1; i<=n-1; i++)
s+= 2*y(x0+ i*h);
printf("the value of definite integrals is %7.5f\n",(h/2)*s);
getch();

AMIT KUMAR

/*output*/
enter the lower limit, upper limit, no. of intervals
122
the value of definite integrals is 0.32885
enter the lower limit, upper limit, no. of intervals
132
the value of definite integrals is 0.50000
enter the lower limit, upper limit, no. of intervals
159
the value of definite integrals is 0.59599

AMIT KUMAR

/*Program 7:-Simpsons
method*/
#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();
printf("enter the lower limit, upper limit, no. of intervals\n");
scanf("%f%f%d",&x0,&xn,&n);
h=(xn-x0)/n;
s=y(x0)+y(xn)+y(x0+h);
for(i=3;i<=n-1;i<2)
s+=4*y(x0+i*h)+2*y(x0+(i-1)*h);
printf("the value of definite integrals is %7.5f\n",(h/3)*s);
getch();
}

AMIT KUMAR

/*Output*/
enter the lower limit, upper limit, no. of intervals
2
3
2
the value of definite integrals is 0.07299

AMIT KUMAR

/*Program 8:-Runge kutta


Method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x,float y)
{return x+y*y;
}
main()
{float x0,y0,x,x1,y1,h;
clrscr();
printf("Enter the values of x0,y0,x,h \n");
scanf("%f%f%f%f",&x0,&y0,&x,&h);
x1=x0;y1=y0;
while(1)
{
if(x==x1) break;
y1+=h*f(x1,y1);
x1+=h;
printf("whenx=%6.4f,y=%6.4f\n",x1,y1);
getch();
}
}

AMIT KUMAR

/*output*/
Enter the values of x0,y0,x,h
0 1 3 .1
whenx=0.1000,y=1.1000
whenx=0.2000,y=1.2310
whenx=0.3000,y=1.4025
whenx=0.4000,y=1.6292
whenx=0.5000,y=1.9347
whenx=0.6000,y=2.3590
whenx=0.7000,y=2.9755
whenx=0.8000,y=3.9308
whenx=0.9000,y=5.5560
whenx=1.0000,y=8.7329
whenx=1.1000,y=16.4591
whenx=1.2000,y=43.6594
whenx=1.3000,y=234.3935
whenx=1.4000,y=5728.5537
whenx=1.5000,y=3287361.5000
whenx=1.6000,y=1080677892096.0000
whenx=1.7000,y=116786471838643992000000.0000
Floating point error: Overflow.
Abnormal program termination

AMIT KUMAR

/*Program 9:-Euler Method*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x, float y)
{
return x+y*y;
}
main()
{
float x0,y0,x1,y1,xn,h;
clrscr();
printf ("Enter the values of x0,y0,xn,& h\n");
scanf ("%f%f%f%f", &x0,&y0,&xn,&h);
x1=x0; y1=y0;
while (1)
{
if (x1==xn) break;
y1+=h*f(x1,y1);
x1+=h;
printf ("when x=%7.4f, y=%8.5f\n",x1,y1);
getch();
}
}

AMIT KUMAR

/*output*/
Enter the values of x0,y0,xn,& h
1234
when x= 5.0000, y=22.00000
when x= 9.0000, y=1978.00000
when x=13.0000, y=15651950.00000
when x=17.0000, y=979934168219648.00000
when x=21.0000, y=3841084041118427740000000000000.00000
Floating point error: Overflow.
Abnormal program termination

AMIT KUMAR

/*Program10:-Lagrange
Method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100
main()
{
float ax[MAX+1],ay[MAX+1],nr,dr,x,y=0;
int i,j,n;
printf("enter the values of n\n");
scanf("%d",&n);
printf("enter the values\n");
for(i=0;i<=n;i++)
scanf("%f%f",& ax[i],& ay[i]);
printf("enter the values of x for which y is wanted");
scanf("%f", & x);
for(i=0;i<=n;i++)
{
nr= dr= 1;
for(j=0;j<=n;j++)
if(j!=i)
{
nr*= x-ax[j];
dr*= ax[i]-ax[j];
}
y+= (nr/dr)*ay[i];
}
printf("when x= %7.4f,y=%8.5f\n",x,y);
getch();
}

AMIT KUMAR

/*Output*/

enter the values of n


2
enter the values
456
458
enter the values of x for which y is wanted
2
when x= 2.0000,y=-22.00000

AMIT KUMAR

You might also like