Numerical Methods File
Numerical Methods File
S.
no
Topic
1.
Bisection Method
2.
3.
4.
5.
6.
Trapezoidal Method
7.
Simpsons Rule
8.
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
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
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
AMIT KUMAR
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
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
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
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*/
AMIT KUMAR