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 (20)

PDF
C++ TUTORIAL 2
Farhan Ab Rahman
 
PPTX
New presentation oop
Ch shampi Ch shampi
 
PDF
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
DOCX
Basic Programs of C++
Bharat Kalia
 
DOCX
C++ file
Mukund Trivedi
 
PPTX
C sharp 8
Germán Küber
 
DOCX
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Alex Penso Romero
 
PDF
Container adapters
mohamed sikander
 
PDF
C++ programs
Mukund Gandrakota
 
PDF
Stl algorithm-Basic types
mohamed sikander
 
DOCX
Travel management
1Parimal2
 
PDF
C++ Programming - 4th Study
Chris Ohk
 
PDF
Static and const members
mohamed sikander
 
DOCX
Opp compile
Muhammad Faiz
 
PPT
Oop1
Vaibhav Bajaj
 
PDF
Implementing stack
mohamed sikander
 
PDF
Polymorphism
mohamed sikander
 
PDF
C++ Programming - 1st Study
Chris Ohk
 
PDF
Go a crash course
Eleanor McHugh
 
PPTX
Ee 3122 numerical methods and statistics sessional credit
Raihan Bin-Mofidul
 
C++ TUTORIAL 2
Farhan Ab Rahman
 
New presentation oop
Ch shampi Ch shampi
 
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
Basic Programs of C++
Bharat Kalia
 
C++ file
Mukund Trivedi
 
C sharp 8
Germán Küber
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Alex Penso Romero
 
Container adapters
mohamed sikander
 
C++ programs
Mukund Gandrakota
 
Stl algorithm-Basic types
mohamed sikander
 
Travel management
1Parimal2
 
C++ Programming - 4th Study
Chris Ohk
 
Static and const members
mohamed sikander
 
Opp compile
Muhammad Faiz
 
Implementing stack
mohamed sikander
 
Polymorphism
mohamed sikander
 
C++ Programming - 1st Study
Chris Ohk
 
Go a crash course
Eleanor McHugh
 
Ee 3122 numerical methods and statistics sessional credit
Raihan Bin-Mofidul
 

Similar to C++ TUTORIAL 9 (20)

PDF
Computer Oriented Numerical Methods Practical File
Harjinder Singh
 
PDF
Assignment on Numerical Method C Code
Syed Ahmed Zaki
 
PDF
Numerical Methods with Computer Programming
Utsav Patel
 
PPT
Calc 6.1b
hartcher
 
DOCX
MATLAB sessions Laboratory 3MAT 275 Laboratory 3Numeric.docx
andreecapon
 
DOC
Numerical Methods in C
Ambili Baby
 
PPT
Numerical method
Kumar Gaurav
 
PDF
Applications Of MATLAB Ordinary Differential Equations (ODE
Justin Knight
 
DOCX
scientific computing
saurabhramteke7
 
PDF
BCSL 058 solved assignment
Indira Gnadhi National Open University (IGNOU)
 
DOCX
Matlab lab manual
nmahi96
 
PPT
Ch08 1
Rendy Robert
 
PDF
21221
inKFUPM
 
PPTX
Euler's method 2_1.pptx c program for Euler's method c program for Euler's me...
rithikapandiyan2020
 
PDF
Ma2002 1.13 rm
Ramakrishna Paduchuri
 
PDF
Numerical Method for UOG mech stu prd by Abdrehman Ahmed
አብድረህማን አህመድ
 
DOCX
Trabajo Scilab
alexistorres
 
DOCX
Listing modul 4
indraatmaja
 
PDF
Numerical Analysis
Mallela Niteesh Kumar Reddy
 
PPTX
Euler's method 2.pptx c program for Euler's method c program for Euler's method
rithikapandiyan2020
 
Computer Oriented Numerical Methods Practical File
Harjinder Singh
 
Assignment on Numerical Method C Code
Syed Ahmed Zaki
 
Numerical Methods with Computer Programming
Utsav Patel
 
Calc 6.1b
hartcher
 
MATLAB sessions Laboratory 3MAT 275 Laboratory 3Numeric.docx
andreecapon
 
Numerical Methods in C
Ambili Baby
 
Numerical method
Kumar Gaurav
 
Applications Of MATLAB Ordinary Differential Equations (ODE
Justin Knight
 
scientific computing
saurabhramteke7
 
Matlab lab manual
nmahi96
 
Ch08 1
Rendy Robert
 
21221
inKFUPM
 
Euler's method 2_1.pptx c program for Euler's method c program for Euler's me...
rithikapandiyan2020
 
Ma2002 1.13 rm
Ramakrishna Paduchuri
 
Numerical Method for UOG mech stu prd by Abdrehman Ahmed
አብድረህማን አህመድ
 
Trabajo Scilab
alexistorres
 
Listing modul 4
indraatmaja
 
Numerical Analysis
Mallela Niteesh Kumar Reddy
 
Euler's method 2.pptx c program for Euler's method c program for Euler's method
rithikapandiyan2020
 
Ad

Recently uploaded (20)

PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Introduction to Indian Writing in English
Trushali Dodiya
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
epi editorial commitee meeting presentation
MIPLM
 
infertility, types,causes, impact, and management
Ritu480198
 
Introduction presentation of the patentbutler tool
MIPLM
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Ad

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