0% found this document useful (0 votes)
125 views

C Plus Plus Code PDF

This C++ code document contains implementations of three root-finding algorithms: the bisection method, Newton-Raphson method, and secant method. For each method, it includes the necessary header files, defines the equation to solve, implements the algorithm's iterative process, and outputs the computed root. The document is intended to demonstrate how to code up these common numerical methods to find roots of nonlinear equations that cannot be solved analytically.
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

C Plus Plus Code PDF

This C++ code document contains implementations of three root-finding algorithms: the bisection method, Newton-Raphson method, and secant method. For each method, it includes the necessary header files, defines the equation to solve, implements the algorithm's iterative process, and outputs the computed root. The document is intended to demonstrate how to code up these common numerical methods to find roots of nonlinear equations that cannot be solved analytically.
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

/*

C++ code for bisection method


for different functions that cannot be solved analytically
you can use it for any function based on your interest
1.Bisection Method C++ Program (with Output)
*/

#include<iostream>
#include<iomanip>
#include<math.h>

/*
Defining equation to be solved.
Change this equation to solve another problem.
*/

//#define f(x) cos(x) - x * exp(x)


#define f(x) (x*x*x)-x-1
using namespace std;

int main()
{
/* Declaring required variables */
float x0, x1, x, f0, f1, f, e;
int step = 1;

/* Setting precision and writing floating point values in fixed-


point notation. */
cout<< setprecision(6)<< fixed;

/* Inputs */
up:
cout<<"Enter first guess: ";
cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>>e;

/* Calculating Functional Value */


f0 = f(x0);
f1 = f(x1);

/* Checking whether given guesses brackets the root or not. */


if( f0 * f1 > 0.0)
{
cout<<"Incorrect Initial Guesses."<< endl;
goto up;
}
/* Implementing Bisection Method */
cout<< endl<<"****************"<< endl;
cout<<"Bisection Method"<< endl;
cout<<"****************"<< endl;
do
{
/* Bisecting Interval */
x = (x0 + x1)/2;
f = f(x);

cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x<<" and


f(x) = "<< setw(10)<< f(x)<< endl;

if( f0 * f < 0)
{
x1 = x;
}
else
{
x0 = x;
}
step = step + 1;
}while(fabs(f)>e);

cout<< endl<< "Root is: "<< x << endl;

return 0;
}

/*
C++ code for Newton Raphson (NR) Method
for different functions that cannot be solved analytically
you can use it for any function based on your interest
3. C++ Program for Newton Raphson (NR) Method
Program: Finding real roots of nonlinear
equation using Newton Raphson Method
Author: CodeSansar
Date: November 18, 2018

*/

#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>

/* Defining equation to be solved.


Change this equation to solve another problem. */
#define f(x) 3*x - cos(x) -1

/* Defining derivative of g(x).


As you change f(x), change this function also. */
#define g(x) 3 + sin(x)

using namespace std;

int main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;

/* Setting precision and writing floating point values in fixed-


point notation. */
cout<< setprecision(6)<< fixed;

/* Inputs */
cout<<"Enter initial guess: ";
cin>>x0;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;

/* Implementing Newton Raphson Method */


cout<< endl<<"*********************"<< endl;
cout<<"Newton Raphson Method"<< endl;
cout<<"*********************"<< endl;
do
{
g0 = g(x0);
f0 = f(x0);
if(g0 == 0.0)
{
cout<<"Mathematical Error.";
exit(0);
}

x1 = x0 - f0/g0;

cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x1<<"


and f(x) = "<< setw(10)<< f(x1)<< endl;
x0 = x1;

step = step+1;

if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}

f1 = f(x1);

}while(fabs(f1)>e);

cout<< endl<<"Root is: "<< x1;


return 0;
}
/*
C++ code for Secant Method Using C++ with Output
for different functions that cannot be solved analytically
you can use it for any function based on your interest
4. Secant Method Using C++ with Output
Program: Finding real roots of nonlinear
equation using Secant Method
Author: CodeSansar
Date: November 18, 2018 */

#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>

/* Defining equation to be solved.


Change this equation to solve another problem. */
#define f(x) x*x*x - 2*x - 5

using namespace std;

int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;

/* Setting precision and writing floating point values in fixed-


point notation. */
cout<< setprecision(6)<< fixed;

/* Inputs */
cout<<"Enter first guess: ";
cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;

/* Implementing Secant Method */


cout<< endl<<"**************"<< endl;
cout<<"Secant Method"<< endl;
cout<<"**************"<< endl;
do
{
f0 = f(x0);
f1 = f(x1);
if(f0 == f1)
{
cout<<"Mathematical Error.";
exit(0);
}

x2 = x1 - (x1 - x0) * f1/(f1-f0);


f2 = f(x2);

cout<<"Iteration-"<< step<<":\t x2 = "<< setw(10)<< x2<<"


and f(x2) = "<< setw(10)<< f(x2)<< endl;

x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;

step = step + 1;

if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}
}while(fabs(f2)>e);

cout<< endl<<"Root is: "<< x2;

return 0;
}

You might also like