SlideShare a Scribd company logo
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
QUESTION 1 
Write a C++ program using Taylor’s series method to solve the equation 
=3x+y2 to approximate y when x=0.1, given that y=1 when x=0. 
#include <iostream> 
#include <cmath> 
#include <iomanip> 
using namespace std; 
int main() 
{ 
double x0=0,y0=1 , h=0.1,y1,y2,y3,y4, y; 
y1=3*x0 + y0*y0; 
y2=3+ 2*y0*y1; 
y3=2*y1*y1 + 2*y0*y2; 
y4=6*y1*y2 + 2*y0*y3; 
y= y0+ (y1*h) + (y2*pow(h,2))/2 + (y3*pow(h,3))/6 + (y4*pow(h,4))/24; 
cout << "The value of y when x=0.1 is " << setprecision(5) <<fixed << y << endl; 
return 0; 
} 
//Output: 
The value of y when x=0.1 is 1.12722 
//Alternative way for question 1 
#include <iostream> 
#include <cmath> 
#include <iomanip> 
using namespace std; 
#define h 0.1 
double f(double x,double y) 
{ 
return 3*x + y*y; 
} 
double ff(double x,double y) 
{
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
return 3 + 2*y*f(x,y); 
} 
double fff(double x, double y) 
{ 
return 2*f(x,y)*f(x,y) + 2*y*ff(x,y); 
} 
double ffff(double x, double y) 
{ 
return 6*f(x,y)*ff(x,y) + 2*y*fff(x,y); 
} 
void taylor(double x,double y[]) 
{ 
int i; 
cout << "ittxtty " << endl; 
for ( i=0;i<=4;i++) 
{ 
y[i+1]=y[i]+h*f(x,y[i] ) + (h*h/2)*ff(x,y[i]) + (pow(h,3)/6)*fff(x,y[i])+ 
(pow(h,4)/24)*ffff(x,y[i]) ; 
cout << i << "tt" << setprecision(1)<<fixed << x << "tt" << setprecision(5) << 
fixed << y[i] << endl; 
x= x+ h; 
} 
} 
int main() 
{ 
double x0,y[100]; 
cout << "Enter x0 and y0 : "; 
cin >> x0 >> y[0]; 
taylor(x0,y); 
return 0; 
} 
//Output: 
Enter x0 and y0 : 0 1 
i x y 
0 0.0 1.00000 
1 0.1 1.12722 
2 0.2 1.32071
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
QUESTION 2 
Write a C++ program using Taylor’s series method to solve 
+4y=x2 , 
y(0)=1 to approximate y(0.2). 
#include <iostream> 
#include <cmath> 
#include <iomanip> 
using namespace std; 
int main() 
{ 
double x0=0,y0=1 , h=0.2,y1,y2,y3,y4, y; 
y1= x0*x0 -4*y0; 
y2= 2*x0- 4*y1; 
y3= 2- 8*x0+16*y1; // y3= 2- 4*y2; 
y4= -8+32*x0 -64*y1; // y4= -4*y3; 
y= y0+ (y1*h) + (y2*pow(h,2))/2 + (y3*pow(h,3))/6 + (y4*pow(h,4))/24; 
cout << "The value of y when x=0.2 is " << setprecision(5) <<fixed << y << endl; 
return 0; 
} 
//Output: 
The value of y when x=0.2 is 0.45387
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
//Alternative for question 2 
#include <iostream> 
#include <cmath> 
#include <iomanip> 
using namespace std; 
#define h 0.2 
double f(double x,double y) 
{ 
return x*x - 4*y; 
} 
double ff(double x,double y) 
{ 
return 2*x - 4*f(x,y); 
} 
double fff(double x, double y) 
{ 
return 2- 8*x + 16*f(x,y); 
} 
double ffff(double x, double y) 
{ 
return -8 + 32*x- 64*f(x,y); 
} 
void taylor(double x,double y[]) 
{ 
int i;
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
cout << "ittxtty " << endl; 
for ( i=0;i<=2;i++) 
{ 
y[i+1]=y[i]+h*f(x,y[i] ) + (h*h/2)*ff(x,y[i]) + (pow(h,3)/6)*fff(x,y[i])+ 
(pow(h,4)/24)*ffff(x,y[i]) ; 
cout << i << "tt" << setprecision(1)<<fixed << x << "tt" << setprecision(5) << fixed 
<< y[i] << endl; 
x= x+ h; 
} 
} 
int main() 
{ 
double x0,y[100]; 
cout << "Enter x0 and y0 : "; 
cin >> x0 >> y[0]; 
taylor(x0,y); 
return 0; 
} 
//Output: 
Enter x0 and y0 : 0 1 
i x y 
0 0.0 1.00000 
1 0.2 0.45387 
2 0.4 0.21894
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
QUESTION 3 
Write a C++ program to solve 
= -xy2 , 푦(2)=1 in the interval 2< x <3 
with h=0.1 using Euler’s method. Compare the results with exact 
solution from 푦= 2/(x2 -2) 
#include<iostream> 
#include <cmath> 
#include<iomanip> 
using namespace std; 
#define F(x,y) -x*y*y 
void main() 
{ 
double y0,y,x,x0,xn,h, exact,error; 
cout <<"Enter the value of range(x0 and xn): "; 
cin >> x0 >> xn; 
cout << "Enter the value of y0: "; 
cin >> y0; 
cout <<"Enter the h: "; 
cin >> h; 
cout << "nnx0= "<< x0 << "tt" << "y0= " << y0; 
x=x0; 
y=y0; 
while(x<xn) 
{
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
y= y + h * F(x,y); 
x=x+h; 
exact = 2/(x*x-2); 
error= exact-y; 
cout << "nx= " << setprecision(1) <<fixed << x << "t"; 
cout << setprecision(4) <<fixed << exact<< "t" <<setprecision(4) <<fixed 
<< y << "t"; 
cout <<setprecision(4) << fixed << fabs(error) << endl; 
} 
} 
//Output: 
Enter the value of range(x0 and xn): 2 3 
Enter the value of y0: 1 
Enter the h: 0.1 
x0= 2 y0= 1 
x= 2.1 0.8299 0.8000 0.0299 
x= 2.2 0.7042 0.6656 0.0386 
x= 2.3 0.6079 0.5681 0.0398 
x= 2.4 0.5319 0.4939 0.0380 
x= 2.5 0.4706 0.4354 0.0352 
x= 2.6 0.4202 0.3880 0.0322 
x= 2.7 0.3781 0.3488 0.0292
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
x= 2.8 0.3425 0.3160 0.0265 
x= 2.9 0.3120 0.2880 0.0240 
x= 3.0 0.2857 0.2640 0.0217 
//Alternative way for question no 3 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
//Given dy/dx 
float f(float(x),float(y)) 
{ 
return (-x*y*y); 
} 
int main() 
{ 
double y[100],x[100], exact,error,percent_error; 
int n,i; 
float h; 
//Entering the initial values of x & y 
cout<<"Enter the value of x0: "; 
cin>>x[0]; 
cout<<"Enter The Value of y0: "; 
cin>>y[0];
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
cout<<"Enter the number of Iterations: "; 
cin>>n; 
cout<<"Enter The Value of Step Size: "; 
cin>>h; 
cout<<"nIterationstxtytExact valuetErrort Percentage Error"<<endl; 
//Calculating the value of x 
for(i=1;i<=n;i++) 
{ 
x[i]=x[i-1]+h; 
} 
//Calculating the value of y 
for(i=1;i<=n;i++) 
{ 
y[i]=y[i-1]+(h*f(x[i-1],y[i-1])); 
} 
//Printing result 
for(i=0;i<=n;i++) 
{ 
exact = 2/(x[i]*x[i]-2); 
error= exact-y[i]; 
percent_error= (fabs(error)/exact)*100; 
cout << i<<"tt"<< setprecision(2) <<x[i]<<"t"; 
cout << setprecision(4)<< y[i] << "t" << exact << "tt" ; 
cout << setprecision(4) <<fixed << fabs(error) << "t ";
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
cout << setprecision(5) << percent_error << endl; 
} 
return 0; 
} 
//Output: 
Enter the value of x0: 2 
Enter The Value of y0: 1 
Enter the number of Iterations: 10 
Enter The Value of Step Size: 0.1 
Iterations x y Exact value Error Percentage Error 
0 2 1 1 0.0000 0.00000 
1 2.10 0.8000 0.8299 0.0299 3.60000 
2 2.20 0.6656 0.7042 0.0386 5.48480 
3 2.30 0.5681 0.6079 0.0398 6.54182 
4 2.40 0.4939 0.5319 0.0380 7.14753 
5 2.50 0.4354 0.4706 0.0352 7.48768 
6 2.60 0.3880 0.4202 0.0322 7.66332 
7 2.70 0.3488 0.3781 0.0292 7.73341 
8 2.80 0.3160 0.3425 0.0265 7.73413 
9 2.90 0.2880 0.3120 0.0240 7.68862 
10 3.00 0.2640 0.2857 0.0217 7.61210
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
QUESTION 4 
Write a C++ program to find y(1) from 
= x+y , y(0)=1 using Euler’s 
method by using h=0.2. The exact solution is y=-1-x+2ex 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
//Given dy/dx 
float f(float(x),float(y)) 
{ 
return (x+y); 
} 
int main() 
{ 
double y[100],x[100], exact,error,percent_error; 
int n,i; 
float h; 
//Entering the initial values of x & y 
cout<<"Enter the value of x0: "; 
cin>>x[0]; 
cout<<"Enter The Value of y0: "; 
cin>>y[0]; 
cout<<"Enter the number of Iterations: ";
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
cin>>n; 
cout<<"Enter The Value of Step Size: "; 
cin>>h; 
cout<<"nIterationstxtytExact valuetErrort Percentage Error"<<endl; 
//Calculating the value of x 
for(i=1;i<=n;i++) 
{ 
x[i]=x[i-1]+h; 
} 
//Calculating the value of y 
for(i=1;i<=n;i++) 
{ 
y[i]=y[i-1]+(h*f(x[i-1],y[i-1])); 
} 
//Printing result 
for(i=0;i<=n;i++) 
{ 
exact = -1-x[i] +2*exp(x[i]); 
error= exact-y[i]; 
percent_error= (fabs(error)/exact)*100; 
cout << i<<"tt"<< setprecision(1) <<x[i]<<"t"; 
cout << setprecision(4)<< y[i] << "t" << exact << "tt" ;
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
cout << setprecision(4) <<fixed << fabs(error) << "t "; 
cout << setprecision(5) << percent_error << 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.2 
Iterations x y Exact value Error Percentage Error 
0 0 1 1 0.0000 0.00000 
1 0.2 1.2000 1.2428 0.0428 3.44427 
2 0.4 1.4800 1.5836 0.1036 6.54497 
3 0.6 1.8560 2.0442 0.1882 9.20821 
4 0.8 2.3472 2.6511 0.3039 11.46256 
5 1.0 2.9766 3.4366 0.4599 13.38324 
QUESTION 5 
Write a C++ program to solve 
= -2xy2, y(0)=1 in the interval 
0≤ x ≤0.5 with h=0.1 using Euler’s method. The exact value is 
y= 1/(x2+1) 
#include<iostream> 
#include<cmath> 
#include<iomanip>
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
using namespace std; 
//Given dy/dx 
float f(float(x),float(y)) 
{ 
return (-2*x*y*y); 
} 
int main() 
{ 
double y[100],x[100], exact,error,percent_error; 
int n,i; 
float h; 
//Entering the initial values of x & y 
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<<"nIterationstxtytExact valuetErrort Percentage Error"<<endl;
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
//Calculating the value of x 
for(i=1;i<=n;i++) 
{ 
x[i]=x[i-1]+h; 
} 
//Calculating the value of y 
for(i=1;i<=n;i++) 
{ 
y[i]=y[i-1]+(h*f(x[i-1],y[i-1])); 
} 
//Printing result 
for(i=0;i<=n;i++) 
{ 
exact = (1/(x[i]*x[i]+1)); 
error= exact-y[i]; 
percent_error= (fabs(error)/exact)*100; 
cout << i<<"tt"<< setprecision(1) <<x[i]<<"t"; 
cout << setprecision(4)<< y[i] << "t" << exact << "tt" ; 
cout << setprecision(4) <<fixed << fabs(error) << "t "; 
cout << setprecision(5) << percent_error << endl; 
} 
return 0; 
}
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
//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 Exact value Error Percentage Error 
0 0 1 1 0.0000 0.00000 
1 0.1 1.0000 0.9901 0.0099 1.00000 
2 0.2 0.9800 0.9615 0.0185 1.92000 
3 0.3 0.9416 0.9174 0.0242 2.63266 
4 0.4 0.8884 0.8621 0.0263 3.05314 
5 0.5 0.8253 0.8000 0.0253 3.15629 
QUESTION 6 
Write a C++ program to solve 
= x+y2 with y(1)=0 at x=1.3 using 
Euler’s Method 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
//Given dy/dx 
float f(float(x),float(y)) 
{ 
return (x+ y*y); 
}
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
int main() 
{ 
double y[100],x[100]; 
int n,i; 
float h; 
//Entering the initial values of x & y 
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<<"nIterationstxty"<<endl; 
//Calculating the value of x 
for(i=1;i<=n;i++) 
{ 
x[i]=x[i-1]+h; 
} 
//Calculating the value of y 
for(i=1;i<=n;i++) 
{
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) 
y[i]=y[i-1]+(h*f(x[i-1],y[i-1])); 
} 
//Printing result 
for(i=0;i<=n;i++) 
{ 
cout << i<<"tt"<< setprecision(2) <<x[i]<< "t" << setprecision(5)<< y[i] << 
endl; 
} 
return 0; 
} 
//Output: 
Enter the value of x0: 1 
Enter The Value of y0: 0 
Enter the number of Iterations: 3 
Enter The Value of Step Size: 0.1 
Iterations x y 
0 1 0 
1 1.1 0.1 
2 1.2 0.211 
3 1.3 0.33545

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
 
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
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
Bharat Kalia
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
Germán Küber
 
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
 
Container adapters
Container adaptersContainer adapters
Container adapters
mohamed sikander
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
mohamed sikander
 
Travel management
Travel managementTravel management
Travel management1Parimal2
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
Chris Ohk
 
Static and const members
Static and const membersStatic and const members
Static and const members
mohamed sikander
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
mohamed sikander
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
mohamed sikander
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
Chris Ohk
 
Go a crash course
Go   a crash courseGo   a crash course
Go a crash course
Eleanor McHugh
 
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
 

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
 
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
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
C++ file
C++ fileC++ file
C++ file
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
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))
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
C++ programs
C++ programsC++ programs
C++ programs
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
Travel management
Travel managementTravel management
Travel management
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
Opp compile
Opp compileOpp compile
Opp compile
 
Oop1
Oop1Oop1
Oop1
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Go a crash course
Go   a crash courseGo   a crash course
Go a crash course
 
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
 

Similar to C++ TUTORIAL 9

Computer graphics
Computer graphics   Computer graphics
Computer graphics
Prianka Padmanaban
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
Prianka Padmanaban
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
Uma mohan
 
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
 
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
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
AAlha PaiKra
 
Property-based testing
Property-based testingProperty-based testing
Property-based testing
Dmitriy Morozov
 
Final DAA_prints.pdf
Final DAA_prints.pdfFinal DAA_prints.pdf
Final DAA_prints.pdf
Yashpatel821746
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
Review questions and answers
Review questions and answersReview questions and answers
Review questions and answers
IIUM
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
ashikul akash
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
Yung-Yu Chen
 
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
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
Abdullah Al Shiam
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
Akira Maruoka
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
Svet Ivantchev
 

Similar to C++ TUTORIAL 9 (20)

Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
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
 
C++ file
C++ fileC++ file
C++ file
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Property-based testing
Property-based testingProperty-based testing
Property-based testing
 
Final DAA_prints.pdf
Final DAA_prints.pdfFinal DAA_prints.pdf
Final DAA_prints.pdf
 
7720
77207720
7720
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
Review questions and answers
Review questions and answersReview questions and answers
Review questions and answers
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
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
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Caropro
CaroproCaropro
Caropro
 

Recently uploaded

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
 
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
 
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
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
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
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
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
 
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
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
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
 

Recently uploaded (20)

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
 
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
 
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
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
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
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
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
 
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
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
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 Á...
 

C++ TUTORIAL 9

  • 1. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 1 Write a C++ program using Taylor’s series method to solve the equation =3x+y2 to approximate y when x=0.1, given that y=1 when x=0. #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double x0=0,y0=1 , h=0.1,y1,y2,y3,y4, y; y1=3*x0 + y0*y0; y2=3+ 2*y0*y1; y3=2*y1*y1 + 2*y0*y2; y4=6*y1*y2 + 2*y0*y3; y= y0+ (y1*h) + (y2*pow(h,2))/2 + (y3*pow(h,3))/6 + (y4*pow(h,4))/24; cout << "The value of y when x=0.1 is " << setprecision(5) <<fixed << y << endl; return 0; } //Output: The value of y when x=0.1 is 1.12722 //Alternative way for question 1 #include <iostream> #include <cmath> #include <iomanip> using namespace std; #define h 0.1 double f(double x,double y) { return 3*x + y*y; } double ff(double x,double y) {
  • 2. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) return 3 + 2*y*f(x,y); } double fff(double x, double y) { return 2*f(x,y)*f(x,y) + 2*y*ff(x,y); } double ffff(double x, double y) { return 6*f(x,y)*ff(x,y) + 2*y*fff(x,y); } void taylor(double x,double y[]) { int i; cout << "ittxtty " << endl; for ( i=0;i<=4;i++) { y[i+1]=y[i]+h*f(x,y[i] ) + (h*h/2)*ff(x,y[i]) + (pow(h,3)/6)*fff(x,y[i])+ (pow(h,4)/24)*ffff(x,y[i]) ; cout << i << "tt" << setprecision(1)<<fixed << x << "tt" << setprecision(5) << fixed << y[i] << endl; x= x+ h; } } int main() { double x0,y[100]; cout << "Enter x0 and y0 : "; cin >> x0 >> y[0]; taylor(x0,y); return 0; } //Output: Enter x0 and y0 : 0 1 i x y 0 0.0 1.00000 1 0.1 1.12722 2 0.2 1.32071
  • 3. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 2 Write a C++ program using Taylor’s series method to solve +4y=x2 , y(0)=1 to approximate y(0.2). #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double x0=0,y0=1 , h=0.2,y1,y2,y3,y4, y; y1= x0*x0 -4*y0; y2= 2*x0- 4*y1; y3= 2- 8*x0+16*y1; // y3= 2- 4*y2; y4= -8+32*x0 -64*y1; // y4= -4*y3; y= y0+ (y1*h) + (y2*pow(h,2))/2 + (y3*pow(h,3))/6 + (y4*pow(h,4))/24; cout << "The value of y when x=0.2 is " << setprecision(5) <<fixed << y << endl; return 0; } //Output: The value of y when x=0.2 is 0.45387
  • 4. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) //Alternative for question 2 #include <iostream> #include <cmath> #include <iomanip> using namespace std; #define h 0.2 double f(double x,double y) { return x*x - 4*y; } double ff(double x,double y) { return 2*x - 4*f(x,y); } double fff(double x, double y) { return 2- 8*x + 16*f(x,y); } double ffff(double x, double y) { return -8 + 32*x- 64*f(x,y); } void taylor(double x,double y[]) { int i;
  • 5. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) cout << "ittxtty " << endl; for ( i=0;i<=2;i++) { y[i+1]=y[i]+h*f(x,y[i] ) + (h*h/2)*ff(x,y[i]) + (pow(h,3)/6)*fff(x,y[i])+ (pow(h,4)/24)*ffff(x,y[i]) ; cout << i << "tt" << setprecision(1)<<fixed << x << "tt" << setprecision(5) << fixed << y[i] << endl; x= x+ h; } } int main() { double x0,y[100]; cout << "Enter x0 and y0 : "; cin >> x0 >> y[0]; taylor(x0,y); return 0; } //Output: Enter x0 and y0 : 0 1 i x y 0 0.0 1.00000 1 0.2 0.45387 2 0.4 0.21894
  • 6. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 3 Write a C++ program to solve = -xy2 , 푦(2)=1 in the interval 2< x <3 with h=0.1 using Euler’s method. Compare the results with exact solution from 푦= 2/(x2 -2) #include<iostream> #include <cmath> #include<iomanip> using namespace std; #define F(x,y) -x*y*y void main() { double y0,y,x,x0,xn,h, exact,error; cout <<"Enter the value of range(x0 and xn): "; cin >> x0 >> xn; cout << "Enter the value of y0: "; cin >> y0; cout <<"Enter the h: "; cin >> h; cout << "nnx0= "<< x0 << "tt" << "y0= " << y0; x=x0; y=y0; while(x<xn) {
  • 7. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) y= y + h * F(x,y); x=x+h; exact = 2/(x*x-2); error= exact-y; cout << "nx= " << setprecision(1) <<fixed << x << "t"; cout << setprecision(4) <<fixed << exact<< "t" <<setprecision(4) <<fixed << y << "t"; cout <<setprecision(4) << fixed << fabs(error) << endl; } } //Output: Enter the value of range(x0 and xn): 2 3 Enter the value of y0: 1 Enter the h: 0.1 x0= 2 y0= 1 x= 2.1 0.8299 0.8000 0.0299 x= 2.2 0.7042 0.6656 0.0386 x= 2.3 0.6079 0.5681 0.0398 x= 2.4 0.5319 0.4939 0.0380 x= 2.5 0.4706 0.4354 0.0352 x= 2.6 0.4202 0.3880 0.0322 x= 2.7 0.3781 0.3488 0.0292
  • 8. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) x= 2.8 0.3425 0.3160 0.0265 x= 2.9 0.3120 0.2880 0.0240 x= 3.0 0.2857 0.2640 0.0217 //Alternative way for question no 3 #include<iostream> #include<cmath> #include<iomanip> using namespace std; //Given dy/dx float f(float(x),float(y)) { return (-x*y*y); } int main() { double y[100],x[100], exact,error,percent_error; int n,i; float h; //Entering the initial values of x & y cout<<"Enter the value of x0: "; cin>>x[0]; cout<<"Enter The Value of y0: "; cin>>y[0];
  • 9. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) cout<<"Enter the number of Iterations: "; cin>>n; cout<<"Enter The Value of Step Size: "; cin>>h; cout<<"nIterationstxtytExact valuetErrort Percentage Error"<<endl; //Calculating the value of x for(i=1;i<=n;i++) { x[i]=x[i-1]+h; } //Calculating the value of y for(i=1;i<=n;i++) { y[i]=y[i-1]+(h*f(x[i-1],y[i-1])); } //Printing result for(i=0;i<=n;i++) { exact = 2/(x[i]*x[i]-2); error= exact-y[i]; percent_error= (fabs(error)/exact)*100; cout << i<<"tt"<< setprecision(2) <<x[i]<<"t"; cout << setprecision(4)<< y[i] << "t" << exact << "tt" ; cout << setprecision(4) <<fixed << fabs(error) << "t ";
  • 10. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) cout << setprecision(5) << percent_error << endl; } return 0; } //Output: Enter the value of x0: 2 Enter The Value of y0: 1 Enter the number of Iterations: 10 Enter The Value of Step Size: 0.1 Iterations x y Exact value Error Percentage Error 0 2 1 1 0.0000 0.00000 1 2.10 0.8000 0.8299 0.0299 3.60000 2 2.20 0.6656 0.7042 0.0386 5.48480 3 2.30 0.5681 0.6079 0.0398 6.54182 4 2.40 0.4939 0.5319 0.0380 7.14753 5 2.50 0.4354 0.4706 0.0352 7.48768 6 2.60 0.3880 0.4202 0.0322 7.66332 7 2.70 0.3488 0.3781 0.0292 7.73341 8 2.80 0.3160 0.3425 0.0265 7.73413 9 2.90 0.2880 0.3120 0.0240 7.68862 10 3.00 0.2640 0.2857 0.0217 7.61210
  • 11. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 4 Write a C++ program to find y(1) from = x+y , y(0)=1 using Euler’s method by using h=0.2. The exact solution is y=-1-x+2ex #include<iostream> #include<cmath> #include<iomanip> using namespace std; //Given dy/dx float f(float(x),float(y)) { return (x+y); } int main() { double y[100],x[100], exact,error,percent_error; int n,i; float h; //Entering the initial values of x & y cout<<"Enter the value of x0: "; cin>>x[0]; cout<<"Enter The Value of y0: "; cin>>y[0]; cout<<"Enter the number of Iterations: ";
  • 12. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) cin>>n; cout<<"Enter The Value of Step Size: "; cin>>h; cout<<"nIterationstxtytExact valuetErrort Percentage Error"<<endl; //Calculating the value of x for(i=1;i<=n;i++) { x[i]=x[i-1]+h; } //Calculating the value of y for(i=1;i<=n;i++) { y[i]=y[i-1]+(h*f(x[i-1],y[i-1])); } //Printing result for(i=0;i<=n;i++) { exact = -1-x[i] +2*exp(x[i]); error= exact-y[i]; percent_error= (fabs(error)/exact)*100; cout << i<<"tt"<< setprecision(1) <<x[i]<<"t"; cout << setprecision(4)<< y[i] << "t" << exact << "tt" ;
  • 13. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) cout << setprecision(4) <<fixed << fabs(error) << "t "; cout << setprecision(5) << percent_error << 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.2 Iterations x y Exact value Error Percentage Error 0 0 1 1 0.0000 0.00000 1 0.2 1.2000 1.2428 0.0428 3.44427 2 0.4 1.4800 1.5836 0.1036 6.54497 3 0.6 1.8560 2.0442 0.1882 9.20821 4 0.8 2.3472 2.6511 0.3039 11.46256 5 1.0 2.9766 3.4366 0.4599 13.38324 QUESTION 5 Write a C++ program to solve = -2xy2, y(0)=1 in the interval 0≤ x ≤0.5 with h=0.1 using Euler’s method. The exact value is y= 1/(x2+1) #include<iostream> #include<cmath> #include<iomanip>
  • 14. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) using namespace std; //Given dy/dx float f(float(x),float(y)) { return (-2*x*y*y); } int main() { double y[100],x[100], exact,error,percent_error; int n,i; float h; //Entering the initial values of x & y 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<<"nIterationstxtytExact valuetErrort Percentage Error"<<endl;
  • 15. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) //Calculating the value of x for(i=1;i<=n;i++) { x[i]=x[i-1]+h; } //Calculating the value of y for(i=1;i<=n;i++) { y[i]=y[i-1]+(h*f(x[i-1],y[i-1])); } //Printing result for(i=0;i<=n;i++) { exact = (1/(x[i]*x[i]+1)); error= exact-y[i]; percent_error= (fabs(error)/exact)*100; cout << i<<"tt"<< setprecision(1) <<x[i]<<"t"; cout << setprecision(4)<< y[i] << "t" << exact << "tt" ; cout << setprecision(4) <<fixed << fabs(error) << "t "; cout << setprecision(5) << percent_error << endl; } return 0; }
  • 16. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) //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 Exact value Error Percentage Error 0 0 1 1 0.0000 0.00000 1 0.1 1.0000 0.9901 0.0099 1.00000 2 0.2 0.9800 0.9615 0.0185 1.92000 3 0.3 0.9416 0.9174 0.0242 2.63266 4 0.4 0.8884 0.8621 0.0263 3.05314 5 0.5 0.8253 0.8000 0.0253 3.15629 QUESTION 6 Write a C++ program to solve = x+y2 with y(1)=0 at x=1.3 using Euler’s Method #include<iostream> #include<cmath> #include<iomanip> using namespace std; //Given dy/dx float f(float(x),float(y)) { return (x+ y*y); }
  • 17. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) int main() { double y[100],x[100]; int n,i; float h; //Entering the initial values of x & y 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<<"nIterationstxty"<<endl; //Calculating the value of x for(i=1;i<=n;i++) { x[i]=x[i-1]+h; } //Calculating the value of y for(i=1;i<=n;i++) {
  • 18. TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) y[i]=y[i-1]+(h*f(x[i-1],y[i-1])); } //Printing result for(i=0;i<=n;i++) { cout << i<<"tt"<< setprecision(2) <<x[i]<< "t" << setprecision(5)<< y[i] << endl; } return 0; } //Output: Enter the value of x0: 1 Enter The Value of y0: 0 Enter the number of Iterations: 3 Enter The Value of Step Size: 0.1 Iterations x y 0 1 0 1 1.1 0.1 2 1.2 0.211 3 1.3 0.33545