SlideShare una empresa de Scribd logo
1 de 12
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado (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 a C++ TUTORIAL 10

Modificacion del programa
Modificacion del programaModificacion del programa
Modificacion del programa
Mario José
 

Similar a 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
 

Último

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
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
PECB
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
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
heathfieldcps1
 

Último (20)

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
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
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
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
 

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