This document contains C++ code examples using numerical methods like Newton-Raphson, successive approximation, and secant methods to find the real roots of various equations. It includes 6 questions/problems with sample code to find roots of equations like x^4 - 11x = 8, e^x - 3x^2 = 0, x^3 - 2x - 3 = 0, and sin(x) + 3cos(x) = 2. For each problem, the code provided the numerical method used, input/output examples, and calculations to iteratively approximate the root to within a set tolerance.
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
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>