SlideShare a Scribd company logo
TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) 
QUESTION 1 
Write a C++ program by using Newton-Raphson method to find the 
real root near 2 of the equation x4 – 11x= 8 
USING DO-WHILE LOOP(FIRST WAY) 
#include <iostream> 
#include <cmath> 
using namespace std; 
#define f(x) pow(x,4) - 11*x - 8 
#define ff(x) 4*pow(x,3) - 11 
void main () 
{ 
double guess, x1, fguess,ffguess; 
cout<<"Enter an initial guess of the solution: "; 
cin>> guess; 
do 
{ 
fguess= f(guess); 
ffguess =ff(guess); 
x1= guess - (fguess/ ffguess); 
cout << "n" <<guess << "tt" << x1<< "tt" << fguess <<"tt" << ffguess 
<< "nn"; 
guess=x1; 
//new approx. becomes previous and it is approximation for the next iteration
}while ( fabs(fguess) > 0.00001); 
cout<<"The root is "<< x1<<endl; 
} 
//Output: 
Enter an initial guess of the solution: 24.0911 
24.0911 18.072 336569 55917.1 
18.072 13.5607 106459 23598.1 
13.5607 10.1825 33659.1 9963.79 
10.1825 7.65875 10630.4 4212.07 
7.65875 5.78392 3348.33 1785.94 
5.78392 4.41096 1047.53 762.974 
4.41096 3.44181 322.039 332.29 
3.44181 2.82066 94.4696 152.088 
2.82066 2.5125 24.2728 78.7663 
2.5125 2.43218 4.21212 52.4422 
2.43218 2.42704 0.239179 46.5503 
2.42704 2.42702 0.000935689 46.1863 
2.42702 2.42702 1.45057e-008 46.1849 
The root is 2.42702
TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) 
USING DO-WHILE LOOP(SECOND WAY) 
#include <cmath> 
#include <iostream> 
#include <iomanip> 
using namespace std; 
double func(double x); 
double diff(double x); 
double newton(double x); 
int main () 
{ 
double a=2,b,r,iter=1; 
cout << "NEWTON-RAPHSON METHOD" << endl; 
cout << "x^4-11x-8 [near 2]n" << endl; 
cout << "itxttf(x)ttf'(x)" <<endl; 
do 
{ 
r=newton(a); 
cout << setprecision(0) << iter << "t"<< setprecision(5) << fixed << a << "tt" 
<< func(a) << "tt" << diff(a) << endl; 
b=a; 
a=r; 
iter++; 
} while (fabs((a-b)/b) > 0.001);
cout << "nThe root is " << a << endl; 
return 0; 
} 
double func(double x) {return (double)pow(x,4)-11*x-8;} 
double diff(double x) {return (double)4*pow(x,3)-11;} 
double newton(double x) {return (double)x-(func(x)/diff(x));} 
//Output: 
NEWTON-RAPHSON METHOD 
x^4-11x-8 [near 2] 
i x f(x) f'(x) 
1 2.00000 -14.00000 21.00000 
2 2.66667 13.23457 64.85185 
3 2.46259 1.68798 48.73623 
4 2.42796 0.04324 46.25104 
The root is 2.42702
TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) 
ALTERNATIVE METHOD FOR QUESTION 1 USING FOR LOOP 
#include <iostream> 
#include <cmath> 
using namespace std; 
#define f(x) pow(x,4) - 11*x - 8 
#define ff(x) 4*pow(x,3) - 11 
void main () 
{ 
double guess, x1, fguess,ffguess; 
cout<<"Enter an initial guess of the solution: "; 
cin>> guess; 
for(int i=0 ; i<=10; i++) 
{ 
fguess=f(guess); 
ffguess =ff(guess); 
x1= guess - (fguess/ ffguess); 
if (fabs(x1- guess)/guess < 0.00001) 
break; 
else 
guess=x1; 
} 
cout<<"The root is "<< x1<<endl; 
}
//Output: 
Enter an initial guess of the solution: 24.0911 
The root is 2.42704 
QUESTION 2 
Write a C++ function program to find the root of the equation 
f(x)=ex – 3x2 to an accuracy of 5 digits. 
USING DO-WHILE LOOP(FIRST WAY) 
#include <iostream> 
#include <cmath> 
using namespace std; 
double f( double x) 
{ 
double fx; 
fx=exp(x) - 3*pow(x,2); 
return fx; 
} 
double ff(double x) 
{ 
double ffx; 
ffx=exp(x) - 6*x; 
return ffx; 
} 
void main () 
{ 
double guess, x1, fguess,ffguess;
TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) 
cout<<"Enter an initial guess of the solution: "; 
cin>> guess; 
do 
{ 
fguess= f(guess); 
ffguess =ff(guess); 
x1= guess - (fguess/ ffguess); 
cout << "n" <<guess << "t" << x1<< "t" << fguess <<"t" << ffguess << 
"nn"; 
guess=x1; 
//new approx becomes previous and it is approximation for the next iteration 
} while ( fabs(fguess) > 0.000001); 
cout<<"The root is "<< x1<<endl; 
} 
//Output: 
Enter an initial guess of the solution: 1 
1 0.914155 -0.281718 -3.28172 
0.914155 0.910018 -0.0123726 -2.99026 
0.910018 0.910008 -3.00348e-005 -2.97574 
0.910008 0.910008 -1.79075e-010 -2.9757 
The root is 0.910008
USING DO-WHILE LOOP(SECOND WAY) 
#include <cmath> 
#include <iostream> 
#include <iomanip> 
using namespace std; 
double func(double x); 
double diff(double x); 
double newton(double x); 
int main () 
{ 
double a=1,b,r; 
int iter=1; 
cout << "NEWTON-RAPHSON METHOD" << endl; 
cout << "e^x-3x^2n" << endl; 
cout << "itxttf(x)ttf'(x)" <<endl; 
do 
{ 
r=newton(a); 
cout << iter << "t"<< setprecision(5) << fixed << a << "tt" << func(a) << "t" 
<< diff(a) << endl; 
b=a; 
a=r; 
iter++; 
} while (fabs((a-b)/b) > 0.001);
TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) 
cout << "nThe root is " << a << endl; 
return 0; 
} 
double func(double x) {return (double)exp(x)-3*pow(x,2);} 
double diff(double x) {return (double)exp(x)-6*x;} 
double newton(double x) {return (double)x-(func(x)/diff(x));} 
//Output: 
NEWTON-RAPHSON METHOD 
e^x-3x^2 
i x f(x) f'(x) 
1 1.00000 -0.28172 -3.28172 
2 0.91416 -0.01237 -2.99026 
3 0.91002 -0.00003 -2.97574 
The root is 0.91001 
QUESTION 3 
Write a C++ Function program to find the smallest positive root of 
the equation x3– 2x – 3 =0. using Successive Approximation Method 
#include <cmath> 
#include <iostream> 
#include <iomanip>
using namespace std; 
double f(double x); 
double g(double x); 
int main () 
{ 
double xo=0,xn=1; 
cout << "SUCCESSIVE APPROXIMATION METHOD" << endl; 
cout << "X^3-2X-3n" << endl; 
while (f(xo)*f(xn) > 0) 
{ 
xn=xo; 
xo++; 
} 
while (fabs(xn-xo) > 0.000001) 
{ 
xo=xn; 
xn=g(xo); 
cout << xo << "t" << xn << endl; 
} 
cout << "nThe root is " << setprecision(6) << xn << endl; 
return 0; 
} 
double f(double x) {return (double)pow(x,3)-2*x-3;} 
double g(double x) {return (double)pow(2*x+3,0.333333);}
TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) 
//Output: 
SUCCESSIVE APPROXIMATION METHOD 
X^3-2X-3 
1 1.70998 
1.70998 1.85856 
1.85856 1.88681 
1.88681 1.89208 
1.89208 1.89306 
1.89306 1.89325 
1.89325 1.89328 
1.89328 1.89329 
1.89329 1.89329 
1.89329 1.89329 
The root is 1.89329 
QUESTION 4 
Write a C++ function to find the positive root of the equation 
cos(x)- 3x +5=0 using Successive Approximation Method 
#include <cmath> 
#include <iostream> 
#include <iomanip> 
using namespace std; 
double f(double x) {return (double)cos(x)-3*x+5;} 
double g(double x) {return (double)(cos(x)+5)/3;}
int main () 
{ 
double xo=0,xn=1; 
cout << "SUCCESSIVE APPROXIMATION METHOD" << endl; 
cout << "cos(x)-3x+5n" << endl; 
while (f(xo)*f(xn) > 0) 
{ 
xn=xo; 
xo++; 
} 
while (fabs(xn-xo) > 0.000001) 
{ 
xo=xn; 
xn=g(xo); 
cout << xo << "t" << xn << endl; 
} 
cout << "nThe root is " << setprecision(6) << xn << endl; 
return 0; 
} 
//Output: 
SUCCESSIVE APPROXIMATION METHOD 
cos(x)-3x+5
TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) 
1 1.84677 
1.84677 1.57584 
1.57584 1.66499 
1.66499 1.63532 
1.63532 1.64517 
1.64517 1.6419 
1.6419 1.64299 
1.64299 1.64262 
1.64262 1.64274 
1.64274 1.6427 
1.6427 1.64272 
1.64272 1.64271 
1.64271 1.64271 
1.64271 1.64271 
The root is 1.64271 
QUESTION 5 
Write a C++ program to find the root of the equation sin(x) + 
3cos(x) =2 using Secant method. Use initial approximations 0 and 1.5 
#include <cmath> 
#include <iostream> 
#include <iomanip> 
using namespace std; 
double f(double x) 
{
return (double)sin(x)+3*cos(x)-2; 
} 
double secant(double x0, double x1) 
{ 
return (double)((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0)); 
} 
int main () 
{ 
double x0,x1=0,x2=1.5; 
cout << "SECANT METHOD" << endl; 
cout << "sin(x)+3cos(x)-2n" << endl; 
do 
{ 
x0=x1; 
x1=x2; 
x2=secant(x0,x1); 
cout << setprecision(6) << fixed << x0 << "t" << x1 << "t" << x2 << endl; 
}while (fabs(x2-x1) > 0.00001); 
cout << "nThe root is " << setprecision(5) << fixed << x2 << endl; 
return 0; 
}
TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) 
//Output: 
SECANT METHOD 
sin(x)+3cos(x)-2 
0.000000 1.500000 0.837851 
1.500000 0.837851 1.160351 
0.837851 1.160351 1.218120 
1.160351 1.218120 1.207622 
1.218120 1.207622 1.207827 
1.207622 1.207827 1.207828 
The root is 1.20783 
QUESTION 6 
Use Secant method to find the real root of the equation x3- 8x =5 
#include <cmath> 
#include <iostream> 
#include <iomanip> 
using namespace std; 
double f(double x) 
{ 
return (double)pow(x,3)-8*x-5; 
}
double secant(double x0, double x1) 
{ 
return (double)((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0)); 
} 
void main () 
{ 
double x0,x1=0,x2=1; 
cout << "SECANT METHOD" << endl; 
cout << "x^3-8x-5n" << endl; 
while (f(x1)*f(x2) > 0) 
{ 
x1=x2; 
x2++; 
} 
do 
{ 
x0=x1; 
x1=x2; 
x2=secant(x0,x1); 
cout << setprecision(6) << fixed << x0 << "t" << x1 << "t" << x2 << endl; 
} 
while (fabs(x2-x1) > 0.00001); 
cout << "nThe root is " << setprecision(5) << fixed << x2 << endl; 
}
TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) 
//Output: 
SECANT METHOD 
x^3-8x-5 
3.000000 4.000000 3.068966 
4.000000 3.068966 3.090738 
3.068966 3.090738 3.100570 
3.090738 3.100570 3.100431 
3.100570 3.100431 3.100432 
The root is 3.10043

More Related Content

What's hot

Polynomial Function and Synthetic Division
Polynomial Function and Synthetic DivisionPolynomial Function and Synthetic Division
Polynomial Function and Synthetic Division
AleczQ1414
 
5 complex numbers y
5 complex numbers y5 complex numbers y
5 complex numbers y
math260
 
1.0 factoring trinomials the ac method and making lists-x
1.0 factoring trinomials  the ac method and making lists-x1.0 factoring trinomials  the ac method and making lists-x
1.0 factoring trinomials the ac method and making lists-x
math260
 
1.3 solving equations y
1.3 solving equations y1.3 solving equations y
1.3 solving equations y
math260
 
Lesson 12: Implicit Differentiation
Lesson 12: Implicit DifferentiationLesson 12: Implicit Differentiation
Lesson 12: Implicit Differentiation
Matthew Leingang
 
23 looking for real roots of real polynomials x
23 looking for real roots of real polynomials x23 looking for real roots of real polynomials x
23 looking for real roots of real polynomials x
math260
 
INVERSE DIFFERENTIAL OPERATOR
INVERSE DIFFERENTIAL OPERATORINVERSE DIFFERENTIAL OPERATOR
INVERSE DIFFERENTIAL OPERATOR
sumanmathews
 
10 rectangular coordinate system x
10 rectangular coordinate system x10 rectangular coordinate system x
10 rectangular coordinate system x
math260
 
1.0 factoring trinomials the ac method and making lists-t
1.0 factoring trinomials  the ac method and making lists-t1.0 factoring trinomials  the ac method and making lists-t
1.0 factoring trinomials the ac method and making lists-t
math260
 
Analytic Geometry
Analytic GeometryAnalytic Geometry
Analytic Geometry
vmayyy
 
3.1 higher derivatives
3.1 higher derivatives3.1 higher derivatives
3.1 higher derivativesmath265
 
11 graphs of first degree functions x
11 graphs of first degree functions x11 graphs of first degree functions x
11 graphs of first degree functions x
math260
 
6 comparison statements, inequalities and intervals y
6 comparison statements, inequalities and intervals y6 comparison statements, inequalities and intervals y
6 comparison statements, inequalities and intervals y
math260
 
Polynomial functionsandgraphs
Polynomial functionsandgraphsPolynomial functionsandgraphs
Polynomial functionsandgraphsJerlyn Fernandez
 
ROOT OF NON-LINEAR EQUATIONS
ROOT OF NON-LINEAR EQUATIONSROOT OF NON-LINEAR EQUATIONS
ROOT OF NON-LINEAR EQUATIONS
fenil patel
 
15 translations of graphs x
15 translations of graphs x15 translations of graphs x
15 translations of graphs x
math260
 
6.2 special cases system of linear equations
6.2 special cases system of linear equations6.2 special cases system of linear equations
6.2 special cases system of linear equationsmath260
 
Bisection
BisectionBisection
Bisection
Shipon Sarder
 

What's hot (20)

Polynomial Function and Synthetic Division
Polynomial Function and Synthetic DivisionPolynomial Function and Synthetic Division
Polynomial Function and Synthetic Division
 
5 complex numbers y
5 complex numbers y5 complex numbers y
5 complex numbers y
 
1.0 factoring trinomials the ac method and making lists-x
1.0 factoring trinomials  the ac method and making lists-x1.0 factoring trinomials  the ac method and making lists-x
1.0 factoring trinomials the ac method and making lists-x
 
1.3 solving equations y
1.3 solving equations y1.3 solving equations y
1.3 solving equations y
 
Lesson 12: Implicit Differentiation
Lesson 12: Implicit DifferentiationLesson 12: Implicit Differentiation
Lesson 12: Implicit Differentiation
 
23 looking for real roots of real polynomials x
23 looking for real roots of real polynomials x23 looking for real roots of real polynomials x
23 looking for real roots of real polynomials x
 
INVERSE DIFFERENTIAL OPERATOR
INVERSE DIFFERENTIAL OPERATORINVERSE DIFFERENTIAL OPERATOR
INVERSE DIFFERENTIAL OPERATOR
 
Funktion nollakohta
Funktion nollakohtaFunktion nollakohta
Funktion nollakohta
 
10 rectangular coordinate system x
10 rectangular coordinate system x10 rectangular coordinate system x
10 rectangular coordinate system x
 
1.0 factoring trinomials the ac method and making lists-t
1.0 factoring trinomials  the ac method and making lists-t1.0 factoring trinomials  the ac method and making lists-t
1.0 factoring trinomials the ac method and making lists-t
 
Analytic Geometry
Analytic GeometryAnalytic Geometry
Analytic Geometry
 
3.1 higher derivatives
3.1 higher derivatives3.1 higher derivatives
3.1 higher derivatives
 
Orthogonal_Polynomials
Orthogonal_PolynomialsOrthogonal_Polynomials
Orthogonal_Polynomials
 
11 graphs of first degree functions x
11 graphs of first degree functions x11 graphs of first degree functions x
11 graphs of first degree functions x
 
6 comparison statements, inequalities and intervals y
6 comparison statements, inequalities and intervals y6 comparison statements, inequalities and intervals y
6 comparison statements, inequalities and intervals y
 
Polynomial functionsandgraphs
Polynomial functionsandgraphsPolynomial functionsandgraphs
Polynomial functionsandgraphs
 
ROOT OF NON-LINEAR EQUATIONS
ROOT OF NON-LINEAR EQUATIONSROOT OF NON-LINEAR EQUATIONS
ROOT OF NON-LINEAR EQUATIONS
 
15 translations of graphs x
15 translations of graphs x15 translations of graphs x
15 translations of graphs x
 
6.2 special cases system of linear equations
6.2 special cases system of linear equations6.2 special cases system of linear equations
6.2 special cases system of linear equations
 
Bisection
BisectionBisection
Bisection
 

Similar to C++ TUTORIAL 7

C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
Farhan Ab Rahman
 
Modificacion del programa
Modificacion del programaModificacion del programa
Modificacion del programaMario José
 
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
 
C++ TUTORIAL 10
C++ TUTORIAL 10C++ TUTORIAL 10
C++ TUTORIAL 10
Farhan Ab Rahman
 
01 derivadas
01   derivadas01   derivadas
01 derivadasklorofila
 
Matlab differential
Matlab differentialMatlab differential
Matlab differential
pramodkumar1804
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
Vyacheslav Arbuzov
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
ikdysfm
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
Avjinder (Avi) Kaler
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
Avjinder (Avi) Kaler
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
Veera Pendyala
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
Syed Ahmed Zaki
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
HODZoology3
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
kinan keshkeh
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
Simone Federici
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
Vikas Sharma
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyYasuharu Nakano
 
Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.
Dr. Volkan OBAN
 

Similar to C++ TUTORIAL 7 (20)

C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 
Modificacion del programa
Modificacion del programaModificacion del programa
Modificacion del programa
 
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
 
C++ TUTORIAL 10
C++ TUTORIAL 10C++ TUTORIAL 10
C++ TUTORIAL 10
 
01 derivadas
01   derivadas01   derivadas
01 derivadas
 
Matlab differential
Matlab differentialMatlab differential
Matlab differential
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Es84
Es84Es84
Es84
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
 
Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.
 

More from Farhan Ab Rahman

C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
Farhan Ab Rahman
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
Farhan Ab Rahman
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
Farhan Ab Rahman
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
Farhan Ab Rahman
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
Farhan Ab Rahman
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
Farhan Ab Rahman
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
Farhan Ab Rahman
 
VIBRATIONS AND WAVES TUTORIAL#2
VIBRATIONS AND WAVES TUTORIAL#2VIBRATIONS AND WAVES TUTORIAL#2
VIBRATIONS AND WAVES TUTORIAL#2Farhan Ab Rahman
 
Kitab Bakurah.amani
Kitab Bakurah.amaniKitab Bakurah.amani
Kitab Bakurah.amani
Farhan Ab Rahman
 

More from Farhan Ab Rahman (11)

C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
VIBRATIONS AND WAVES TUTORIAL#2
VIBRATIONS AND WAVES TUTORIAL#2VIBRATIONS AND WAVES TUTORIAL#2
VIBRATIONS AND WAVES TUTORIAL#2
 
Notis Surau
Notis SurauNotis Surau
Notis Surau
 
Kitab Bakurah.amani
Kitab Bakurah.amaniKitab Bakurah.amani
Kitab Bakurah.amani
 

Recently uploaded

Hemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptxHemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptx
muralinath2
 
Leaf Initiation, Growth and Differentiation.pdf
Leaf Initiation, Growth and Differentiation.pdfLeaf Initiation, Growth and Differentiation.pdf
Leaf Initiation, Growth and Differentiation.pdf
RenuJangid3
 
general properties of oerganologametal.ppt
general properties of oerganologametal.pptgeneral properties of oerganologametal.ppt
general properties of oerganologametal.ppt
IqrimaNabilatulhusni
 
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
Sérgio Sacani
 
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
yqqaatn0
 
Toxic effects of heavy metals : Lead and Arsenic
Toxic effects of heavy metals : Lead and ArsenicToxic effects of heavy metals : Lead and Arsenic
Toxic effects of heavy metals : Lead and Arsenic
sanjana502982
 
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
Wasswaderrick3
 
Orion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWSOrion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWS
Columbia Weather Systems
 
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Sérgio Sacani
 
Introduction to Mean Field Theory(MFT).pptx
Introduction to Mean Field Theory(MFT).pptxIntroduction to Mean Field Theory(MFT).pptx
Introduction to Mean Field Theory(MFT).pptx
zeex60
 
Chapter 12 - climate change and the energy crisis
Chapter 12 - climate change and the energy crisisChapter 12 - climate change and the energy crisis
Chapter 12 - climate change and the energy crisis
tonzsalvador2222
 
Lateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensiveLateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensive
silvermistyshot
 
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
yqqaatn0
 
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
Ana Luísa Pinho
 
in vitro propagation of plants lecture note.pptx
in vitro propagation of plants lecture note.pptxin vitro propagation of plants lecture note.pptx
in vitro propagation of plants lecture note.pptx
yusufzako14
 
Seminar of U.V. Spectroscopy by SAMIR PANDA
 Seminar of U.V. Spectroscopy by SAMIR PANDA Seminar of U.V. Spectroscopy by SAMIR PANDA
Seminar of U.V. Spectroscopy by SAMIR PANDA
SAMIR PANDA
 
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
University of Maribor
 
role of pramana in research.pptx in science
role of pramana in research.pptx in sciencerole of pramana in research.pptx in science
role of pramana in research.pptx in science
sonaliswain16
 
bordetella pertussis.................................ppt
bordetella pertussis.................................pptbordetella pertussis.................................ppt
bordetella pertussis.................................ppt
kejapriya1
 
BLOOD AND BLOOD COMPONENT- introduction to blood physiology
BLOOD AND BLOOD COMPONENT- introduction to blood physiologyBLOOD AND BLOOD COMPONENT- introduction to blood physiology
BLOOD AND BLOOD COMPONENT- introduction to blood physiology
NoelManyise1
 

Recently uploaded (20)

Hemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptxHemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptx
 
Leaf Initiation, Growth and Differentiation.pdf
Leaf Initiation, Growth and Differentiation.pdfLeaf Initiation, Growth and Differentiation.pdf
Leaf Initiation, Growth and Differentiation.pdf
 
general properties of oerganologametal.ppt
general properties of oerganologametal.pptgeneral properties of oerganologametal.ppt
general properties of oerganologametal.ppt
 
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
 
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
 
Toxic effects of heavy metals : Lead and Arsenic
Toxic effects of heavy metals : Lead and ArsenicToxic effects of heavy metals : Lead and Arsenic
Toxic effects of heavy metals : Lead and Arsenic
 
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
DERIVATION OF MODIFIED BERNOULLI EQUATION WITH VISCOUS EFFECTS AND TERMINAL V...
 
Orion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWSOrion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWS
 
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
 
Introduction to Mean Field Theory(MFT).pptx
Introduction to Mean Field Theory(MFT).pptxIntroduction to Mean Field Theory(MFT).pptx
Introduction to Mean Field Theory(MFT).pptx
 
Chapter 12 - climate change and the energy crisis
Chapter 12 - climate change and the energy crisisChapter 12 - climate change and the energy crisis
Chapter 12 - climate change and the energy crisis
 
Lateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensiveLateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensive
 
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
原版制作(carleton毕业证书)卡尔顿大学毕业证硕士文凭原版一模一样
 
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
Deep Behavioral Phenotyping in Systems Neuroscience for Functional Atlasing a...
 
in vitro propagation of plants lecture note.pptx
in vitro propagation of plants lecture note.pptxin vitro propagation of plants lecture note.pptx
in vitro propagation of plants lecture note.pptx
 
Seminar of U.V. Spectroscopy by SAMIR PANDA
 Seminar of U.V. Spectroscopy by SAMIR PANDA Seminar of U.V. Spectroscopy by SAMIR PANDA
Seminar of U.V. Spectroscopy by SAMIR PANDA
 
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
Remote Sensing and Computational, Evolutionary, Supercomputing, and Intellige...
 
role of pramana in research.pptx in science
role of pramana in research.pptx in sciencerole of pramana in research.pptx in science
role of pramana in research.pptx in science
 
bordetella pertussis.................................ppt
bordetella pertussis.................................pptbordetella pertussis.................................ppt
bordetella pertussis.................................ppt
 
BLOOD AND BLOOD COMPONENT- introduction to blood physiology
BLOOD AND BLOOD COMPONENT- introduction to blood physiologyBLOOD AND BLOOD COMPONENT- introduction to blood physiology
BLOOD AND BLOOD COMPONENT- introduction to blood physiology
 

C++ TUTORIAL 7

  • 1. TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 1 Write a C++ program by using Newton-Raphson method to find the real root near 2 of the equation x4 – 11x= 8 USING DO-WHILE LOOP(FIRST WAY) #include <iostream> #include <cmath> using namespace std; #define f(x) pow(x,4) - 11*x - 8 #define ff(x) 4*pow(x,3) - 11 void main () { double guess, x1, fguess,ffguess; cout<<"Enter an initial guess of the solution: "; cin>> guess; do { fguess= f(guess); ffguess =ff(guess); x1= guess - (fguess/ ffguess); cout << "n" <<guess << "tt" << x1<< "tt" << fguess <<"tt" << ffguess << "nn"; guess=x1; //new approx. becomes previous and it is approximation for the next iteration
  • 2. }while ( fabs(fguess) > 0.00001); cout<<"The root is "<< x1<<endl; } //Output: Enter an initial guess of the solution: 24.0911 24.0911 18.072 336569 55917.1 18.072 13.5607 106459 23598.1 13.5607 10.1825 33659.1 9963.79 10.1825 7.65875 10630.4 4212.07 7.65875 5.78392 3348.33 1785.94 5.78392 4.41096 1047.53 762.974 4.41096 3.44181 322.039 332.29 3.44181 2.82066 94.4696 152.088 2.82066 2.5125 24.2728 78.7663 2.5125 2.43218 4.21212 52.4422 2.43218 2.42704 0.239179 46.5503 2.42704 2.42702 0.000935689 46.1863 2.42702 2.42702 1.45057e-008 46.1849 The root is 2.42702
  • 3. TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) USING DO-WHILE LOOP(SECOND WAY) #include <cmath> #include <iostream> #include <iomanip> using namespace std; double func(double x); double diff(double x); double newton(double x); int main () { double a=2,b,r,iter=1; cout << "NEWTON-RAPHSON METHOD" << endl; cout << "x^4-11x-8 [near 2]n" << endl; cout << "itxttf(x)ttf'(x)" <<endl; do { r=newton(a); cout << setprecision(0) << iter << "t"<< setprecision(5) << fixed << a << "tt" << func(a) << "tt" << diff(a) << endl; b=a; a=r; iter++; } while (fabs((a-b)/b) > 0.001);
  • 4. cout << "nThe root is " << a << endl; return 0; } double func(double x) {return (double)pow(x,4)-11*x-8;} double diff(double x) {return (double)4*pow(x,3)-11;} double newton(double x) {return (double)x-(func(x)/diff(x));} //Output: NEWTON-RAPHSON METHOD x^4-11x-8 [near 2] i x f(x) f'(x) 1 2.00000 -14.00000 21.00000 2 2.66667 13.23457 64.85185 3 2.46259 1.68798 48.73623 4 2.42796 0.04324 46.25104 The root is 2.42702
  • 5. TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) ALTERNATIVE METHOD FOR QUESTION 1 USING FOR LOOP #include <iostream> #include <cmath> using namespace std; #define f(x) pow(x,4) - 11*x - 8 #define ff(x) 4*pow(x,3) - 11 void main () { double guess, x1, fguess,ffguess; cout<<"Enter an initial guess of the solution: "; cin>> guess; for(int i=0 ; i<=10; i++) { fguess=f(guess); ffguess =ff(guess); x1= guess - (fguess/ ffguess); if (fabs(x1- guess)/guess < 0.00001) break; else guess=x1; } cout<<"The root is "<< x1<<endl; }
  • 6. //Output: Enter an initial guess of the solution: 24.0911 The root is 2.42704 QUESTION 2 Write a C++ function program to find the root of the equation f(x)=ex – 3x2 to an accuracy of 5 digits. USING DO-WHILE LOOP(FIRST WAY) #include <iostream> #include <cmath> using namespace std; double f( double x) { double fx; fx=exp(x) - 3*pow(x,2); return fx; } double ff(double x) { double ffx; ffx=exp(x) - 6*x; return ffx; } void main () { double guess, x1, fguess,ffguess;
  • 7. TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) cout<<"Enter an initial guess of the solution: "; cin>> guess; do { fguess= f(guess); ffguess =ff(guess); x1= guess - (fguess/ ffguess); cout << "n" <<guess << "t" << x1<< "t" << fguess <<"t" << ffguess << "nn"; guess=x1; //new approx becomes previous and it is approximation for the next iteration } while ( fabs(fguess) > 0.000001); cout<<"The root is "<< x1<<endl; } //Output: Enter an initial guess of the solution: 1 1 0.914155 -0.281718 -3.28172 0.914155 0.910018 -0.0123726 -2.99026 0.910018 0.910008 -3.00348e-005 -2.97574 0.910008 0.910008 -1.79075e-010 -2.9757 The root is 0.910008
  • 8. USING DO-WHILE LOOP(SECOND WAY) #include <cmath> #include <iostream> #include <iomanip> using namespace std; double func(double x); double diff(double x); double newton(double x); int main () { double a=1,b,r; int iter=1; cout << "NEWTON-RAPHSON METHOD" << endl; cout << "e^x-3x^2n" << endl; cout << "itxttf(x)ttf'(x)" <<endl; do { r=newton(a); cout << iter << "t"<< setprecision(5) << fixed << a << "tt" << func(a) << "t" << diff(a) << endl; b=a; a=r; iter++; } while (fabs((a-b)/b) > 0.001);
  • 9. TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) cout << "nThe root is " << a << endl; return 0; } double func(double x) {return (double)exp(x)-3*pow(x,2);} double diff(double x) {return (double)exp(x)-6*x;} double newton(double x) {return (double)x-(func(x)/diff(x));} //Output: NEWTON-RAPHSON METHOD e^x-3x^2 i x f(x) f'(x) 1 1.00000 -0.28172 -3.28172 2 0.91416 -0.01237 -2.99026 3 0.91002 -0.00003 -2.97574 The root is 0.91001 QUESTION 3 Write a C++ Function program to find the smallest positive root of the equation x3– 2x – 3 =0. using Successive Approximation Method #include <cmath> #include <iostream> #include <iomanip>
  • 10. using namespace std; double f(double x); double g(double x); int main () { double xo=0,xn=1; cout << "SUCCESSIVE APPROXIMATION METHOD" << endl; cout << "X^3-2X-3n" << endl; while (f(xo)*f(xn) > 0) { xn=xo; xo++; } while (fabs(xn-xo) > 0.000001) { xo=xn; xn=g(xo); cout << xo << "t" << xn << endl; } cout << "nThe root is " << setprecision(6) << xn << endl; return 0; } double f(double x) {return (double)pow(x,3)-2*x-3;} double g(double x) {return (double)pow(2*x+3,0.333333);}
  • 11. TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) //Output: SUCCESSIVE APPROXIMATION METHOD X^3-2X-3 1 1.70998 1.70998 1.85856 1.85856 1.88681 1.88681 1.89208 1.89208 1.89306 1.89306 1.89325 1.89325 1.89328 1.89328 1.89329 1.89329 1.89329 1.89329 1.89329 The root is 1.89329 QUESTION 4 Write a C++ function to find the positive root of the equation cos(x)- 3x +5=0 using Successive Approximation Method #include <cmath> #include <iostream> #include <iomanip> using namespace std; double f(double x) {return (double)cos(x)-3*x+5;} double g(double x) {return (double)(cos(x)+5)/3;}
  • 12. int main () { double xo=0,xn=1; cout << "SUCCESSIVE APPROXIMATION METHOD" << endl; cout << "cos(x)-3x+5n" << endl; while (f(xo)*f(xn) > 0) { xn=xo; xo++; } while (fabs(xn-xo) > 0.000001) { xo=xn; xn=g(xo); cout << xo << "t" << xn << endl; } cout << "nThe root is " << setprecision(6) << xn << endl; return 0; } //Output: SUCCESSIVE APPROXIMATION METHOD cos(x)-3x+5
  • 13. TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) 1 1.84677 1.84677 1.57584 1.57584 1.66499 1.66499 1.63532 1.63532 1.64517 1.64517 1.6419 1.6419 1.64299 1.64299 1.64262 1.64262 1.64274 1.64274 1.6427 1.6427 1.64272 1.64272 1.64271 1.64271 1.64271 1.64271 1.64271 The root is 1.64271 QUESTION 5 Write a C++ program to find the root of the equation sin(x) + 3cos(x) =2 using Secant method. Use initial approximations 0 and 1.5 #include <cmath> #include <iostream> #include <iomanip> using namespace std; double f(double x) {
  • 14. return (double)sin(x)+3*cos(x)-2; } double secant(double x0, double x1) { return (double)((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0)); } int main () { double x0,x1=0,x2=1.5; cout << "SECANT METHOD" << endl; cout << "sin(x)+3cos(x)-2n" << endl; do { x0=x1; x1=x2; x2=secant(x0,x1); cout << setprecision(6) << fixed << x0 << "t" << x1 << "t" << x2 << endl; }while (fabs(x2-x1) > 0.00001); cout << "nThe root is " << setprecision(5) << fixed << x2 << endl; return 0; }
  • 15. TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) //Output: SECANT METHOD sin(x)+3cos(x)-2 0.000000 1.500000 0.837851 1.500000 0.837851 1.160351 0.837851 1.160351 1.218120 1.160351 1.218120 1.207622 1.218120 1.207622 1.207827 1.207622 1.207827 1.207828 The root is 1.20783 QUESTION 6 Use Secant method to find the real root of the equation x3- 8x =5 #include <cmath> #include <iostream> #include <iomanip> using namespace std; double f(double x) { return (double)pow(x,3)-8*x-5; }
  • 16. double secant(double x0, double x1) { return (double)((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0)); } void main () { double x0,x1=0,x2=1; cout << "SECANT METHOD" << endl; cout << "x^3-8x-5n" << endl; while (f(x1)*f(x2) > 0) { x1=x2; x2++; } do { x0=x1; x1=x2; x2=secant(x0,x1); cout << setprecision(6) << fixed << x0 << "t" << x1 << "t" << x2 << endl; } while (fabs(x2-x1) > 0.00001); cout << "nThe root is " << setprecision(5) << fixed << x2 << endl; }
  • 17. TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) //Output: SECANT METHOD x^3-8x-5 3.000000 4.000000 3.068966 4.000000 3.068966 3.090738 3.068966 3.090738 3.100570 3.090738 3.100570 3.100431 3.100570 3.100431 3.100432 The root is 3.10043