You are on page 1of 15

Program for bisection method in c++ code

Example
 Live Demo

#include <iostream>
using namespace std;
#define EP 0.01
// An example function whose solution is determined using
// Bisection Method. The function is x^3 - x^2 + 2
double solution(double x) {
   return x*x*x - x*x + 2;
}
// Prints root of solution(x) with error in EPSILON
void bisection(double a, double b) {
   if (solution(a) * solution(b) >= 0) {
      cout << "You have not assumed right a and b\n";
      return;
   }
   double c = a;
   while ((b-a) >= EP) {
      // Find middle point
      c = (a+b)/2;
      // Check if middle point is root
      if (solution(c) == 0.0)
         break;
       // Decide the side to repeat the steps
      else if (solution(c)*solution(a) < 0)
         b = c;
      else
         a = c;
   }
   cout << "The value of root is : " << c;
}
 // main function
int main() {
   double a =-500, b = 100;
   bisection(a, b);
   return 0;
}

Output
The value of root is : -0.991821

Solve roots of nonlinear equations in method of False Position


using c++ code
#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)

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 False Position Method */
cout<< endl<<"*********************"<< endl;
cout<<"False Position Method"<< endl;
cout<<"*********************"<< endl;
do
{
/* Applying False Position Method */
/* x is next approximated root using
False Position method */
x = x0 - (x0-x1) * f0/ (f0-f1);
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;
f1 = f;
}
else
{
x0 = x;
f0 = f;
}
step = step + 1;
}while(fabs(f)>e);

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

return 0;
}

False Position C++ Program Output

Enter first guess: 0


Enter second guess: 1
Enter tolerable error: 0.00001

*********************
False Position Method
*********************
Iteration-1: x = 0.314665 and f(x) = 0.519871
Iteration-2: x = 0.446728 and f(x) = 0.203545
Iteration-3: x = 0.494015 and f(x) = 0.070802
Iteration-4: x = 0.509946 and f(x) = 0.023608
Iteration-5: x = 0.515201 and f(x) = 0.007760
Iteration-6: x = 0.516922 and f(x) = 0.002539
Iteration-7: x = 0.517485 and f(x) = 0.000829
Iteration-8: x = 0.517668 and f(x) = 0.000271
Iteration-9: x = 0.517728 and f(x) = 0.000088
Iteration-10: x = 0.517748 and f(x) = 0.000029
Iteration-11: x = 0.517754 and f(x) = 0.000009

Root is: 0.517754

Program

/* Program: Finding real roots of nonlinear


equation using Fixed Point Iteration
Author: CodeSansar
Date: November 18, 2018 */
/* Header Files */
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>

/* Define function f(x) which


is to be solved */
#define f(x) cos(x)-3*x+1
/* Write f(x) as x = g(x) and
define g(x) here */
#define g(x) (1+cos(x))/3

using namespace std;

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

/* 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 Fixed Point Iteration */


cout<< endl<<"**************************"<<
endl;
cout<<"Fixed Point Iteration Method"<< endl;
cout<<"**************************"<< endl;
do
{
x1 = g(x0);
cout<<"Iteration-"<< step<<":\t x1 = "<<
setw(10)<< x1<<" and f(x1) = "<< setw(10)<< f(x1)<<
endl;

step = step + 1;

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

x0 = x1;

}while( fabs(f(x1)) > e);

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


return(0);
}

Output

Enter initial guess: 1


Enter tolerable error: 0.000001
Enter maximum iteration: 10

**************************
Fixed Point Iteration Method
**************************
Iteration-1: x1 = 0.513434 and f(x1) = 0.330761
Iteration-2: x1 = 0.623688 and f(x1) = -0.059333
Iteration-3: x1 = 0.603910 and f(x1) = 0.011391
Iteration-4: x1 = 0.607707 and f(x1) = -0.002162
Iteration-5: x1 = 0.606986 and f(x1) = 0.000411
Iteration-6: x1 = 0.607124 and f(x1) = -0.000078
Iteration-7: x1 = 0.607098 and f(x1) = 0.000015
Iteration-8: x1 = 0.607102 and f(x1) = -0.000003
Iteration-9: x1 = 0.607102 and f(x1) = 0.000001

Root is 0.607102

C++ Source Code: Newton Raphson 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;
}

Newton Raphson C++ Output

Enter initial guess: 2


Enter tolerable error: 0.000001
Enter maximum iteration: 10

*********************
Newton Raphson Method
*********************
Iteration-1: x1 = 0.614547 and f(x1) = 0.026607
Iteration-2: x1 = 0.607108 and f(x1) = 0.000023
Iteration-3: x1 = 0.607102 and f(x1) = -0.000000

Root is: 0.607102


C++ Source Code: Secant Method

/* 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;
}

C++ Program Output: Secant Method

Enter first guess: 0


Enter second guess: 1
Enter tolerable error: 0.000001
Enter maximum iteration: 10
**************
Secant Method
**************
Iteration-1: x2 = -5.000000 and f(x2) = -120.000000
Iteration-2: x2 = 1.315789 and f(x2) = -5.353550
Iteration-3: x2 = 1.610713 and f(x2) = -4.042600
Iteration-4: x2 = 2.520173 and f(x2) = 5.965955
Iteration-5: x2 = 1.978057 and f(x2) = -1.216554
Iteration-6: x2 = 2.069879 and f(x2) = -0.271572
Iteration-7: x2 = 2.096267 and f(x2) = 0.019166
Iteration-8: x2 = 2.094527 and f(x2) = -0.000268
Iteration-9: x2 = 2.094552 and f(x2) = 0.000001

Root is: 2.094552

You might also like