SlideShare a Scribd company logo
TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) 
QUESTION 1 
Write a C++ program using second order RK method with h=0.1 to find 
y(2.5) for 
= -xy2, y(2)=1 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double f(double x,double y) 
{ 
double func; 
func= -x*y*y; 
return func; 
} 
void main() 
{ 
float x,x0,xn,y,y0, h,exact,error,k1,k2; 
cout << "Enter the stepsize h: "; 
cin >> h; 
cout << "Enter x0,xn and y0: "; 
cin >> x0 >> xn >> y0; 
x=x0; 
y=y0; 
cout << "nXtYttExactttErrorn";
while(x < xn) 
{ 
k1=h*f(x,y); 
k2=h*f((x+h/2),(y + k1/2)); 
y= y+ k2; 
x=x+h; 
exact=2/(x*x -2); 
error= exact-y; 
cout << setprecision(1) <<fixed << x << "t"<< setprecision(6) << fixed << y << "t"; 
cout << setprecision(6) << exact << "t" << fabs(error) << endl; 
} 
} 
//Output: 
Enter the stepsize h: 0.1 
Enter x0,xn and y0: 2 2.5 1 
X Y Exact Error 
2.1 0.833950 0.829876 0.004074 
2.2 0.709463 0.704225 0.005238 
2.3 0.613199 0.607903 0.005296 
2.4 0.536859 0.531915 0.004944 
2.5 0.475051 0.470588 0.004462 
2.6 0.424136 0.420168 0.003967
TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) 
QUESTION 2 
Write a C++ function program using RK2 method to solve 
= sin(y) , 
with y(0)=1 from x=0 to 0.5 with step size h=0.1. 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double f(double(x),double(y)) 
{ 
return sin(y); 
} 
int main() 
{ 
long double y[100],x[100], k[100][100]; 
int n,i; 
float h; 
cout<<"Enter the value of x0: "; 
cin>>x[0]; 
cout<<"Enter The Value of y0: "; 
cin>>y[0]; 
cout<<"Enter the number of iterations: "; 
cin>>n; 
cout<<"Enter The Value of Step Size: "; 
cin>>h;
cout<<"nIterationstxtytK1ttK2"<<endl; 
for(i=1;i<=n;i++) 
{ 
k[1][i]= h*f(x[i-1],y[i-1]); 
k[2][i]= h*f(x[i-1]+h/2 ,y[i-1]+ (k[1][i])/2); 
y[i]=y[i-1]+ k[2][i]; 
x[i]=x[i-1]+h; 
} 
for(i=0;i<=n;i++) 
{ 
cout << i <<"tt"<< setprecision(1) << x[i]<<"t"; 
cout << setprecision(5) << y[i] << "t"; 
cout << setprecision(3)<< k[1][i] << "tt" << k[2][i] << endl; 
} 
return 0; 
} 
//Output: 
Enter the value of x0: 0 
Enter The Value of y0: 1 
Enter the number of iterations: 5 
Enter The Value of Step Size: 0.1 
Iterations x y K1 K2 
0 0 1 0.0841 0.0863 
1 0.1 1.0863 0.0885 0.0905 
2 0.2 1.1768 0.0923 0.094
TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) 
3 0.3 1.2708 0.0955 0.0968 
4 0.4 1.3677 0.0979 0.0988 
5 0.5 1.4665 
//Alternative way for question 2 
#include<iostream> 
#include<cmath> 
using namespace std; 
double f(double x,double y) 
{ 
double func; 
func= sin(y); 
return func; 
} 
void main() 
{ 
float x,x0,xn,y,y0,n, h,k1,k2; 
cout << "Enter no. of subintervals: "; 
cin >> n; 
cout << "Enter x0,xn and y0: "; 
cin >> x0 >> xn >> y0; 
h=(xn-x0)/n ;
x=x0; 
y=y0; 
cout << "nXttYn"; 
while(x<xn) 
{ 
k1=h*f(x,y); 
k2=h*f((x+h/2),(y + k1/2)); 
y= y+ k2; 
x=x+h; 
cout << x << "tt"<< y <<endl; 
} 
} 
//Output 
Enter no. of subintervals: 5 
Enter x0,xn and y0: 0 0.5 1 
X Y 
0.1 1.08635 
0.2 1.17681 
0.3 1.27082 
0.4 1.36766 
0.5 1.46647
TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) 
QUESTION 3 
Write a C++ program using fourth order RK method with h=0.1 to 
approximate y(1.5) for 
= 2xy, y(1)=1. Compute the errors using exact 
solution, y=ex2-1 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double f(double x,double y) 
{ 
double func; 
func= 2*x*y; 
return func; 
} 
void main() 
{ 
double x,x0,xn,y,y0, exact,error, h,k1,k2,k3,k4; 
cout << "Enter the stepsize h: "; 
cin >> h; 
cout << "Enter x0,xn and y0: "; 
cin >> x0 >> xn >> y0; 
x=x0; 
y=y0; 
cout << "nXtYtExacttErrorn";
while(x <xn) 
{ 
k1=f(x,y); 
k2=f((x+h/2),(y + k1*h/2)); 
k3=f((x+ h/2) ,(y+k2*h/2)); 
k4=f(x+h, y+h*k3); 
y= y+ (h/6)*(k1+2*k2+2*k3+k4); 
x=x+h; 
exact=exp(x*x -1); 
error= exact - y; 
cout << setprecision(1) << fixed << x << "t"<< setprecision(4) << 
fixed << y <<"t" ; 
cout << setprecision(4) <<fixed << exact << "t" << fabs(error) << endl; 
} 
} 
//Output: 
Enter the stepsize h: 0.1 
Enter x0,xn and y0: 1 1.5 1 
X Y Exact Error 
1.1 1.2337 1.2337 0.0000 
1.2 1.5527 1.5527 0.0000 
1.3 1.9937 1.9937 0.0000 
1.4 2.6116 2.6117 0.0001 
1.5 3.4902 3.4903 0.0001
TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) 
QUESTION 4 
Do Problem 1 with RK4. Compare the results between RK2 and RK4. 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double f(double x,double y) 
{ 
double func; 
func= -x*y*y; 
return func; 
} 
void main() 
{ 
double x,x0,xn,y,y0,h,exact,error,k1,k2,k3,k4; 
cout << "Enter the stepsize h: "; 
cin >> h; 
cout << "Enter x0,xn and y0: "; 
cin >> x0 >> xn >> y0; 
x=x0; 
y=y0; 
cout << "nXtYtExactttErrorn"; 
while(x <xn) 
{
k1=f(x,y); 
k2=f((x+h/2),(y + k1*h/2)); 
k3=f((x+ h/2) ,(y+k2*h/2)); 
k4=f(x+h, y+h*k3); 
y= y+ (h/6)*(k1+2*k2+2*k3+k4); 
x=x+h; 
exact= 2/(x*x-2); 
error= exact-y; 
cout << setprecision(1) << fixed << x << "t"<< setprecision(6) << 
fixed << y << "t"; 
cout <<setprecision(6) << exact << "t" << fabs(error) << endl; 
} 
} 
//Output: 
Enter the stepsize h: 0.1 
Enter x0,xn and y0: 2 2.5 1 
X Y Exact Error 
2.1 0.829885 0.829876 0.000010 
2.2 0.704237 0.704225 0.000011 
2.3 0.607914 0.607903 0.000011 
2.4 0.531924 0.531915 0.000010 
2.5 0.470596 0.470588 0.000008 
Thus, RK4 is better than RK2 since RK4 has smaller error difference than RK2 for y(2.5)
TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) 
QUESTION 5 
Write a C++ function program using classical RK4 method with h=0.2 to 
obtain y(1) for 
= y-x, y(0)=2. 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double f(double x,double y) 
{ 
double func; 
func= y-x; 
return func; 
} 
void main() 
{ 
double x,x0,xn,y,y0,h,k1,k2,k3,k4; 
cout << "Enter the stepsize h: "; 
cin >> h; 
cout << "Enter x0,xn and y0: "; 
cin >> x0 >> xn >> y0; 
x=x0; 
y=y0; 
cout << "nXtYn"; 
while(x <xn) 
{ 
k1=f(x,y);
k2=f((x+h/2),(y + k1*h/2)); 
k3=f((x+ h/2) ,(y+k2*h/2)); 
k4=f(x+h, y+h*k3); 
y= y+ (h/6)*(k1+2*k2+2*k3+k4); 
x=x+h; 
cout << setprecision(1) << fixed << x << "t"<< setprecision(6) << 
fixed << y << endl; 
} 
} 
//Output: 
Enter the stepsize h: 0.2 
Enter x0,xn and y0: 0 1 2 
X Y 
0.2 2.421400 
0.4 2.891818 
0.6 3.422106 
0.8 4.025521 
1.0 4.718251

More Related Content

What's hot

C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
Farhan Ab Rahman
 
New presentation oop
New presentation oopNew presentation oop
New presentation oop
Ch shampi Ch shampi
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
Bharat Kalia
 
Container adapters
Container adaptersContainer adapters
Container adapters
mohamed sikander
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
Germán Küber
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
mohamed sikander
 
Static and const members
Static and const membersStatic and const members
Static and const members
mohamed sikander
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Alex Penso Romero
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
Chris Ohk
 
Ee 3122 numerical methods and statistics sessional credit
Ee 3122 numerical methods and statistics sessional  creditEe 3122 numerical methods and statistics sessional  credit
Ee 3122 numerical methods and statistics sessional creditRaihan Bin-Mofidul
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
mohamed sikander
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
mohamed sikander
 
Travel management
Travel managementTravel management
Travel management1Parimal2
 
Numerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingNumerical Methods with Computer Programming
Numerical Methods with Computer Programming
Utsav Patel
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th Study
Chris Ohk
 

What's hot (20)

C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
New presentation oop
New presentation oopNew presentation oop
New presentation oop
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
C++ file
C++ fileC++ file
C++ file
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Oop1
Oop1Oop1
Oop1
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
C++ programs
C++ programsC++ programs
C++ programs
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Ee 3122 numerical methods and statistics sessional credit
Ee 3122 numerical methods and statistics sessional  creditEe 3122 numerical methods and statistics sessional  credit
Ee 3122 numerical methods and statistics sessional credit
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
Travel management
Travel managementTravel management
Travel management
 
Opp compile
Opp compileOpp compile
Opp compile
 
Numerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingNumerical Methods with Computer Programming
Numerical Methods with Computer Programming
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th Study
 

Viewers also liked

BSAD 372 SPRING 2017 CH 11
BSAD 372 SPRING 2017 CH 11BSAD 372 SPRING 2017 CH 11
BSAD 372 SPRING 2017 CH 11
Janice Robinson
 
BSAD 372 SPRING 2017 CH 1
BSAD 372 SPRING 2017 CH 1BSAD 372 SPRING 2017 CH 1
BSAD 372 SPRING 2017 CH 1
Janice Robinson
 
BSAD 372 SPRING 2017 CH 2
BSAD 372 SPRING 2017 CH 2BSAD 372 SPRING 2017 CH 2
BSAD 372 SPRING 2017 CH 2
Janice Robinson
 
BSAD 372 SPRING 2017 CH 4
BSAD 372 SPRING 2017 CH 4BSAD 372 SPRING 2017 CH 4
BSAD 372 SPRING 2017 CH 4
Janice Robinson
 
BSAD 372 SPRING 2017 CH 7
BSAD 372 SPRING 2017 CH 7BSAD 372 SPRING 2017 CH 7
BSAD 372 SPRING 2017 CH 7
Janice Robinson
 
BSAD 372 SPRING 2017 CH 3
BSAD 372 SPRING 2017 CH 3BSAD 372 SPRING 2017 CH 3
BSAD 372 SPRING 2017 CH 3
Janice Robinson
 
BSAD 372 SPRING 2017 CH 10
BSAD 372 SPRING 2017 CH 10BSAD 372 SPRING 2017 CH 10
BSAD 372 SPRING 2017 CH 10
Janice Robinson
 
BSAD 372 SPRING 2017 CH 5
BSAD 372 SPRING 2017 CH 5BSAD 372 SPRING 2017 CH 5
BSAD 372 SPRING 2017 CH 5
Janice Robinson
 

Viewers also liked (9)

BSAD 372 SPRING 2017 CH 11
BSAD 372 SPRING 2017 CH 11BSAD 372 SPRING 2017 CH 11
BSAD 372 SPRING 2017 CH 11
 
BSAD 372 SPRING 2017 CH 1
BSAD 372 SPRING 2017 CH 1BSAD 372 SPRING 2017 CH 1
BSAD 372 SPRING 2017 CH 1
 
BSAD 372 SPRING 2017 CH 2
BSAD 372 SPRING 2017 CH 2BSAD 372 SPRING 2017 CH 2
BSAD 372 SPRING 2017 CH 2
 
BSAD 372 SPRING 2017 CH 4
BSAD 372 SPRING 2017 CH 4BSAD 372 SPRING 2017 CH 4
BSAD 372 SPRING 2017 CH 4
 
Oxfam india
Oxfam indiaOxfam india
Oxfam india
 
BSAD 372 SPRING 2017 CH 7
BSAD 372 SPRING 2017 CH 7BSAD 372 SPRING 2017 CH 7
BSAD 372 SPRING 2017 CH 7
 
BSAD 372 SPRING 2017 CH 3
BSAD 372 SPRING 2017 CH 3BSAD 372 SPRING 2017 CH 3
BSAD 372 SPRING 2017 CH 3
 
BSAD 372 SPRING 2017 CH 10
BSAD 372 SPRING 2017 CH 10BSAD 372 SPRING 2017 CH 10
BSAD 372 SPRING 2017 CH 10
 
BSAD 372 SPRING 2017 CH 5
BSAD 372 SPRING 2017 CH 5BSAD 372 SPRING 2017 CH 5
BSAD 372 SPRING 2017 CH 5
 

Similar to C++ TUTORIAL 10

Modificacion del programa
Modificacion del programaModificacion del programa
Modificacion del programaMario José
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
kinan keshkeh
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
ashikul akash
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
Prianka Padmanaban
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
Prianka Padmanaban
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
Kandarp Tiwari
 
Interpolation graph c++
Interpolation graph c++Interpolation graph c++
Interpolation graph c++
rpiitcbme
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
Uma mohan
 
The International Journal of Engineering and Science (IJES)
The International Journal of Engineering and Science (IJES)The International Journal of Engineering and Science (IJES)
The International Journal of Engineering and Science (IJES)
theijes
 
Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cpp
Alamgir Hossain
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
kkkseld
 
No3
No3No3
Computer Aided Manufacturing Design
Computer Aided Manufacturing DesignComputer Aided Manufacturing Design
Computer Aided Manufacturing Design
V Tripathi
 
C programs
C programsC programs
C programs
Azaj Khan
 
Property-based testing
Property-based testingProperty-based testing
Property-based testing
Dmitriy Morozov
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
Timur Safin
 
Newton two Equation method
Newton two Equation  method Newton two Equation  method
Newton two Equation method
shanto017
 

Similar to C++ TUTORIAL 10 (20)

Modificacion del programa
Modificacion del programaModificacion del programa
Modificacion del programa
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Caropro
CaroproCaropro
Caropro
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
Interpolation graph c++
Interpolation graph c++Interpolation graph c++
Interpolation graph c++
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
The International Journal of Engineering and Science (IJES)
The International Journal of Engineering and Science (IJES)The International Journal of Engineering and Science (IJES)
The International Journal of Engineering and Science (IJES)
 
Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cpp
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
Ee
EeEe
Ee
 
C++ file
C++ fileC++ file
C++ file
 
No3
No3No3
No3
 
Computer Aided Manufacturing Design
Computer Aided Manufacturing DesignComputer Aided Manufacturing Design
Computer Aided Manufacturing Design
 
C programs
C programsC programs
C programs
 
Property-based testing
Property-based testingProperty-based testing
Property-based testing
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
 
Newton two Equation method
Newton two Equation  method Newton two Equation  method
Newton two Equation method
 

Recently uploaded

Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 

Recently uploaded (20)

Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 

C++ TUTORIAL 10

  • 1. TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 1 Write a C++ program using second order RK method with h=0.1 to find y(2.5) for = -xy2, y(2)=1 #include<iostream> #include<cmath> #include<iomanip> using namespace std; double f(double x,double y) { double func; func= -x*y*y; return func; } void main() { float x,x0,xn,y,y0, h,exact,error,k1,k2; cout << "Enter the stepsize h: "; cin >> h; cout << "Enter x0,xn and y0: "; cin >> x0 >> xn >> y0; x=x0; y=y0; cout << "nXtYttExactttErrorn";
  • 2. while(x < xn) { k1=h*f(x,y); k2=h*f((x+h/2),(y + k1/2)); y= y+ k2; x=x+h; exact=2/(x*x -2); error= exact-y; cout << setprecision(1) <<fixed << x << "t"<< setprecision(6) << fixed << y << "t"; cout << setprecision(6) << exact << "t" << fabs(error) << endl; } } //Output: Enter the stepsize h: 0.1 Enter x0,xn and y0: 2 2.5 1 X Y Exact Error 2.1 0.833950 0.829876 0.004074 2.2 0.709463 0.704225 0.005238 2.3 0.613199 0.607903 0.005296 2.4 0.536859 0.531915 0.004944 2.5 0.475051 0.470588 0.004462 2.6 0.424136 0.420168 0.003967
  • 3. TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 2 Write a C++ function program using RK2 method to solve = sin(y) , with y(0)=1 from x=0 to 0.5 with step size h=0.1. #include<iostream> #include<cmath> #include<iomanip> using namespace std; double f(double(x),double(y)) { return sin(y); } int main() { long double y[100],x[100], k[100][100]; int n,i; float h; cout<<"Enter the value of x0: "; cin>>x[0]; cout<<"Enter The Value of y0: "; cin>>y[0]; cout<<"Enter the number of iterations: "; cin>>n; cout<<"Enter The Value of Step Size: "; cin>>h;
  • 4. cout<<"nIterationstxtytK1ttK2"<<endl; for(i=1;i<=n;i++) { k[1][i]= h*f(x[i-1],y[i-1]); k[2][i]= h*f(x[i-1]+h/2 ,y[i-1]+ (k[1][i])/2); y[i]=y[i-1]+ k[2][i]; x[i]=x[i-1]+h; } for(i=0;i<=n;i++) { cout << i <<"tt"<< setprecision(1) << x[i]<<"t"; cout << setprecision(5) << y[i] << "t"; cout << setprecision(3)<< k[1][i] << "tt" << k[2][i] << endl; } return 0; } //Output: Enter the value of x0: 0 Enter The Value of y0: 1 Enter the number of iterations: 5 Enter The Value of Step Size: 0.1 Iterations x y K1 K2 0 0 1 0.0841 0.0863 1 0.1 1.0863 0.0885 0.0905 2 0.2 1.1768 0.0923 0.094
  • 5. TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) 3 0.3 1.2708 0.0955 0.0968 4 0.4 1.3677 0.0979 0.0988 5 0.5 1.4665 //Alternative way for question 2 #include<iostream> #include<cmath> using namespace std; double f(double x,double y) { double func; func= sin(y); return func; } void main() { float x,x0,xn,y,y0,n, h,k1,k2; cout << "Enter no. of subintervals: "; cin >> n; cout << "Enter x0,xn and y0: "; cin >> x0 >> xn >> y0; h=(xn-x0)/n ;
  • 6. x=x0; y=y0; cout << "nXttYn"; while(x<xn) { k1=h*f(x,y); k2=h*f((x+h/2),(y + k1/2)); y= y+ k2; x=x+h; cout << x << "tt"<< y <<endl; } } //Output Enter no. of subintervals: 5 Enter x0,xn and y0: 0 0.5 1 X Y 0.1 1.08635 0.2 1.17681 0.3 1.27082 0.4 1.36766 0.5 1.46647
  • 7. TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 3 Write a C++ program using fourth order RK method with h=0.1 to approximate y(1.5) for = 2xy, y(1)=1. Compute the errors using exact solution, y=ex2-1 #include<iostream> #include<cmath> #include<iomanip> using namespace std; double f(double x,double y) { double func; func= 2*x*y; return func; } void main() { double x,x0,xn,y,y0, exact,error, h,k1,k2,k3,k4; cout << "Enter the stepsize h: "; cin >> h; cout << "Enter x0,xn and y0: "; cin >> x0 >> xn >> y0; x=x0; y=y0; cout << "nXtYtExacttErrorn";
  • 8. while(x <xn) { k1=f(x,y); k2=f((x+h/2),(y + k1*h/2)); k3=f((x+ h/2) ,(y+k2*h/2)); k4=f(x+h, y+h*k3); y= y+ (h/6)*(k1+2*k2+2*k3+k4); x=x+h; exact=exp(x*x -1); error= exact - y; cout << setprecision(1) << fixed << x << "t"<< setprecision(4) << fixed << y <<"t" ; cout << setprecision(4) <<fixed << exact << "t" << fabs(error) << endl; } } //Output: Enter the stepsize h: 0.1 Enter x0,xn and y0: 1 1.5 1 X Y Exact Error 1.1 1.2337 1.2337 0.0000 1.2 1.5527 1.5527 0.0000 1.3 1.9937 1.9937 0.0000 1.4 2.6116 2.6117 0.0001 1.5 3.4902 3.4903 0.0001
  • 9. TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 4 Do Problem 1 with RK4. Compare the results between RK2 and RK4. #include<iostream> #include<cmath> #include<iomanip> using namespace std; double f(double x,double y) { double func; func= -x*y*y; return func; } void main() { double x,x0,xn,y,y0,h,exact,error,k1,k2,k3,k4; cout << "Enter the stepsize h: "; cin >> h; cout << "Enter x0,xn and y0: "; cin >> x0 >> xn >> y0; x=x0; y=y0; cout << "nXtYtExactttErrorn"; while(x <xn) {
  • 10. k1=f(x,y); k2=f((x+h/2),(y + k1*h/2)); k3=f((x+ h/2) ,(y+k2*h/2)); k4=f(x+h, y+h*k3); y= y+ (h/6)*(k1+2*k2+2*k3+k4); x=x+h; exact= 2/(x*x-2); error= exact-y; cout << setprecision(1) << fixed << x << "t"<< setprecision(6) << fixed << y << "t"; cout <<setprecision(6) << exact << "t" << fabs(error) << endl; } } //Output: Enter the stepsize h: 0.1 Enter x0,xn and y0: 2 2.5 1 X Y Exact Error 2.1 0.829885 0.829876 0.000010 2.2 0.704237 0.704225 0.000011 2.3 0.607914 0.607903 0.000011 2.4 0.531924 0.531915 0.000010 2.5 0.470596 0.470588 0.000008 Thus, RK4 is better than RK2 since RK4 has smaller error difference than RK2 for y(2.5)
  • 11. TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 5 Write a C++ function program using classical RK4 method with h=0.2 to obtain y(1) for = y-x, y(0)=2. #include<iostream> #include<cmath> #include<iomanip> using namespace std; double f(double x,double y) { double func; func= y-x; return func; } void main() { double x,x0,xn,y,y0,h,k1,k2,k3,k4; cout << "Enter the stepsize h: "; cin >> h; cout << "Enter x0,xn and y0: "; cin >> x0 >> xn >> y0; x=x0; y=y0; cout << "nXtYn"; while(x <xn) { k1=f(x,y);
  • 12. k2=f((x+h/2),(y + k1*h/2)); k3=f((x+ h/2) ,(y+k2*h/2)); k4=f(x+h, y+h*k3); y= y+ (h/6)*(k1+2*k2+2*k3+k4); x=x+h; cout << setprecision(1) << fixed << x << "t"<< setprecision(6) << fixed << y << endl; } } //Output: Enter the stepsize h: 0.2 Enter x0,xn and y0: 0 1 2 X Y 0.2 2.421400 0.4 2.891818 0.6 3.422106 0.8 4.025521 1.0 4.718251