SlideShare a Scribd company logo
1 of 12
Download to read offline
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

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
 
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
Raihan Bin-Mofidul
 
Travel management
Travel managementTravel management
Travel management
1Parimal2
 

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 (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 programa
Mario José
 

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

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 

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