SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
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-ee01083101premrings
 
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 StudyChris 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
 
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 ProgrammingUtsav Patel
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th StudyChris Ohk
 

Was ist angesagt? (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
 

Andere mochten auch

Andere mochten auch (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
 

Ähnlich wie RK4 Solves ODEs

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 Assignmentashikul akash
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsKandarp 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 manualUma 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 cppAlamgir Hossain
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1kkkseld
 
Computer Aided Manufacturing Design
Computer Aided Manufacturing DesignComputer Aided Manufacturing Design
Computer Aided Manufacturing DesignV Tripathi
 
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 BoFTimur Safin
 
Newton two Equation method
Newton two Equation  method Newton two Equation  method
Newton two Equation method shanto017
 

Ähnlich wie RK4 Solves ODEs (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
 

Kürzlich hochgeladen

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Kürzlich hochgeladen (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

RK4 Solves ODEs

  • 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