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

Más contenido relacionado

La actualidad más candente

Fundamental theorem of arithmatic
Fundamental theorem of arithmaticFundamental theorem of arithmatic
Fundamental theorem of arithmaticSubhrajeet Praharaj
 
Lesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum ValuesLesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum ValuesMatthew Leingang
 
Lesson 26: The Fundamental Theorem of Calculus (slides)
Lesson 26: The Fundamental Theorem of Calculus (slides)Lesson 26: The Fundamental Theorem of Calculus (slides)
Lesson 26: The Fundamental Theorem of Calculus (slides)Matthew Leingang
 
22 double integrals
22 double integrals22 double integrals
22 double integralsmath267
 
Solve ODE - BVP through the Least Squares Method
Solve ODE - BVP through the Least Squares MethodSolve ODE - BVP through the Least Squares Method
Solve ODE - BVP through the Least Squares MethodSuddhasheel GHOSH, PhD
 
Stuff You Must Know Cold for the AP Calculus BC Exam!
Stuff You Must Know Cold for the AP Calculus BC Exam!Stuff You Must Know Cold for the AP Calculus BC Exam!
Stuff You Must Know Cold for the AP Calculus BC Exam!A Jorge Garcia
 
Regula Falsi (False position) Method
Regula Falsi (False position) MethodRegula Falsi (False position) Method
Regula Falsi (False position) MethodIsaac Yowetu
 
X2 T01 07 nth roots of unity
X2 T01 07 nth roots of unityX2 T01 07 nth roots of unity
X2 T01 07 nth roots of unityNigel Simmons
 
Section 4.2 properties of rational functions
Section 4.2 properties of rational functions Section 4.2 properties of rational functions
Section 4.2 properties of rational functions Wong Hsiung
 
Lesson 25: Evaluating Definite Integrals (slides)
Lesson 25: Evaluating Definite Integrals (slides)Lesson 25: Evaluating Definite Integrals (slides)
Lesson 25: Evaluating Definite Integrals (slides)Matthew Leingang
 
Cálculo II - Aula 7: Teorema Fundamental do Cálculo
Cálculo II - Aula 7: Teorema Fundamental do CálculoCálculo II - Aula 7: Teorema Fundamental do Cálculo
Cálculo II - Aula 7: Teorema Fundamental do Cálculowillianv
 
Chapter 07 retorno econòmico
Chapter 07 retorno econòmicoChapter 07 retorno econòmico
Chapter 07 retorno econòmicofedericoblanco
 
Numerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingNumerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingUtsav Patel
 
Calculus Cheat Sheet All
Calculus Cheat Sheet AllCalculus Cheat Sheet All
Calculus Cheat Sheet AllMoe Han
 
Power series & Radius of convergence
Power series & Radius of convergencePower series & Radius of convergence
Power series & Radius of convergenceDhruv Darji
 
Applications of calculus in commerce and economics
Applications of calculus in commerce and economicsApplications of calculus in commerce and economics
Applications of calculus in commerce and economicssumanmathews
 
Lesson 2: Limits and Limit Laws
Lesson 2: Limits and Limit LawsLesson 2: Limits and Limit Laws
Lesson 2: Limits and Limit LawsMatthew Leingang
 
35182797 additional-mathematics-form-4-and-5-notes
35182797 additional-mathematics-form-4-and-5-notes35182797 additional-mathematics-form-4-and-5-notes
35182797 additional-mathematics-form-4-and-5-notesWendy Pindah
 

La actualidad más candente (20)

Fundamental theorem of arithmatic
Fundamental theorem of arithmaticFundamental theorem of arithmatic
Fundamental theorem of arithmatic
 
Lesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum ValuesLesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum Values
 
Lesson 26: The Fundamental Theorem of Calculus (slides)
Lesson 26: The Fundamental Theorem of Calculus (slides)Lesson 26: The Fundamental Theorem of Calculus (slides)
Lesson 26: The Fundamental Theorem of Calculus (slides)
 
22 double integrals
22 double integrals22 double integrals
22 double integrals
 
Solve ODE - BVP through the Least Squares Method
Solve ODE - BVP through the Least Squares MethodSolve ODE - BVP through the Least Squares Method
Solve ODE - BVP through the Least Squares Method
 
Stuff You Must Know Cold for the AP Calculus BC Exam!
Stuff You Must Know Cold for the AP Calculus BC Exam!Stuff You Must Know Cold for the AP Calculus BC Exam!
Stuff You Must Know Cold for the AP Calculus BC Exam!
 
Regula Falsi (False position) Method
Regula Falsi (False position) MethodRegula Falsi (False position) Method
Regula Falsi (False position) Method
 
X2 T01 07 nth roots of unity
X2 T01 07 nth roots of unityX2 T01 07 nth roots of unity
X2 T01 07 nth roots of unity
 
Section 4.2 properties of rational functions
Section 4.2 properties of rational functions Section 4.2 properties of rational functions
Section 4.2 properties of rational functions
 
Implementing String
Implementing StringImplementing String
Implementing String
 
Lesson 25: Evaluating Definite Integrals (slides)
Lesson 25: Evaluating Definite Integrals (slides)Lesson 25: Evaluating Definite Integrals (slides)
Lesson 25: Evaluating Definite Integrals (slides)
 
ONTO ONE TO ONE FUNCTION.ppt
ONTO ONE TO ONE FUNCTION.pptONTO ONE TO ONE FUNCTION.ppt
ONTO ONE TO ONE FUNCTION.ppt
 
Cálculo II - Aula 7: Teorema Fundamental do Cálculo
Cálculo II - Aula 7: Teorema Fundamental do CálculoCálculo II - Aula 7: Teorema Fundamental do Cálculo
Cálculo II - Aula 7: Teorema Fundamental do Cálculo
 
Chapter 07 retorno econòmico
Chapter 07 retorno econòmicoChapter 07 retorno econòmico
Chapter 07 retorno econòmico
 
Numerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingNumerical Methods with Computer Programming
Numerical Methods with Computer Programming
 
Calculus Cheat Sheet All
Calculus Cheat Sheet AllCalculus Cheat Sheet All
Calculus Cheat Sheet All
 
Power series & Radius of convergence
Power series & Radius of convergencePower series & Radius of convergence
Power series & Radius of convergence
 
Applications of calculus in commerce and economics
Applications of calculus in commerce and economicsApplications of calculus in commerce and economics
Applications of calculus in commerce and economics
 
Lesson 2: Limits and Limit Laws
Lesson 2: Limits and Limit LawsLesson 2: Limits and Limit Laws
Lesson 2: Limits and Limit Laws
 
35182797 additional-mathematics-form-4-and-5-notes
35182797 additional-mathematics-form-4-and-5-notes35182797 additional-mathematics-form-4-and-5-notes
35182797 additional-mathematics-form-4-and-5-notes
 

Similar a C++ Root Finding Methods Tutorial

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
 
01 derivadas
01   derivadas01   derivadas
01 derivadasklorofila
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C CodeSyed Ahmed Zaki
 
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
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry 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 a C++ Root Finding Methods Tutorial (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.
 

Más de 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
 

Último

SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxUmerFayaz5
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Lokesh Kothari
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real timeSatoshi NAKAHIRA
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfSumit Kumar yadav
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
Nanoparticles synthesis and characterization​ ​
Nanoparticles synthesis and characterization​  ​Nanoparticles synthesis and characterization​  ​
Nanoparticles synthesis and characterization​ ​kaibalyasahoo82800
 
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...ssifa0344
 
Presentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptxPresentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptxgindu3009
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSérgio Sacani
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhousejana861314
 
Broad bean, Lima Bean, Jack bean, Ullucus.pptx
Broad bean, Lima Bean, Jack bean, Ullucus.pptxBroad bean, Lima Bean, Jack bean, Ullucus.pptx
Broad bean, Lima Bean, Jack bean, Ullucus.pptxjana861314
 
Formation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksFormation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksSérgio Sacani
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptxRajatChauhan518211
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Nistarini College, Purulia (W.B) India
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bSérgio Sacani
 

Último (20)

Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
 
The Philosophy of Science
The Philosophy of ScienceThe Philosophy of Science
The Philosophy of Science
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptx
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real time
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdf
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
Nanoparticles synthesis and characterization​ ​
Nanoparticles synthesis and characterization​  ​Nanoparticles synthesis and characterization​  ​
Nanoparticles synthesis and characterization​ ​
 
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
 
Presentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptxPresentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptx
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhouse
 
Broad bean, Lima Bean, Jack bean, Ullucus.pptx
Broad bean, Lima Bean, Jack bean, Ullucus.pptxBroad bean, Lima Bean, Jack bean, Ullucus.pptx
Broad bean, Lima Bean, Jack bean, Ullucus.pptx
 
Formation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksFormation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disks
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptx
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
 

C++ Root Finding Methods Tutorial

  • 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