Académique Documents
Professionnel Documents
Culture Documents
PROGRAMMING
FOR
NUMERICAL LAB
@
[P]-[A__B]-[]
@ [P]-[A__B]-[]
Page 1
CONTENTS
1. BISECTION METHOD
2. SECANT METHOD
3. EULER METHOD
4. TRAPEZOIDAL METHOD
5. REGULA FALSI METHOD
6. FIXED POINT METHOD
7. LAGRANGES INTERPOLATION
8. RUNGE-KUTTA METHOD
9. SIMPSONS 1/3 RULE
10. SIMPSONS 3/8 RULE
11. GAUSS-SEIDEL METHOD
12. NEWTON-RAPHSON METHOD
13. NEWTONS DIVIDED DIFFERENCE METHOD
14. HUENS METHOD
@ [P]-[A__B]-[]
Page 2
BISECTION METHOD
AIM: To find solution of x2-4x+3 by BISECTION METHOD.
ALGORITHM:
1. Start
2. Define the function f(x)
3. Read initial values say x1 & x2.
4. if (((f(x1)*f(x2))<0)
Set x=x1 & y=x2
Else
The selected interval is wrong.
5. Repeat the following steps.
6. z=(x+y)/2
7. if((f(x)*f(z)<0)
Set y=z
Else
Set x=z
8. while((f(x)*f(z)==0))
9. Print the root is z
10. Stop
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<math.h>
float fun(float k)
{
float p;
p=(pow(k,2)-4*k+3);
return p;
}
void fun1(float a,float b)
{
if(fun(a)*fun(b)>0)
{
cout<<"\n\n\t\t WRONG INTERVAL\n\n\t\t";
}
if(fun(a)*fun(b)<0)
{
float s;
s=(a+b)/2;
if(fun(a)*fun(s)<0)
{
b=s;
@ [P]-[A__B]-[]
Page 3
cout<<"\n\t\t "<<s;
fun1(a,b);
}
else if(fun(a)*fun(s)==0)
{
cout<<"\n\n\t\t soln is\t"<<s;
}
else
{
a=s;
fun1(a,b);
}
}
}
void main()
{
clrscr();
float a,b,c,d;
cout<<"\n\n\t\t enter the limits for first soln..\t";
cin>>a>>b;
cout<<"\n\n\t\t enter the limits for second soln..\t";
cin>>c>>d;
fun1(a,b);
getch();
fun1(c,d);
getch();
}
SAMPLE OUTPUT:
enter the limits for first soln... -13000 5000
enter the limits for second soln...
2.98 3.5
WRONG INTERVAL
Iteration 1. 3.24
Iteration 2. 3.11
Iteration 3. 3.045
Iteration 4. 3.0125
Iteration 5. 3.004375
Iteration 6. 3.000313
Iteration 7. 3.000059
Iteration 8. 3.000027
Iteration 9. 3.000011
Iteration 10. 3.000003
Iteration 11. 3.000001
SOLUTION IS
@ [P]-[A__B]-[]
Page 4
SECANT METHOD
AIM:To find solution of equation x2-5x+4 by SECANT METHOD.
ALGORITHM:
1. Start
2. Function f(x) is declared.
3. Range in which the root lies is determined.
4. Then consecutive values are calculated:
Xn+1=Xn-(f(Xn)(Xn+1-Xn)/(f(Xn+1)-f(Xn))
Till((Xn+1-Xn)>0.000001))
5. Print the root.
8. Stop
PROGRAM:
#include<stdio.h>
main()
{
float a,b,c,f,g,s,h;
printf("enter two value :");
scanf("%f %f",&a,&s);
f=(a*a)-(5*a)+4;
g=s*s-5*s+4;
b=a-((f*(a-s))/(f-g));
printf("\nITERATION
SOLUTION");
printf(" \n\n 1 \t\t
%f \n",b);
float e;
e=0.1;
int i=1;
while(e>0.001)
{
f=(a*a)-(5*a)+4;
h=(b*b)-(5*b)+4;
c=b-((h*(b-a))/(h-f));
if(h==f)
{
printf("\t 1 \t\t %f",b);
}
else
printf("\n %d \t\t
if(c>b)
e=(c-b);
@ [P]-[A__B]-[]
%f \n",i+1,c);
Page 5
else
e=(b-c);
a=b;
b=c;
i++;
}
printf("\n Solution after %d iterations is %f \n",i,c);
}
SAMPLE OUTPUT:
Enter two values: 5 6
ITERATION
SOLUTION
1
2
3
4
5
4.333333
4.076923
4.007519
4.000187
4.000000
EULER METHOD
AIM: To find value of y at x=0.3 when given differential equation is y= (x+y).
ALGORITHM:
1.INPUT:
Initial values x0 ,y0, stepsize h , number of steps N
2.OUTPUT:
Approximation y(n+1) to the solution y(x(n+1)) at
x(n+1) = x0+ (n+1)h where n=0,1,2...N-1
3.For n=0,1,2...N-1 do
4.y=y+h*(x+y)
5.x=x+h
6.OUTPUT x(n+1) , y(n+1)
7.End
8.stop
PROGRAM:
@ [P]-[A__B]-[]
Page 6
#include<stdio.h>
float f(float x,float y)
{
float z;
z=x+y;
return z;
}
int main()
{
float h,x,y;
int i,n;
printf("enter values of x , y , h,n \n ");
scanf(" %f %f %f %d", &x ,&y ,&h ,&n);
for (i=0;i<n;i++)
{
printf (" x %d
printf (" y %d
y=y+h*(x+y);
x=x+h;
}
% f \n ",i,x);
% f \n ",i,y);
SAMPLE OUTPUT:
Enter
values of x , y , h ,n :
x0
0.000000
y0
1.000000
x1
0.050000
y1
1.050000
x2
0.100000
y2
1.105000
x3
0.150000
y3
1.165250
x4
0.200000
y4
1.231012
@ [P]-[A__B]-[]
0.05
Page 7
x5
0.250000
y5
1.302563
x6
0.300000
y6
1.380191
TRAPEZOIDAL METHOD
AIM:
To find the solution of integration of x3 from 0 to 1.
ALGORITHM:
1.Input: enter limits a,b,n=1
2.compute:
3.h=(b-a)/n
4.while(x<b)
5.k=f(x)
6.m=m+2k
7.x=x+h
8.end
9.y=h(f(a)+f(b)+m)/2
10.n++
11.end
PROGRAM:
#include<stdio.h>
main()
{
int n;
float a,m,b,e;
float f,g,k,h,x,z,y;
printf("\n Enter the limits");
scanf("%f %f",&a,&b);
f=a*a*a ; g=b*b*b;
n=1;
h=(b-a)/n;
z=h*(f+g)/2;
e=0.1; n=2;
while(e>0.0001)
@ [P]-[A__B]-[]
Page 8
{
h=(b-a)/n;
m=0; x=a+h;
while(x<b)
{
k=x*x*x;
m=m+(2*k);
x=x+h;
}
y=h*(f+m+g)/2;
printf("\n %f ",y);
if(z>y)
e=z-y;
else
e=y-z;
z=y;
n++;
}
printf(\n Solution of integration is : %f,y);
}
SAMPLE OUTPUT:
1
Solution
0.312500
0.277778
0.265625
0.260000
0.256944
0.255102
0.253906
0.253086
0.252500
0.252066
0.335069
0.251479
0.251276
0.251111
0.250977
0.250865
0.250772
Page 9
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
printf("REGULA FALSI METHOD\n");
float a,b,c,f,p,q,r;
printf("enter two values");
scanf("%f %f",&a,&b);
p=a*a-4*a+3;
q=b*b-4*b+3;
f=p*q;
r=0.1;
int i=0;
printf("\n ITERATION
SOLUTION");
if(f>0)
printf("invalid input");
else if(f==0 && p==0)
@ [P]-[A__B]-[]
Page 10
printf("solution is %f",a);
else if(f==0 && q==0)
printf("\n solution is %f",b);
else
{
while(r>0.000001 || r<(-0.000001))
{
p=(a*a-4*a+3);
q=(b*b-4*b+3);
c=((a*q)-(b*p))/(q-p);
r=(c*c-4*c+3);
if((r*p)<0)
{
printf("\n%d\t\t%f",i,c);
b=c;
getch();
}
else
{
printf("\n %d \t\t %f",i,c);
a=c;
getch();
}
i++;
}
}
printf("\n Solution after %d iterations is %f",i,c);
}
SAMPLE OUTPUT:
Enter two points
ITERATION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@ [P]-[A__B]-[]
-2
2.8
SOLUTION
2.285714
2.528302
2.320652
2.076810
1.823419
1.591454
1.402483
1.262630
1.166314
1.103222
1.063239
1.038429
1.023236
1.014007
1.008428
1.005065
Page 11
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1.003042
1.001826
1.001096
1.000658
1.000395
1.000237
1.000142
1.000085
1.000051
1.000031
1.000018
1.000011
1.000007
1.000004
1.000002
1.000001
1.000001
1.000000
Page 12
if(g>z)
e=g-z;
else
e=z-g;
z=g;
i++;
}
printf("\n Solution of equation is %f",g);
}
SAMPLE OUTPUT:
Enter the first value : 0.5
1 iteration 0.609756
2 iteration 0.728968
3 iteration 0.653000
4 iteration 0.701061
5 iteration 0.670472
6 iteration 0.689878
7 iteration 0.677538
8 iteration 0.685374
9 iteration 0.680394
10 iteration 0.683557
11 iteration 0.681547
12 iteration 0.682824
13 iteration 0.682013
14 iteration 0.682528
15 iteration 0.682201
16 iteration 0.682409
17 iteration 0.682276
18 iteration 0.682360
Solution of equation is 0.682360
LAGRANGES INTERPOLATION
AIMTo determine value at a given point using Lagranges interpoation when values are given as f(0)=1,
f(1)=3, f(3) = 13,f(8)=123
ALGORITHM1. Read the number of points(n),given points and its functional values.
2. Declare d,p,sum
3. For i=0 to n-1
@ [P]-[A__B]-[]
Page 13
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
p=1
for j=0 to n-1
if (i!=j)
d= (a-x[j])/(x[i]-x[j])
p=p*d
End of if statement
End of inner for loop
P1=p*f[i]
Sum=sum+p1
End of for loop
Print the result
PROGRAM:
#include<iostream.h>
int main()
{
int n;
float a,x[10],f[10];
cout<<"Enter given point";
cin>>a;
cout<<"Enter the no of points";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter values of x"<<k;
cin>>x[k];
cout<<"Enter function value";
cin>>f[k];
}
float sum=0;
for(int i=0;i<n;i++)
{
float p=1;
for(int j=0;j<n;j++)
{
if(j!=i)
{
float d=((a-x[j])/(x[i]-x[j]));
p=p*d;
}
}
float p1=p*f[i];
sum=sum+p1;
}
cout<<"Value at given point "<<sum;
return 0;
@ [P]-[A__B]-[]
Page 14
SAMPLE OUTPUT:
Enter given point : 5
Enter number of points : 4
Enter value of x0 : 0
Enter functional value : 1
Enter value of x1 : 1
Enter functional value : 3
Enter value of x2 : 3
Enter functional value : 13
Enter value of x3 : 8
Enter functional value : 123
Value at given point : 38.14286
RUNGE-KUTTA METHOD
AIM- To solve (dy/dx=(y2-x2)/(y2+x2)) with y(0) =1 at 0.2 and 0.4
Page 15
int main()
{
float x[10],y[10],h,k1,k2,k3,k4;
int n,i;
printf("enter x.,y.,h");
scanf("%f%f%f",&x[0],&y[0],&h);
printf("Enter value of n");
scanf("%d",&n);
printf(" \n x0 and y0 : %f %f ",x[0],y[0]);
for( i=0;i<n;i++)
{
k1=h*(f(x[i],y[i]));
k2=h*(f(x[i]+(h/2),y[i]+(k1/2)));
k3=h*(f(x[i]+h,y[i]+(k2/2)));
k4=h*(f(x[i]+h,y[i]+k3));
x[i+1]=x[i]+h;
y[i+1]=y[i]+((k1+(2*k2)+(2*k3)+k4)/6);
printf("x %d and y%d :%f %f\n",i+1,i+1,x[i+1],y[i+1]);
}
}
SAMPLE OUTPUT:
Enter x.,y., h : 0 1 0.2
Enter value of n : 2
X0 and y0 : 0.000000 1.000000
X1 and y1 : 0.200000 1.192788
X2 and y2 : 0.400000 1.3669499
PART II
AIM:
dy/dx= (2xy/(x^2+xe^x))
x0=1, y0=0 at 1.2 and 1.4
PROGRAM 2 :
#include<stdio.h>
#include<math.h>
float f(float x,float y)
{
return(((y*y)-(x*x))/((y*y)+(x*x)));
}
int main()
{
float x[10],y[10],h,k1,k2,k3,k4;
int n,i;
@ [P]-[A__B]-[]
Page 16
printf("enter x.,y.,h");
scanf("%f%f%f",&x[0],&y[0],&h);
printf("Enter value of n");
scanf("%d",&n);
printf("x0 and y0 :%f %f ",x[0],y[0]);
for( i=0;i<n;i++)
{
k1=h*(f(x[i],y[i]));
k2=h*(f(x[i]+(h/2),y[i]+(k1/2)));
k3=h*(f(x[i]+h,y[i]+(k2/2)));
k4=h*(f(x[i]+h,y[i]+k3));
x[i+1]=x[i]+h;
y[i+1]=y[i]+((k1+(2*k2)+(2*k3)+k4)/6);
printf("x%d and y%d :%f %f\n",i+1,i+1,x[i+1],y[i+1]);
}
}
SAMPLE OUTPUT:
Enter value of x,y,h : 1 0 0.2
Enter value of n : 2
X0 and y0: 0.000000 0.0000000
X1 and y1: 1.200000 0.136281
X2 and y2: 1.400000 0.263182
Page 17
#include<stdio.h>
main()
{
int n,i;
i=1;
float a,y,m,s,b,h,f,g,z,x,k,e;
printf("\n Input the limits");
scanf("%f %f",&a,&b);
f=a*a*a; g=b*b*b;
n=2;
h=(b-a)/n;
m=h*h*h;
z=h*(f+g+(4*m))/3;
n=n+2;
e=0.1;
printf("\n ITERATION SOLUTION");
while(e>0.001)
{
h=(b-a)/n;
x=a+h;
s=0;int c=1;
while(x<b)
{
if((c%2)!=0)
k=4*x*x*x;
else
k=2*x*x*x;
s=s+k; c++ ; x=x+h;
}
y=h*(f+g+s)/3;
printf("\n %d \t \t %f",i++,y);
if(z>y)
e=z-y;
else
e=y-z;
z=y; n=n+2;
}
printf(\n Solution of integration is :%f,y);
}
SAMPLE OUTPUT:
Input the limits: 8.2 9.0
ITERATION
1
2
3
4
SOLUTION
607.145630
574.745667
509.945984
548.825623
@ [P]-[A__B]-[]
Page 18
5
6
7
542.345642
509.946136
509.945923
#include<stdio.h>
#include<math.h>
float f(float m);
main()
{
float a,b,n;
float h,s1=0,s2=0,s=0;
b=M_PI_2;
printf("\n Enter the limits : ");
scanf("%f %f",&a,&n);
float x[6],y[6];
x[0]=a;x[n]=b;
h=(b-a)/n;
for(int i=1;i<n;i++)
{
x[i]=x[0]+(i*h);
y[i]=f(x[i]);
@ [P]-[A__B]-[]
Page 19
if(i%3==0)
s2+=y[i];
else
s1+=y[i];
}
s=(3*h/8)*((f(a)+f(b)+(3*s1)+(3*s2)));
printf("\n The value of integration is : %f",s);
}
float f(float m)
{
float p;
p=sqrt(1-(0.162*sin(m)*sin(m)));
return p;
}
SAMPLE OUTPUT:
Enter the limits : 0
iterations
Page 20
PROGRAM:
#include<stdio.h>
int main( )
{
float x[10][10],k0,k1,k2,k3 ;
int i,j,k;
x[0][0]=0;x[1][0]=0;x[2][0]=0;x[3][0]=0;
printf (" iteration values are \n ") ;
for(i=0;i<6;i++)
{ k0=x[0][0];
k1=x[1][0];
k2=x[2][0];
k3=x[3][0];
x[0][0]=k1/5+k2/10+k3/10+0.3;
x[1][0]=k0/5+k2/10+k3/10+1.5;
x[2][0]=k0/10+k1/10+k3/5+2.7;
x[3][0]=k0/10+k1/10+k2/5-0.9;
for (j=0;j<4;j++)
{
for (k=0;k<1;k++)
{
printf(" % f ",x[j][k]);
printf (" ");
}
}
printf (" \n \n ");
}
return 0;
}
OUTPUT:
Iteration values are :
@ [P]-[A__B]-[]
Page 21
0.300000
0.780000
0.900000
0.962400
0.984480
0.993888
1.500000
1.740000
1.908000
1.960800
1.984800
1.993824
2.700000
2.700000
2.916000
2.959200
2.985120
2.993760
z
-0.900000
-0.180000
-0.108000
-0.036000
-0.015840
-0.006048
PROGRAM:
#include<stdio.h>
main()
{
float a,b,c,f,g;
printf("\nEnter the first value: ");
scanf("%f",&a);
f=(a*a)-(4*a)+3;
g=(2*a)-4;
b=a-(f/g);
printf("\n ITERATION
printf("\n\n 1
float e=0.1;
int i=0;
SOLUTION");
%f ",b);
while(e>0.000001)
{
c=b-((b*b)-(4*b)+3)/((2*a)-4);
if(c==2.0){printf("Solution is %f ",b);}
printf("\n %d \t
%f",i+2,c);
if(c>b)
@ [P]-[A__B]-[]
Page 22
e=(c-b);
else
e=(b-c);
b=c;
i++;
}
printf("\n\nSolution of function is %f",c);
}
SAMPLE OUTPUT:
Enter the first value : 0.5
ITERATION
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
SOLUTION
0.916667
0.974537
0.991728
0.997266
0.999091
0.999697
0.999899
0.999966
0.999989
0.999996
0.999999
1.000000
NEWTONS DIVIDED
DIFFERENCE METHOD
AIM :
To find value of a function at a given point using NEWTONS DIVIDED DIFFERENCE METHOD
Values given as f(5)=150, f(7)=392, f(11)=1452 , f(13)=2366, f(17)=5202
ALGORITHM:
1. Define function as given.
@ [P]-[A__B]-[]
Page 23
PROGRAM:
#include<iostream.h>
float x[20],f[20];
float func(int low,int high)
{
if(low==high-1)
return (f[high]-f[low])/(x[high]-x[low]);
else
return (func(low+1,high)-func(low,high-1))/(x[high]-x[low]);
}
float calcpx(float y,int n)
{
float px;
px=f[0];
float fact=1;
int j;
for(int i=1;i<=n-1;i++)
{
for(j=0;j<i;j++)
{
fact=fact*(y-x[j]);
}
px+=(fact*func(0,i));
fact=1;
}
return px;
}
int main()
{
//clrscr();
cout<<"Approximation of a func. by NEWTON'S DIVIDED DIFFERENCE RULE ";
int n ;
cout<<"\nEnter the no. of points to be considered : ";
@ [P]-[A__B]-[]
Page 24
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\nEnter point no. "<<i+1<<" x = ";
cin>>x[i];
cout<<"f(x) = ";
cin>>f[i];
}
float y;
cout<<"\nEnter the point where appx. value has to be found : ";
cin>>y;
float ans=calcpx(y,n);
cout<<"\nThe appx. value is : "<<ans;
return 0;
//getch();
}
OUTPUT:
Approximation of a function by newton divided difference method
Enter the no. of points to be considered : 5
Enter point no 1 x=5
f(x)=150
Enter point no 2 x=7
f(x)=392
Enter point no 3 x=11
f(x)=1452
Enter point no 4 x=13
f(x)=2366
Enter point no 5 x=17
f(x)=5202
enter the point where appx. Value has to be found : 9
the appx. Value is : 810
HUENS METHOD
AIM: To find value of y at x=0.3 for the derivative y=(x+y).
@ [P]-[A__B]-[]
Page 25
ALGORITM:
PROGRAM:
#include<stdio.h>
#include<math.h>
float f(float x,float y)
{
float z ;
z=x+y;
return z;
}
int main()
{
float x0,y0,y1,h,y11,e;
int i,n;
printf (" enter values of x0 , y0 , h , n \n ");
scanf ("%f%f%f%d",&x0,&y0,&h,&n);
printf(" x
y \n ");
for (i=0;i<n;i++)
{
y11=y0+h*(f(x0,y0));
y1=y0+((h/2)*(f(x0,y0)+f(x0,y11)));
printf("
iteration values of %f
printf(" %f \n ",y1);
if((y1-y11)>0)
e=(y1-y11);
else
e=(y11-y1);
are \n ",x0+h);
while (e>0.001)
{
@ [P]-[A__B]-[]
Page 26
y11=y1 ;
y1=y0+((h/2)*(f(x0,y0)+f(x0,y11)));
printf(" %f \n ",y1);
if((y1-y11)>0)
e=(y1-y11);
else
e=(y11-y1);
}
y0=y1;
x0=x0+h;
}
return 0;
}
SAMPLE OUTPUT:
Enter
values
of
x0 , y0 , h , n :
Iteration
1.051250
1.051281
values
of
0.050000
are
Iteration
1.107722
1.107756
values
of
0.100000
are
Iteration
1.169654
1.169692
values
of
0.150000
are
Iteration
1.237326
1.237367
values
of
0.200000
are
Iteration
1.311032
1.311077
values
of
0.250000
are
Iteration
1.391082
1.391131
values of
0.300000
0.05
are
****************************************
@ [P]-[A__B]-[]
Page 27