numerical analysis laboratory
TRANSCRIPT
-
8/2/2019 Numerical Analysis Laboratory
1/39
MADE BY ROLL NUMBER
1)GOURANGA SARKAR 1) 0008103010072) SANJOY CHANDRA MANDAL 2) 000810301013
3) BAPPA SAHA 3) 000810301018
4) KUNAL SINHA RAY 4) 000810301025
-
8/2/2019 Numerical Analysis Laboratory
2/39
C0NTENTS
SET 1 PROBLEM NO.3
SET 2 PROBLEM NO.7
SET 3 PROBLEM NO.3
SET 4 PROBLEM NO.3
SET 5 PROBLEM NO.3
SET 5 PROBLEM NO.9
SET 6 PROBLEM NO.3
SET 6 PROBLEM NO.9
SET 7 PROBLEM NO.1
GROUP NO.3
CLASS: B.CH.E 2nd
YEAR SECTION: A - 1
-
8/2/2019 Numerical Analysis Laboratory
3/39
SET 1 PROBLEM NO.3
TITLE: WRITE A PROGRAM TO FIND SUM OF THE FOLLOWING INFINITE SERIES
CORRECT TO THE FIFTH DECIMAL PLACES.
1+(1/2!)+(1/3!)+(1/4!)+(1/5!)+.
ALGORITHM:
Step 1: start algorithm.
Step 2: declare integer type variables a ,x ,float term=1,float sum=0. Initialize x with zero.
Step 3: clear the output screen.
Step 4: print the value of a.
Step 5: repeat step6 and step7 when a>0.
Step 6: term=term/x.
Step 7: sum=sum+term,x++, a--.
Step 8: print the sum.
Step 9: in the output and go to the next.
Step 10: end the algorithm.
-
8/2/2019 Numerical Analysis Laboratory
4/39
PROGRAM:
// program to calculate sum of series //
# include
# include
# include
# define eps 1.0e-05
void main(void)
{
int a;
int x=1;
float term=1;
float sum=0;
clrscr();
printf("\n\n\n enter the value of term at which term sum will be needed a:");
scanf("%d" ,&a);
} while(a>0)
{
term=term/x;
sum=sum+term;
x++;
a--;
}
printf("\n\n\n sum of series upto following term is =%f",sum);
getch();
}
-
8/2/2019 Numerical Analysis Laboratory
5/39
SAMPLE INPUT: Enter the value of a
1) 2
2) 3
3) 4
4) 5
5) 6
SAMPLE OUTPUT: Sum of series of corresponding value of a.
1) 1.5000000
2) 1.6666667
3) 1.7083334
4) 1.7166667
5) 1.7180556
.
REMARKS: From this program we can easily calcute the sum of this series for upto various
terms.
-
8/2/2019 Numerical Analysis Laboratory
6/39
SET 2 PROGRAM NO.7
TITLE:CALCULATE THE SPCIFIC VOLUME OF CHLORINE AT T=460K AND
P=15atm USING VANDER-WAALS EQUATION OF STATE USING BISECTION
TECHNIQUE.
P={RT/(v-b)}{ a/(v*v)}
a=27(R*R)*(T_c*T_c)/64P_c, b=RT_c/8P_c.
TAKE THE IDEAL GAS VOLUME AS THE INITIAL GUESS,P_c=76atm, T_c=417K
ALGORITHM:
Step 1: define the equestion f(x)=0.
Step 2: read the diserid accuracy say,epsilon(e) & maxit.
Step 3: do
{ Enter the initial approximation say, a & b.
} while(f(a)*f(b)>0);
Step 4: k < - 0.
Step 5: do
{ x < - (a+b)/2
if(f(a)*f(b)e & k < maxit);
Step 6: if (k
-
8/2/2019 Numerical Analysis Laboratory
7/39
PROGRAM:
//program for bisection method//
#include
#include
#include
main()
{
float f(float);
float x0,x1,x2,f0,f1,f2,R=0.0821,T=417,P=76;
int i=1,j;
printf("\n the volume of ideal gas is =%f",(R*T)/8*P);
printf("Give the guess values x0,x1 (x0
-
8/2/2019 Numerical Analysis Laboratory
8/39
printf("%d %f %f %f %f %f %f\n",i,x0,x1,x2,f0,f1,f2);
if((f2/f0)>0)
{
x0=x2;
f0=f2;
}
else
{
x1=x2;
f1=f2;
}
i++;
}
}
printf("\n\nThe root of the eqn converges to %f.",x2);
}
float f(float x)
{
float f;
f=(pow(x,3)-0.257*x*x+0.43*x-0.024);
return(f);
-
8/2/2019 Numerical Analysis Laboratory
9/39
SAMPLE INPUT : the volume of ideal gas =325.239136
the gauss value is X0 , X1 ( X0 < X1 )
-0.5
1
Upto which decimal point you want to the root?
0.0001
SAMPLE OUTPUT : 1 -0.500000 1.000000 0.250000 -0.428250 1.149000
2 -0.500000 0.250000 -0.125000 -0.428250 0.000000
22 -0.500000 -0.499999 -0.500000 -0.428250 0.000000
23 -0.500000 -0.500000 -0.500000 -0.428250 0.000000
REMARKS : the equestion converges to "-0.500000"
-
8/2/2019 Numerical Analysis Laboratory
10/39
SET 3 PROBLEM NO.3
TITLE: SOLVE THE FOLLOWING EQUESTION USING GAUSS-SIEDEL TECHNIQUE.
10*X1 X2 + 2*X3 =4
X1 + 10*X2 X3 =3
2*X1 + 3*X2 + 20*X3 =7
ALGORITHM:
Step 1: start the algorithm.
Step 2: sum < - 0.0
Step 3: read the desired accuracy say epsilone (e) & maxit.
Step 4: read the number of equestions.
Step 5: read the right handside constant ( b[][]).
Step 6: read the co-efficient matrix row wise ( a[][]).
Step 7: for i < - 1 to n.
X[i] < - 0.0
Repeat
Step 8: for i < - 1 to n.
for j < - 1 to n.
for k < - 1 to maxit
if( j!=1)
sum=sum + a[i][j]*X[j]
Previous x[i] < - X[i]
X[i] < - ( b[i] (sum) )/ a[i] [j]
Step 9: write i , x[i]
Step 10: fabs ( x[i]- prex[i])
-
8/2/2019 Numerical Analysis Laboratory
11/39
PROGRAM:
// program for gauss-siedel method //
#include
#include
#include
#define n 3
void main ( )
{
int i,j,p,k,maxit;
float x[n],a[n][n],sum=0,px[n],e=pow(e,-0.5),b[n];clrscr( );
printf("\n enter the number of equations say n:");
scanf("%d",&p);
printf("\enter the maximum iteration number say maxit:");
scanf("%d", & maxit);
printf("\n enter the right hand side constant say b:");
for(i=0;i
-
8/2/2019 Numerical Analysis Laboratory
12/39
for(i=0;i
-
8/2/2019 Numerical Analysis Laboratory
13/39
SAMPLE INPUT : enter the number of equestion ,say n : 3
enter the maximum iteration number say maxit :50
enter the right hand side constant, say b :
b[ 0 ] = 4
b[ 0 ] = 3
b[ 0 ] = 7
enter the coefficient matrix (raw wise),say a[ ][ ] :
a[ 0 ][ 0 ] = 10
a[ 0 ][ 1 ] = -1
a[ 0 ][ 2 ] = 2
a[ 1 ][ 0 ] = 1
a[ 1 ][ 1 ] = 10
a[ 1 ][ 2 ] = -1
a[ 2 ][ 0 ] = 2
a[ 2 ][ 1 ] = 3
a[ 2 ][ 2 ] = 20
CORRESPONDING SAMPLE OUTPUT :
the solution x[ 0 ] = 0.400000
the solution x[ 1 ] = 0.300000
the solution x[ 2 ] = 0.350000
REMARKS: this approximate value is very close to the actual mathematical value.
-
8/2/2019 Numerical Analysis Laboratory
14/39
SET 4 PROBLEM NO.3
TITLE: USE LAGRANGIAN FORMULA FOR POLYNOMIAL APPROXIMATION AND
SIMPSOMS RULE TO EVALUTE THE INTEGRAL .
ALGORITHM:
Step 1: start the algorithm.
Step 2: define function f(x).
Step 3: read the values a(lower limit) , b(upper limit) & n(no. of strips).
Step 4: h < - (b-a)/n
Step 5: ends < - f(a) + f(b)
Step 6: k < - 1sum 1< - 0.0
sum 2< - 0.0
do
{
If (k%2==0)
sum 1+=f(a+k*h)
else if (k%2==1)
sum 2+=f(a+k*h)
k< - k+1}while (K
-
8/2/2019 Numerical Analysis Laboratory
15/39
PROGRAM:
// program for lagrangian formula //
#include
#include
#include
void main()
{
float x[50],y[50],sum=0.0,product,z;
int n,i,j,k,X,Y;
clrscr();
printf("\n enter the total terms:");
scanf("%d",&x);
printf("\n \t\t--:enter the table:--");
printf("\n X:");
scanf("%d",&X);
printf("\n Y:");
scanf("%d",&Y);
for(i=0;i
-
8/2/2019 Numerical Analysis Laboratory
16/39
for(k=0;k
-
8/2/2019 Numerical Analysis Laboratory
17/39
SAMPLE INPUT:
enter the total terms :
--:enter the table
x:
y:
enter the point at which you want to inter polate:
SAMPLE OUTPUT:
sum=
interpolation at (x=)=
-
8/2/2019 Numerical Analysis Laboratory
18/39
PROGRAM:
// program for simson's rule//
#include
#include
#include
#define f(x) ((((x-1)*(x-2)*(x-4)*(x-5)*(x-6))/((0-1)*(0-2)*(0-4)*(0-5)*(0-6))*1)+(((x-0)*(x-
2)*(x-4)*(x-5)*(x-6))/((1-0)*(1-2)*(1-4)*(1-5)*(1-6))*15)+(((x-0)*(x-1)*(x-4)*(x-5)*(x-
6))/((2-0)*(2-1)*(2-4)*(2-5)*(2-6))*14)+(((x-0)*(x-2)*(x-5)*(x-1)*(x-6))/((4-0)*(4-1)*(4-
5)*(4-6)*(4-2))*5)+(((x-0)*(x-2)*(x-4)*(x-6)*(x-1))/((5-0)*(5-1)*(5-2)*(5-4)*(5-
6))*6)+(((x-0)*(x-2)*(x-1)*(x-4)*(x-6))/((6-0)*(6-1)*(6-2)*(6-4)*(6-5))*19))
void main()
{
float a,b,h,sum1,sum2,ends,i;
int j,n;
clrscr();
printf("\n enter the limits of integration:");
printf("\n lower limit (a)=\t");
scanf("%f",&a);printf("\n uppar limit(b)=\t");
scanf("%f",&b);
printf("\n enter the no. of subinterval:\n by which the interval[a,b]to be
devided :\n");
scanf("%d",&n);
if(n%2==0)
{
h=(b-a)/(float)n;
ends=f(a)+f(b);sum1=0.0;
sum2=0.0;
for(j=1;j
-
8/2/2019 Numerical Analysis Laboratory
19/39
else
sum2+=f(a+j*h);
}
i=(h/3)*(ends+(4*sum2)+(2*sum1));
printf("\n required integral(i)=%f",i);}
else
{
printf("\n no need for calculations");
}
getch();
}
-
8/2/2019 Numerical Analysis Laboratory
20/39
SAMPLE INPUT:
Enter the limits of integration :
Lower limit (a) = 0
Upper limit (b) = 6
Enter the no.of subinterval by which the interval [a,b] to be devided: 2
SAMPLE OUTPUT:
Required integral (i) =38.700001
REMARKS: the numerical value is approximately equal to the mathematical
value.
-
8/2/2019 Numerical Analysis Laboratory
21/39
SET 5 PROGRAM NO.3
TITLE: THE EMPIRICAL EQUESTION FOR THE WORLDS POPULATION ,y AS A
FUNCTION OF TIME IS GIVEN BY(1)dy/dt=4.25713*pow(10,-12)*pow(y,2010101) ,y0= pow(10,9)
THE BASE YEAR IS 1840 AD. OBTAIN THE WORLD POPULATION AT
DIFFERENT YEARS HENCEFORTH.
(2)Dy/dt= -200[y F(t)] + dF(t)/dt ; y0=10
F(t)=10 (10 + t)*pow(e,-t)
THE ANALYTIC SOLUTION IS
y= 10- (10 + t)*pow(e,-t) + 10*exp( -200t)
USING RANGA-KUTTA(4th
ORDER).
ALGORITHM:
Step 1: Difine f(t,y) [=RHS of ODE dy/dx= f(t,y)
Step 2: Read t0,y0,h [initial value of t,y & step size h no of t]
Step 3: Read tp [value of t at which y required]
Step 4: n=( tp-t)/h + 0.5 [n= number of step size]
Step 5: For I < - 1 to n
Step 6: K1 < - f(t0,y0)
Step 7: K2 < - f(t0 +1/2*h, y0 + 1/2*K1*h)
Step 8: K3 < - f(t0 +1/2*h, y0 + 1/2*K2*h)
Step 9: K4 < - f(t0 + 1/2*h,y0 +1/2* K3*h)
Step 10: t0 < - t0 + h
Step 11: K < - h/6*(K1 + 2*K2 + 2*K3 + 1/2*K3*h)
Step 12: yi < - yi + K
Step 13: Write I,ti,yi
Step 14: next i /(repeat)
Step 15: end the algorithm.
-
8/2/2019 Numerical Analysis Laboratory
22/39
PROGRAM (1);
//program for range kutta//
#include
#include
#include
main()
{
int i,n;
float p,t,y,xp,h,m11,m12,m21,m22,m13,m23,m14,m24;
float func(float,float,float);
printf("enter the base year and pop.\n");
scanf("%f%f",&t,&y);
printf("\n enter the year at which pop. req.:");
scanf("%f",&xp);
printf("\n enter the step size,h:");
scanf("%f",&h);
n=(int)((xp-t)/h+0.5);
for(i=1;i
-
8/2/2019 Numerical Analysis Laboratory
23/39
m23=func(p+0.5*h,t+0.5*m12,y+0.5*m22);
m14=func(p+h,t+m13,y+m23);
m24=func(p+h,t+m13,y+m23);
t=t+(m11+2.0*m12+2.0*m13+m14)/6.0;
y=y+(m21+2.0*m22+2.0*m23+m24)/6.0;
printf("%5d%15.6f%15.6f\n",i,t,y);
}
printf("\n value of pop.at req. year=%f is %f\n ",t,y);
}
float func(float p,float t,float y)
{
float f;
f=(4.25713*pow(10,-12)*pow(y,2.010101));
return(f);
}
-
8/2/2019 Numerical Analysis Laboratory
24/39
SAMPLE INPUT :
ENTER THE BASE YEAR AND POPULATION -
1840
0.2
ENTER THE YEARAT WHICH POPULATION REQUIRED : 1841
ENTER THE STEP SIZE , h = 0.02
CORRESPONDING SAMPLE OUTPUT :
1 1840.000000 0.200000
REMARKS :
VALUE OF POPULATION AT REQUIRED YEAR = 1840 IS 0.200000
-
8/2/2019 Numerical Analysis Laboratory
25/39
PROGRAM (2):
//program for range kutta//
#include
#include
#include
main()
{
int i,n;
float t,y,xp,h,m1,m2,m3,m4;
float func(float,float);
printf("enter the base year and pop.\n");
scanf("%f%f",&t,&y);
printf("\n enter the year at which pop. req.:");
scanf("%f",&xp);
printf("\n enter the step size,h:");
scanf("%f",&h);
n=(int)((xp-t)/h+0.5);
for(i=1;i
-
8/2/2019 Numerical Analysis Laboratory
26/39
m4=func(t+h,y+m3*h);
t=t+h;
y=y+(m1+2.0*m2+2.0*m3+m4)*h/6.0;
printf("%5d%15.6f%15.6f\n",i,t,y);
}
printf("\n value of pop.at req. year=%f is %f\n ",t,y);
}
float func(float t,float y)
{
float f;
f=(4.25713*pow(10,-12)*pow(y,2.010101));
return(f);
}
-
8/2/2019 Numerical Analysis Laboratory
27/39
SAMPLE INPUT :ENTER THE BASE YEAR AND POPULATION -
1840
0.2
ENTER THE YEARAT WHICH POPULATION REQUIRED : 1841
ENTER THE STEP SIZE , h = 0.02
CORRESPONDING SAMPLE OUTPUT :
1 1840.000000 0.200000
REMARKS :
VALUE OF POPULATION AT REQUIRED YEAR = 1840 IS 0.200000
-
8/2/2019 Numerical Analysis Laboratory
28/39
SET 6 PROBLEM NO.3
TITLE: (1)COMPUTE THE NUMERICAL SOLUTION OVER THE INTERVEL [0,2]. USING
4TH
ORDER RANGA KUTTA METHOD.
dy1/dt= -0.2*y1 + 0.2*y2
dy2/dt= 10*y1 (60+0.125*t)*y2 + 0.124*t ,[y0]=[0,0]T
(2)COMPUTE THE NUMERICAL SOLUTION, USING 4TH
ORDER RANGA KUTTA.
dy1/dt= 1.3*(y2-y1) + 1.04*x*10^4ky2
dy2/dt= 1.88*x*10^3*[y4-(1+k)]
dy3/dt= 1752 + 266.1y2 - 269.3*y3
dy4/dt= 0.1 + 320y2 321*y4
k= 6*x*10^-3exp[20.7 - 15*x*10^3/y1]
[y0]= [759.167,0,600,0.1]^T
ALGORITHM:
Step 1: Difine f(t,y) [=RHS of ODE dy/dx= f(t,y) Step 2: Read t0,y0,h [initial value of t,y & step size h no of t]
Step 3: Read tp [value of t at which y required]
Step 4: n=( tp-t)/h + 0.5 [n= number of step size]
Step 5: For I < - 1 to n
Step 6: K1 < - f(t0,y0)
Step 7: K2 < - f(t0 +1/2*h, y0 + 1/2*K1*h)
Step 8: K3 < - f(t0 +1/2*h, y0 + 1/2*K2*h)
Step 9: K4 < - f(t0 + 1/2*h,y0 +1/2* K3*h)
Step 10: t0 < - t0 + h Step 11: K < - h/6*(K1 + 2*K2 + 2*K3 + 1/2*K3*h)
Step 12: yi < - yi + K
Step 13: Write I,ti,yi
Step 14: next i /(repeat)
Step 15: end the algorithm.
-
8/2/2019 Numerical Analysis Laboratory
29/39
PROGRAM (1):
//program for 4th order ranga kutta//
#include
#include
#include
main()
{
int i,n;
float t,y1,y2,xp,h,m11,m12,m21,m22,m13,m23,m14,m24;
float func1(float,float,float);
float func2(float,float,float);
printf("enter the initial values of t,y1,y2 \n");
scanf("%f%f%f",&t,&y1,&y2);
printf("\n enter the time at which y1 and y2 are req.:");
scanf("%f",&xp);
printf("\n enter the step size,h:");
scanf("%f",&h);
n=(int)((xp-t)/h+0.5);
for(i=1;i
-
8/2/2019 Numerical Analysis Laboratory
30/39
m13=h*func1(t+0.5*h,y1+0.5*m12,y2+0.5*m22);
m23=h*func2(t+0.5*h,y1+0.5*m12,y2+0.5*m22);
m14=h*func1(t+h,y1+m13,y2+m23);
m24=h*func2(t+h,y1+m13,y2+m23);
t=t+h;
y1=y1+(m11+2.0*m12+2.0*m13+m14)/6.0;
y2=y2+(m21+2.0*m22+2.0*m23+m24)/6.0;
printf("%5d%15.6f%15.6f%15.6f\n",i,t,y1,y2);
}
printf("\n values of y1 and y2 at t=%f is %f and %f",t,y1,y2);
}
float func1(float t,float y1,float y2)
{
float f1;
f1=(-0.2*y1+0.2*y2);
return(f1);
}
float func2(float t,float y1, float y2)
{
float f2;
f2=(10.0*y1-(60.0+0.125*t)*y2+0.124*t);
return(f2);
}
-
8/2/2019 Numerical Analysis Laboratory
31/39
SAMPLE INPUT :
ENTER THE INITIAL VALUES OF t , y1 , y2
0
0
0ENTER THE TIME AT WHICH Y1 AND Y2 ARE REQUIRED :2
ENTER THE STEP SIZE ,h :0.001
CORRESPONDING SAMPLE OUTPUT :
VALUES OF y1 AND y2 AT t = 2.000037 IS0.000728
AND 0.004201
-
8/2/2019 Numerical Analysis Laboratory
32/39
PROGRAM (2):
//program for 4th order multidimentional range kutta//
#include
#include
#include
main()
{
int i,n;
float
t,y1,y2,y3,y4,xp,h,m11,m12,m21,m22,m13,m23,m14,m24,m31,m32,m33,m34,m41,m42,m43,
m44;
float func1(float,float,float,float,float);
float func2(float,float,float,float,float);
float func3(float,float,float,float,float);
float func4(float,float,float,float,float);
printf("enter the initial values of t,y1,y2,y3,y4 \n");
scanf("%f%f%f%f%f",&t,&y1,&y2,&y3,&y4);
printf("\n enter the time at which y1,y2,y3 and y4 are req.:");
scanf("%f",&xp);
printf("\n enter the step size,h:");
scanf("%f",&h);
n=(int)((xp-t)/h+0.5);
-
8/2/2019 Numerical Analysis Laboratory
33/39
for(i=1;i
-
8/2/2019 Numerical Analysis Laboratory
34/39
printf("%5d%15.6f%15.6f%15.6f%15.6f%15.6f\n",i,t,y1,y2,y3,y4);
}
printf("\n values of y1,y2,y3 and y4 at t=%f are %f,%f,%f and %f",t,y1,y2,y3,y4);
}
float func1(float t,float y1,float y2,float y3,float y4)
{
float f1;
f1=1.3*(y2-y1)+1.04*pow(10,4)*(6*pow(10,-4)*pow(2.73,(20.7-
(15*pow(10,3))/y1)))*y2;
return(f1);
}
float func2(float t,float y1, float y2,float y3,float y4)
{
float f2;
f2=1.88*pow(10,3)*(y4-(1.0+(6*pow(10,-4)*pow(2.73,(20.7-(15*pow(10,3))/y1)))));
return(f2);
}
float func3(float t,float y1,float y2,float y3,float y4)
{
float f3;
f3=(1752+266.1*y2-269.3*y3);
return(f3);
}
-
8/2/2019 Numerical Analysis Laboratory
35/39
float func4(float t,float y1,float y2,float y3,float y4)
{
float f4;
f4=(0.1+320*y3-321.0*y4);
return(f4);
}
-
8/2/2019 Numerical Analysis Laboratory
36/39
SAMPLE INPUT :
ENTER THE INITIAL VALUES OF t , y1 , y2
0
00
ENTER THE TIME AT WHICH Y1 AND Y2 ARE REQUIRED :2
ENTER THE STEP SIZE ,h :0.001
CORRESPONDING SAMPLE OUTPUT :
VALUES OF y1 AND y2 AT t = 2.000037IS 0.000728
AND 0.004201
-
8/2/2019 Numerical Analysis Laboratory
37/39
SET 7 PROBLEM NO.1
TITLE:MULTIDIMENSIONAL NEWTON RAPSONS METHOD.
#include
#include
#include
#define f(x,y) (x*x+x*y+y*y-7)
#define g(x,y) (pow(x,3)+pow(y,3)-9)
main()
{
int i,j,k,n;
double x_0=1.5,y_0=0.5,det_J,b[2][1],a[2][1],x[2][1];
b[0][0]=x_0;
b[1][0]=y_0;
x[0][0]=x_0;
x[1][0]=y_0;
for(k=0;k
-
8/2/2019 Numerical Analysis Laboratory
38/39
x[0][0]=b[0][0]-(a[0][0]/det_J);
x[1][0]=b[1][0]-(a[1][0]/det_J);
printf("\nFor the %d iteration current value: %f %f",k+1,x[0][0],x[1][0]);
b[0][0]=x[0][0];
b[1][0]=x[1][0];
}
getch();
return 0;
}
-
8/2/2019 Numerical Analysis Laboratory
39/39
SAMPLE OUTPUT:
for the 1 iteration current value:2.267544 0.925439
for the 1 iteration current value:2.037271 0.964470
for the 1 iteration current value:2.001258 0.998737
for the 1 iteration current value:2.000002 0.999998
for the 1 iteration current value:2.000000 1.000000
for the 1 iteration current value:2.000000 1.000000