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

PDF
C++ TUTORIAL 10
PDF
C++ TUTORIAL 1
PDF
C++ TUTORIAL 8
PDF
C++ TUTORIAL 6
PDF
C++ TUTORIAL 3
PDF
C++ TUTORIAL 4
PDF
C++ TUTORIAL 7
PDF
C++ TUTORIAL 5
C++ TUTORIAL 10
C++ TUTORIAL 1
C++ TUTORIAL 8
C++ TUTORIAL 6
C++ TUTORIAL 3
C++ TUTORIAL 4
C++ TUTORIAL 7
C++ TUTORIAL 5

What's hot (20)

PDF
C++ TUTORIAL 2
PPTX
New presentation oop
PDF
54602399 c-examples-51-to-108-programe-ee01083101
DOCX
Basic Programs of C++
DOCX
C++ file
PPTX
C sharp 8
DOCX
Programa en C++ ( escriba 3 números y diga cual es el mayor))
PDF
Container adapters
PDF
C++ programs
PDF
Stl algorithm-Basic types
DOCX
Travel management
PDF
C++ Programming - 4th Study
PDF
Static and const members
DOCX
Opp compile
PPT
PDF
Implementing stack
PDF
Polymorphism
PDF
C++ Programming - 1st Study
PDF
Go a crash course
PPTX
Ee 3122 numerical methods and statistics sessional credit
C++ TUTORIAL 2
New presentation oop
54602399 c-examples-51-to-108-programe-ee01083101
Basic Programs of C++
C++ file
C sharp 8
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Container adapters
C++ programs
Stl algorithm-Basic types
Travel management
C++ Programming - 4th Study
Static and const members
Opp compile
Implementing stack
Polymorphism
C++ Programming - 1st Study
Go a crash course
Ee 3122 numerical methods and statistics sessional credit
Ad

Similar to C++ TUTORIAL 9 (20)

PDF
Computer Oriented Numerical Methods Practical File
PDF
Assignment on Numerical Method C Code
PDF
Numerical Methods with Computer Programming
PPT
Calc 6.1b
DOCX
MATLAB sessions Laboratory 3MAT 275 Laboratory 3Numeric.docx
DOC
Numerical Methods in C
PPT
Numerical method
PDF
Applications Of MATLAB Ordinary Differential Equations (ODE
DOCX
scientific computing
PDF
DOCX
Matlab lab manual
PPT
Ch08 1
PDF
21221
PPTX
Euler's method 2_1.pptx c program for Euler's method c program for Euler's me...
PDF
Ma2002 1.13 rm
PDF
Numerical Method for UOG mech stu prd by Abdrehman Ahmed
DOCX
Trabajo Scilab
DOCX
Listing modul 4
PDF
Numerical Analysis
PPTX
Euler's method 2.pptx c program for Euler's method c program for Euler's method
Computer Oriented Numerical Methods Practical File
Assignment on Numerical Method C Code
Numerical Methods with Computer Programming
Calc 6.1b
MATLAB sessions Laboratory 3MAT 275 Laboratory 3Numeric.docx
Numerical Methods in C
Numerical method
Applications Of MATLAB Ordinary Differential Equations (ODE
scientific computing
Matlab lab manual
Ch08 1
21221
Euler's method 2_1.pptx c program for Euler's method c program for Euler's me...
Ma2002 1.13 rm
Numerical Method for UOG mech stu prd by Abdrehman Ahmed
Trabajo Scilab
Listing modul 4
Numerical Analysis
Euler's method 2.pptx c program for Euler's method c program for Euler's method
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
master seminar digital applications in india
PDF
Business Ethics Teaching Materials for college
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Microbial diseases, their pathogenesis and prophylaxis
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pre independence Education in Inndia.pdf
VCE English Exam - Section C Student Revision Booklet
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Types and Its function , kingdom of life
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
01-Introduction-to-Information-Management.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
master seminar digital applications in india
Business Ethics Teaching Materials for college
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester

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