Practical Journal (To All Department)
Practical Journal (To All Department)
Practical Journal (To All Department)
of
Name:________________________________________________________
1
DEPARTMENT OF BASIC SCIENCES AND RELATED STUDIES
MEHRAN UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
JAMSHORO, SINDH, PAKISTAN.
BSRS LABORATORY
LIST OF PRACTICALS
02
To find the root of non-linear equations f x 0 using Regula-Falsi
method.
06 To find Dominant Eigen values and Eigen Vectors using Power’s method
2
09 To Interpolate using Lagrange’s Interpolation Formula.
3
PRACTICAL#01
Objective: Write a C++ program to calculate absolute error, relative error & percentage error
(taking exact value x=5.8395 and approximate value x1=5.7995).
SOURCE CODE
//To compute Absolute, Relative and Percentile Errors
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float ExactX, ApproximateX;
float AError, RError, ApError, RpError ;
cout<<"Enter the Exact value"; cin>>ExactX ;
cout<<"Enter the Approximate value";
cin>>ApproximateX ;
AError = fabs(ExactX - ApproximateX) ;
RError = fabs((ExactX - ApproximateX)/fabs(ExactX)) ;
ApError = AError * 100;
RpError = RError * 100;
cout<<"\n Absolute Error = "<<AError;
cout<<"\n Relative Error = "<<RError;
cout<<"\n Absolute Percentile Error = "<<ApError;
cout<<"\n Relative Percentile Error = "<<RpError;
cout<<”\nF16EL34”;
return 0 ;
}
4
Input Output
5
Review Questions
Answer:
Answer:
Q.3) Discus the physical significance of Absolute, Relative and Percentile error?
Answer:
6
PRACTICAL#02
7
SOURCE CODE:
// To find root of f(x)=0 using Bisection Method
#include <iostream>
#include <cmath>
#define f(x) (x*exp(x)-cos(x))
using namespace std;
int main ()
{
float a,b,xn,xo=a,et,error;
int count=1;
ab:
cout<<"\nEnter the values of a, b, and error tolerance";
cin>>a>>b>>et;
if(f(a)*f(b)>0)
{cout<<"\nRoot doesnot lie between "<<a<<" and "<<b;
goto ab;
}
cout<<"\n*************Bisection Meth-od**************\n";
cout<<"\nit#\t a \t b \t Root \t Error \n";
do
{ cout<<count;
xn=(a+b)/2;
error=fabs(xn-xo);
printf("\t%.5f\t\t%.5f\t\t%.5f\t\t%.5f",a,b,xn,error);
cout<<"\n............................................................................\n";
if (f(a)*f(xn)<0) {b=xn; xo=xn;}
else
{a=xn; xo=xn;}
count++;
}
while (error>et);
cout<<"\n The approximate root containing "<<et<<" error is "<<xn;
cout<<”\nF16EL34”;
return 0;
}
8
Input Output
a=2, b=3
9
Review Questions:
10
PRACTICAL#03
Objective: Write C++ program to find an approximate root of cos x xe x (2 dp) using Regula-
Falsi method.
SOURCE CODE:
// To find root of f(x)=0 using Regula-Falsi Method.Error tolerance: absolute error
at most 0.001
#include <iostream>
#include <cmath>
#define f(x) (x*exp(x)-cos(x))
using namespace std;
int main ()
{
float a,b,xn,xo=a,et,error;
int count=1;
ab:
cout<<"\nEnter the values of a, b, and error tolerance";
cin>>a>>b>>et;
if(f(a)*f(b)>0)
{cout<<"\nRoot doesnot lie between "<<a<<" and "<<b;
goto ab;
}
cout<<"\n*************regular falsi**************\n";
cout<<"\nit#\t a \t b \t Root \t Error \n";
do
{ cout<<count;
xn=(a*f(b)-b*f(a))/(f(b)-f(a));
error=fabs(xn-xo);
printf("\t%.5f\t\t%.5f\t\t%.5f\t\t%.5f",a,b,xn,error);
cout<<"\n............................................................................\n";
if (f(a)*f(xn)<0)
{b=xn; xo=xn;}
else
{a=xn; xo=xn;}
count++;
}
while (error>et);
cout<<"\n The approximate root containing "<<et<<" error is "<<xn;
cout<<”\nF16EL34”;
12
return 0;
}
Input Output
13
Review Questions:
14
PRACTICAL#04
{ float xo,xn,et,error;
int iteration=1;
cout<<"\nEnter initial guess and error tolerance value ";
cin>>xo>>et;
cout<<"\n*******************NEWTON-RAPHSON
METHOD*********************\n";
do
{ cout<<"\n"<<iteration;
xn=xo-f(xo)/df(xo);
error=fabs(xn-xo);
printf ("\t\t%f\t\t%f",xn,error);
cout<<"\n..........................................................";
xo=xn;
iteration++;
}
while (error>et);
cout<<"\n The approximate root is "<<xn;
cout<<”\nF16EL34”;
getch();
return 0;
}
16
Input Output
17
REVIEW QUESTIONS:
Answer:
Answer:
Answer:
18
PRACTICAL#05
SOURCE CODE:
//Fixed Point Iteration Method
#include<iostream>
#include<cmath>
#define g(x) ((pow(x,3)-12)/13)// Write the re-arrangement function g(x)
using namespace std;
int main()
{
float xo,x,Error,ReqError;
int i=0;
cout<<"Enter the value of initial guess i.e. x0 = ";
cin>>xo;
cout<<"Required Accuracy (in %) = ";
cin>>ReqError;
do{ x= g(xo);
Error=fabs(x-xo)*100;
xo = x;
i++;
cout<<"\n\n Iteration# ="<<i<<"\t Root="<<x<<"\tError="<<Error; }
while(Error>ReqError);
cout<<"\nThe approximate root of given equation is found to be "<<x<<" upto "<<
Error<<"% absolute error after "<<i<<" iterations by Fixed-Point Iteration
Method.";
system("pause" );
cout<<”\nF16EL34”;
return 0 ; }
19
Output:
20
REVIEW QUESTION:
Q.1) what is the use of fixed point iteration method?
Answer:
Answer:
Answer:
Answer:
21
PRACTICAL#06
Objective: Write C++ program to solve the following linear system using Gauss Jacobi method
upto 5% accuracy.
4 x y z 13, 3x 5 y 2 z 21, 2 x y 6 z 19
Algorithm Of Gauss Jacobi
Step
1. Read: n
2. for i =1 to n by 1 do
3. for j=1 to n+1 by 1 do
4. read; aij
end for
5. Read: maxit, epsilon
6. for i=1 to n by 1 do
7. Set old_xi = 0
endfor
8. for k =1 to maxit by 1 do
9. set big =0
10. for i=1 to n by 1 do
11. set sum = 0
12. for j =1 to n by 1 do
13. if (i ≠ j) then
14. set sum = sum + aij × old_xj
endif
endfor
15. new_xi = (ai(n+1)- sum)/aii
16. relerror = | (new_xi – old_xi) / new_xi|
17. if (relerror >big) then
18. set big = relerror
endif
endfor
19. if (big ≤ epsilon) then
20. write “solution converges in ”, k, “iteration”
21. for i =1 to n by 1 do
22. write: new_x1 as solution vector
22
endfor
23. exit
endif
24. for i=1 to n by 1 do
25. set old_xi = new_xi
endfor
endfor
26. write: “ solution does not converge in” , maxit, “iteration”
27. exit
SOURCE CODE
// The Jacobi's Method for Linear Algebraic Systems
//Solve: 8x-3y+2z=20; 4x+11y-z=33; 6x+3y+12z=35
// Error tolerance = 0.001%
#include <iostream>
#include <cmath>
#define X(y,z) ((120 -3*y+z)/50*14)
#define Y(x,z) ((110-2*x-4*z)/50*14)
#define Z(x,y) ((108-x+y)/50*14)
using namespace std;
int main( )
{
float x0,y0,z0,xn,yn,zn,ReqErr,errx,erry,errz;
int iteration=1;
cout<<"\n Enter initial guess and error tolerance: ";
cin>>x0>>y0>>z0>>ReqErr;
printf("\nit# \t x\t y\t z\t errx\t erry\t errz \n");
do
{ cout<<endl<< iteration;
xn=X(y0,z0);
errx=fabs((xn-x0)/xn)*100;
yn=Y(x0,z0);
erry=fabs((yn-y0)/yn)*100;
zn=Z(x0,y0);
errz=fabs((zn-z0)/zn)*100;
printf("\t%.4f\t %.8f \t %.8f \t %.8f \t %.8f \t%.8f ",xn,yn,zn,errx,erry,errz);
x0=xn;y0=yn;z0=zn;
iteration++;
23
}
while (errx>ReqErr||erry>ReqErr||errz>ReqErr);
cout<<"\n\n The Approximate solution is x = "<<xn<<" , y = "<<yn<<" and z =
"<<zn<<" . ";
cout<<”\nF16EL34”;
return 0;
}
Input Output
24
REVIEW QUESTIONS:
Answer:
Answer:
Answe
25
PRACTICAL#07
Objective: Write C++ program to solve the following linear system using Gauss Seidel method
upto 5% accuracy.
10 x 2 y 3 z 205, 2 x 10 y 2 z 154, 2 x y 10 z 120
1) Read: n
2) For I = 1 to n by 1 do
3) For J = 1 to n+1 by do
4) Read: aij
endfor
endif
26
endfor
Endfor
24) exit
endfor
endfor
25) write: “Solution does not converge in”, maxit, “iterations”
26) exit
SOURCE CODE
//Gauss's Seidal Method for Linear Algebraic Systems
//Solve: 8x1-3x2+2x3=20; 4x1+11x2-x3=33; 6x1+3x2+12x3=35
// Error tolerance = 0.001%
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#define X(y,z) ((120 -3*y+z)/50*14)
#define Y(x,z) ((110-2*x-4*z)/50*14)
#define Z(x,y) ((108-x+y)/50*14)
using namespace std;
int main( )
{
float x0,y0,z0,xn,yn,zn,ReqError,errx,erry,errz;
int iteration=1;
cout<<"\n Enter initial guess and error tolerance: ";
cin>>x0>>y0>>z0>>ReqError;
printf("\nit# \t x\t y\t z\t errx\t erry\t errz \n");
do
{ cout<<endl<< iteration;
xn=X(y0,z0);
errx=fabs((xn-x0)/xn)*100;
27
x0=xn;
yn=Y(x0,z0);
erry=fabs((yn-y0)/yn)*100;
y0=yn;
zn=Z(x0,y0);
errz=fabs((zn-z0)/zn)*100;
z0=zn;
printf("\t%.4f\t %.4f\t %.4f \t %.4f \t %.4f\t %.4f ",xn,yn,zn,errx,erry,errz);
iteration++;
}
while (errx>ReqError||erry>ReqError||errz>ReqError);
cout<<"\n\n The Approximate solution is x = "<<xn<<" , y = "<<yn<<" and z =
"<<zn<<" . ";
cout<<”\nF16El34”;
return 0;
}
Input Output
28
REVIEW QUESTIONS:
Answer:
Answer:
29
PRACTICAL#08
Objective: Write C++ program to find dominant eigen value of the following matrix correct to 2dp using
Power method.
10 7 8
A 7 6 6
8 6 19
Algorithm of power Method:
Step 1. Read: n
2. for i = 1 to n by 1 do
3. for j = 1 to n by 1 do
4. read: aij
endfor
endfor
5. read: epsilon
6. for i = 1 to n by 1 do
7. set xi = 0
endfor
8. set big = 0
9. for i = 1 to n by 1 do
10. set si = 0
11. for j = 1 to n by 1 do
endfor
13. lambda = si I
14. for i = 1 to n by 1 do
endif
Endfor
22. for i = 1 to n by 1 do
endfor
24. exist
SOURCE CODE
31
{
float a[10][10],x0[10],y[10],xn[10],ibig=0.0,big,error,et;
int i,j,n,iteration=1;
cout<<"\n enter order of matrix : ";cin>>n;
cout<<"\n enter coefficients of the matrix : ";
for (i=0;i<n;i++)
for (j=0;j<n;j++)
cin>>a[i][j];
cout<<"\n enter initial eigen vector : ";
for (i=0;i<n;i++)
cin>>x0[i];
cout<<"\n enter error tolerance : ";cin>>et;
cout<<"\n it #"<<setw(10)<<" E-Value "<<setw(10)<<" E-Vector
"<<setw(20)<<" Error "<<endl;
do
{
for (i=0;i<n;i++)
{
//y[i]=0.0;
for (j=0;j<n;j++)
y[i]+=a[i][j]*x0[j];
}
big=y[1];
for(i=1;i<n;i++)
if (fabs(y[i])>fabs(big))
big=y[i];
for (i=0;i<n;i++)
xn[i]=(y[i]/big);
error=fabs((big-ibig)/big)*100;
cout<<endl<<iteration;
printf(" \t % .4f ",big);
for (i=0;i<n;i++)
printf("\t%.4f ",xn[i]);
printf(" \t%.4f ",error);
ibig=big;
for (i=0;i<n;i++)
x0[i]=xn[i];
32
iteration++;
}
while (error>et);
cout<<”\nF16EL34”;
getch();
return 0;
}
Input Output
33
REVIEW QUESTIONS:
Q.4) What does the power method yields smallest Eigen value?
Answer:
34
PRACTICAL#09
1. Read: n, x
2. For i = 1 to n by 1 do
3. Read: xi , yj
Endfor
4. If ((x<x1) or (x>xn)) then
5. Write: “value lies inside range”
6. Exit
Endif
7. For i = 2 to n by 1 do
8. If (xi>x) then go to step 9
Endfor
9. Set k = i-1
10. Set u = (x-xk)/(xk+1-xk)
11. For j =1 to n-1 to by 1 do
12. For i = 1 to n-j by 1 do
13. If (j =1) then
14. dij = yi+1 - yi
else
15. dij = d(i+1)(j-1) – di(j-1)
endif
endfor
endfor
16. set sum = yk
17. set i =1 to n-k by 1 do
18. set prod = 1.0
19. for j = 0 to i-1 by 1 do
20. set prod = prod × (u-j)
21. find i! and let its value be m
35
22. set sum = sum +(dki × prod)/m
endfor
endfor
23. write: sum as the interpolated value
24. Exit
Source Code
// Georgy-Newton's Forward Difference Interpolation Formula
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main( )
{
float x[10],y[10][10],xp,p,h,yp;
int n,i,j;
printf("Enter total number of data points : ");
cin>>n;
cout<<"\n Enter data points : ";
printf("\nX\tY\n");
for(i=0;i<n;i++)
{
cin>>x[i]>>y[i][0];
}
//Forward Difference Table
for(j=1;j<n;j++)
for(i=0;i<(n-j);i++)
36
y[i][j] = y[i+1][j-1] - y[i][j-1];
printf("\n***********Forward Difference Table ***********\n");
for(i=0;i<n;i++)
{
printf("\n\t%.2f \t%.2f",x[i],y[i][0]);
for(j=1;j<(n-i);j++)
printf("\t%.2f",y[i][j]);
}
// Newton's Forward Difference Interpolation Formula's Calculation
printf("\n enter value of x at which y is required : ");
cin>>xp;
h=x[1]-x[0];
p=(xp-x[0])/h;
yp=y[0][0]+p*y[0][1]+(p*(p-1)*y[0][2])/2+(p*(p-1)*(p-2)*y[0][3])/6+(p*(p-
1)*(p-2)*(p-3)*y[0][4])/24;
cout<<"\n The required value is f("<<xp<<") = "<<yp;
cout<<”\nF16EL34”;
return 0;
}
37
Input Output
38
REVIEW QUESTIONS
Anwer:
Answer:
Q.3 Which are the main pitfall of the newton forward interpolation formula?
Answer:
39
PRACTICAL#10
1. Read: n,x
2. For i = 1 to n by 1 do
3. Read: xi , yi
Endfor
4. If ((x>x1) or (x>xn)) then
5. Write: “value lies outside range”
6. Exit
Endif
7. For i = 2 to n by 1 do
8. If (xi>x) then go to step 9
Endfor
9. Set k = i-1
10.Set u = (x-xk)/(xk -xk-1)
11.For j =1 to n-1 to by 1 do
12.For i = J+1 to n by 1 do
13.If (j =1) then
14.dij = yi - yi - 1
else
15.dij = di(j-1) – d(i-1)(j-1)
endif
endfor
endfor
16.set sum = yk
17.set i =1 to k-1 by 1 do
18.set prod = 1.0
19.for j = 0 to i-1 by 1 do
20.set prod = prod × (u+j)
21.find i! and let its value be m
22.set sum = sum +(dki × prod)/m
40
endfor
endfor
23. write: sum as the interpolated value
24.Exit
Source Code:
// Newton's Backward Interpolation Formula
#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;
int main( )
{
float x[10],y[10][10],xp,p,h,yp;
int n,i,j;
printf("Enter total number of data points: ");
cin>>n;
cout<<"\n Enter data points : ";
printf("\nX\tY\n");
for(i=0;i<n;i++)
{
cin>>x[i]>>y[i][0];
}
//Backward Difference Table
for(j=1;j<n;j++)
for(i=n-1;i>=j;i--)
y[i][j] = y[i][j-1] - y[i-1][j-1];
41
printf("\n***********Backward Difference Table ***********\n\n");
for(i=0;i<n;i++)
{
printf("\n\t%.2f\t%.2f",x[i],y[i][0]);
for(j=1;j<=i;j++)
printf("\t%.2f",y[i][j]);
}
// Newton's Backward Interpolation Formula's Calculation
cout<<"\n enter x value at which y is required: ";
cin>>xp;
h=x[1]-x[0];
p=(xp-x[n-1])/h;
yp=y[n-1][0]+p*y[n-1][1]+(p*(p+1)*y[n-1][2])/2+(p*(p+1)*(p+2)*y[n-1][3])/6;
cout<<"\n The required value is y("<<xp<<") = "<<yp;
cout<<”F16EL34”;
getch();
return 0;
}
42
Input Output
43
REVIEW QUESTIONS:
Answer:
Q.2 If 𝒚𝟎 = 𝟏, 𝒚𝟏 = 𝟐, 𝒚𝟐 = 𝟒 then ∆𝟐 𝒚𝟎 =?
Answer:
Answer:
44
PRACTICAL#11
Objective: Write C++ program to estimate y at x 10 from the following data set using
Lagrange’s interpolation formula.
x 5 6 9 11
y 12 13 14 16
ALGORITHM: Langrange
Step 1. read: n, x
2. for i=1 to n by 1 do
3. read: xi, yi
endfor
4. set sum = 0.0
5. for I = 1 to n by 1 do
6. set prod = 1.0
7. for j = 1 to n by 1 do
8. if (j ≠ 1) then
9. set prod = prod × (x - xj)/(x1 - xj)
endif
endfor
10. set sum = sum + yi × prod
endfor
11. write: sum as the interpolated value
12. exit
45
SOURCE CODE
/********** Lagrange's Interpolation Method***************/
#include <iostream>
#include <conio.h>
using namespace std;
int main( )
{
int n,i,j;
float L[10],sum=0,x[10],y[10],a;
cout<<"\n Enter number of data points: ";
cin>>n;
cout<<"\n Enter values of x and corresponding funtion values: ";
printf("\n x \t y \n");
for(i=0;i<n;i++)
cin>>x[i]>>y[i];
cout<<"\n Enter x value at which function is to be estimated: ";
cin>>a;
for(i=0;i<n;i++)
{
L[i]=1;
for(j=0;j<n;j++)
{
if(j!=i)
L[i]*=(a-x[j])/(x[i]-x[j]);
}
sum+=L[i]*y[i];
}
cout<<"\n The estimated value of function = "<<sum;
cout<<”\nF16EL34”;
return 0;
}
46
Input Output
47
REVIEW QUESTIONS
Answer:
Q.2 Can we use langrage’s interpolation formula for equally and unequally space data?
Answer:
Answer:
48
PRACTICAL#12
6
1
Objective: Write C++ program to evaluate 1 x
0
2
dx using Trapezoidal rule.
1. Read:n
2. For j =1 to n by 1 do
3. Read: xj, yj
4. Set sum = y1 + yn
5. for j = 2 to n - 1 by 1 do
6. Set sum = sum +2 × yj
endfor
7. Set i = (h/2) × sum
8. Write: i as the value of the integral
9. exit
49
SOURCE CODE:
//Trapezoidal Rule
#include<iostream>
#include<cmath>
#define F(x,y) (x+y)
using namespace std;
int main ()
{
float x[10],f[10],h,sum=0.0,I;
int n,i;
cout<<"\nEnter no. of sub-intervals : ";
cin>>n;
cout<<"\nEnter limits of integration : ";
cin>>x[0]>>x[n];
h=(x[n]-x[0])/n;
for(i=0;i<=n;i++)
x[i]=x[0]+i*h;
cout<<"\nEnter data points: \n"; cout<<"\n x\t f(x)\n";
for(i=0;i<=n;i++)
{cout<<x[i];
cin>>f[i];};
for(i=1;i<n;i++)
sum+=f[i];
I=(h/2.0)*(f[0]+2*sum+f[n]) ;
cout<<"\n Required result = "<<I;
cout<<”\nF16EL34”;
return 0;
}
50
Input Output
51
REVIEW QUESTIONS:
Answer:
52
PRACTICAL#13
6
1
Objective: Write C++ program to evaluate 1 x
0
2
dx using Simpson’s 1/3rd rule
Step:
1. Read: a,b,n
2. set h=(b-a) /n
4. set s2=0
5. set s4=0
7.set s4=s4+f(a+j×h)
end for
53
SOURCE CODE:
//Simpson's 1/3 Rule
#include<iostream>
#include<cmath>
#define f(x) (1/1+pow(x,2))
using namespace std;
int main ( )
{
float a, b, h, p, S1=0, S2=0, A;
int n;
cout<<"Number of sub-intervals = ";
cin>>n;
cout<<"Value of lower limit (a) = ";
cin>>a;
cout<<"Value of higher limit (b) = ";
cin>>b;
h=(b-a)/n;
for(p=(a+2*h);p<b;p=p+2*h)
{ S1=S1+f(p); }
for(p=(a+h);p<b;p=p+2*h)
{ S2=S2+f(p); }
A=h*( f(a)+f(b)+2*S1+4*S2)/3;
cout<<"\n Result of required Definite Integral by Simpson's 1/3 Rule is " <<A;
cout<<”\nF16EL34”;
return 0;
}
54
Input Output
55
REVIEW QUESTIONS:
56
PRACTICAL#14
8)set s2 = s2 + f(a + (j + 2) * h)
endfor
9) set i = (3h/8) * (s + 3 * s3 + 2 * s2)
57
SOURCE CODE:
//Simpson's 3/8th Rule
#include <iostream>
using namespace std;
int main ()
{
float a,b,h,p,S1=0,S2=0,A;
int n;
cout<<”Number of sub-intervals =”;
cin>>n;
cout<<”Value of lower limit (a) =”;
cin>>a;
cout<<”Value of higher limit (b) =”;
cin>>b;
h=(b-a)/n;
for(p=(a+3*h);p<b;p=(p+3*h))
{
S1=S1+f(p);
}
for(p=(a+h);p<b;p=(p+h))
{ S2=S2+f(p); }
A=3*h*(f(a)+f(b)-S1+3*S2)/8;
cout<<”\nResult of Required Definite Integral By Sampson’s 3/8 Rule is “<<A;
cout<<”\nF16EL34”;
return 0;
}
58
Input Output
59
Review Questions:
60
Q.5) Simpson’s 3/8th rule is applied on which point of parabola?
Answer:
PRACTICAL#15
Objective: Write C++ program to find an approximate value of y at x 0.5 using Euler’s
dy
method, given that x y and y 1 when x 0 .
dx
ALGORITM: EULER’s METHOD
Step
1. Read
2. Read:
3. Set:
4. Set:
5. Set:
6. Write:
7. While do
8. Set )
9. Set
10. Set I = I +1
11. Write
endwhile
12. Exit
61
SOURCE CODE
//Euler's Method
#include<iostream>
#include<cmath>
#define F(x,y)(pow(x,2)-y)
using namespace std;
int main ( )
{
float xo,x1,yo,y1,xn,yn,h;
cout<<"Enter xo,yo,xn,h";
cin>>xo>>yo>>xn>>h;
cout<<"\n X= "<<xo<<"\t\ty="<<yo;
y1=yo;
x1=xo;
while(x1<xn)
{
yn=y1+h*F(x1,y1);
x1=x1+h;
cout<<"\n X="<<x1<<"\t\ty="<<yn;
y1=yn;
}
Cout<<”\nF16EL34”;
return 0;
}
62
Input Output
63
REVIEW QUESTIONS:
Q.6) Write the equation for Euler’s method for finding approximate
solution?
Answer:
64
PRACTICAL#16
Object: Write C++ program to find an approximate value of y at x 0.5 using Modified Euler’s
dy
method, given that x y and y 1 when x 0 .
dx
SOURCE CODE:
//MOdified Euler's Method or Mid-point Method
#include<iostream>
#include<cmath>
#define f(x,y) (x+y)
using namespace std;
int main()
{
float xo,yo,x,y,h,k1,k;
int n,i;
cout<<"\nEnter the value of xo,yo";
cin>>xo>>yo;
cout<<"\nEnter the value of x";
cin>>x;
cout<<"\nEnter the value of h";
cin>>h;
n=(x-xo)/h;
for(i=1;i<=n+1;i++) {
k1=h*f(xo,yo);
k=h*f(xo+h/2,yo+k1/2);
y=yo+k;
xo=xo+h;
yo=y;
cout<<"\n\n y("<<xo<<")="<<y;
}
cout<<"\n\n\nF16EL34";
return 0; }
65
Input Output
66
REVIEW QUESTION:
Answer:
Q.1) Write the equation for Modified Euler’s method for finding
approximate solution?
Answer:
67
PRACTICAL#17
Object: Write C++ program to find an approximate value of y at x 0.4 using 4th order RK
dy y 2 x 2
method, given that and y 1 when x 0 .
dx y 2 x 2
Algorithm: 4th Order RK Method:
Step 1. Read: x1 , y1 , xf
2. read: n
3. set x = x1
4 . set y = y1
5. set i = 1
6. write: i, x, y
7. while ( x ≤ xf ) do
8. set s1 = f( x,y )
9. set s2 = f( x +h/2 , y+s1h/2 )
10. set s3 = f( x+ h/2 , y+s2h/2 )
11. set s4 = f( x+h, y+s3h )
12. set s =( s1+ 2s2 +2s3+s4 )/6
13. set y =y+h×s
14. set x = x+h
15. set i = i + 1
16. write: i,x,y
endwhile
17. exit
68
SOURCE CODE:
//Classical Runge-Kutta Method or RK4
#include<iostream>
#include<cmath>
#define f(x,y) (pow(x,2)-y)
using namespace std;
int main()
{
float xo,yo,x,y,h,k1,k2,k3, k4, k;
int n,i;
cout<<"\nEnter the value of xo,yo";
cin>>xo>>yo;
cout<<"\nEnter the value of x";
cin>>x;
cout<<"\nEnter the value of h";
cin>>h;
n=(x-xo)/h;
for(i=1;i<=n+1;i++)
{
k1=h*f(xo,yo);
k2=h*f(xo+0.5*h,yo+0.5*k1);
k3= h*f(xo+0.5*h,yo+0.5*k2);
k4 =h*f(xo+h,yo+k3);
k=(k1+2*k2+2*k3+k4)/6;
y=yo+k;
xo=xo+h;
yo=y;
cout<<"\n\n y("<<xo<<")="<<y;
}
cout<<"\n\n\nF16EL34";
return 0;
}
69
Input Output
70
REVIEW QUESTIONS:
71