Bisection Method: Muataz Abdulsmad

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Omar Al-Mukhtar University

Faculty of Engineering
Department of Computer Engineering
Numerical Analysis

Bisection Method

Bisection Method is one of the simplest, reliable, easy to implement and


convergence guaranteed method for finding real root of non-linear
equations.
Bisection method is bracketing method and starts with two initial guesses
say x0 and x1 such that x0 and x1 brackets the root i.e. f(x0)f(x1)<0

Root is obtained in Bisection method by successive halving the interval i.e.


If x0 and x1 are two guesses then we compute new approximated root as:
x2 = (x0 + x1)/2

Now we have following three different cases:


If f(x2)=0 then the root is x2

If f(x0)f(x2)<0 then root lies between x0 and x2

If f(x0)f(x2)> 0 then root lies between x1 and x2.

And then process is repeated until we find the root within desired
accuracy.

Muataz Abdulsmad
Bisection Method Program in C++ :
#include<iostream>
using namespace std;

//function used is:


3 2
¿ / X −2 X +3
double func(double x)
{
return x*x*x - 2*x*x + 3;
}
double e=0.01;
double c;

void bisection(double a,double b)


{
if(func(a) * func(b) >= 0)
{
cout<<"Incorrect a and b";
return;
}
c = a;
while ((b-a) >= e)
{
c = (a+b)/2;
if (func(c) == 0.0){
cout<<"Root = "<<c<<endl;
break;
}
else if (func(c)*func(a) < 0){
cout<<"Root ="<<c<<endl;
b = c;
}
else{
cout<<"Root ="<<c<<endl;
a = c;
Muataz Abdulsmad
}
}
}

int main()
{
double a,b;
a=-10;
b=20;
cout<<"The function used is x^3-2x^2+3\n";
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
bisection(a,b);
cout<<"\n";
cout<<"Accurate Root calculated is
="<<c<<endl;
return 0;
}
Output :

a = -10.000000
b = 20.000000
Root = 5.000000
Root = -2.500000
Root = 1.250000
Root = -0.625000
Root = -1.562500
Root = -1.093750
Root = -0.859375
Root = -0.976563
Root = -1.035156
Root = -1.005859
Root = -0.991211
Root = -0.998535
Accurate Root calculated is = -0.998535
Newton-Raphson Method

The Newton-Raphson Method, or simply Newton's Method, is a


technique of finding a solution to an equation in one
variable f(x)=0 with the means of numerical approximation.

Muataz Abdulsmad
f ( x 0)
x 1=x 0− where x0 is the first approximate value, then,
f ' (x 0)
f ( x 1)
x 2=x 1− and so on.
f ' (x 1)
So if xn is the current estimated value, the next approximation xn+1 is given
by
f ( xn )
xn+1= xn− ' ( )
f xn

Newton Raphson Method Program in C++ :


#include<bits/iostream>
#include<math.h>
using namespace std;
#define EPSILON 0.0001
using namespace std;

double func(double x)
{
return x*x*x - 2*x*x +3;
}

double derivFunc(double x)
{
return 3*x*x - 4*x;
}

// Function to find the root


void newtonRaphson(double x){
double x1;
x1=x-func(x)/derivFunc(x);

while(fabs(x1-x)>=EPSILON)
{

x=x1;
x1=x-func(x)/derivFunc(x);
}
Muataz Abdulsmad
cout << "The value of the root is : " << x1;
}

int main()
{
double x0 =-10; // Initial values assumed
newtonRaphson(x0);
return 0;
}
Output:

The value of the root is : -1

Secant Method
Secant Method is open method and starts with two initial guesses for finding
real root of non-linear equations.in Secant method if x0 and x1 are initial
guesses then next approximated root x2 is obtained by following formula:
x2 = x1 - (x1-x0) * f(x1) / ( f(x1) - f(x0) )

And an algorithm for Secant method involves repetition of above process


i.e. we use x1 and x2 to find x3 and so on until we find the root within
desired accuracy.

Secant Method Program in C++ :


#include<iostream>
using namespace std;
#include<math.h>
#define EPSILON 0.

double func(double x){


return x*x -2*x -5;
}

void secant(double x0,double x1){


double x2;
Muataz Abdulsmad
while(1){
x2=x1-func(x1)*(x1-x0)/(func(x1)-func(x0));
if(fabs(x2-x1)<=EPSILON){
break;}
x0=x1;
x1=x2;
}
cout << "The value of the root is : " << x2;
}

int main()
{
double x0=2,x1=4; // Initial values assumed
secant(x0,x1);
return 0;
}
Output:
The value of the root is : 3.44949

Fixed Point Iteration Method :


Fixed point iteration method is open and simple method for finding real root of
non-linear equation by successive approximation. It requires only one initial
guess to start. Since it is open method its convergence is not guaranteed. This
method is also known as Iterative Method.
To find the root of nonlinear equation f(x)=0 by fixed point iteration
method, we write given equation f(x)=0 in the form of x = g(x)

If x0 is initial guess then next approximated root in this method is obtaine by:
x1 = g(x0)
and similarly, next to next approximated root is obtained by using value
of x1 i.e.
Muataz Abdulsmad
x2 = g(x1)
and the process is repeated until we get root within desired accuracy.

Note: While expressing f(x)=0 to x = g(x) we can have many different forms.
For convergence, following criteraia must be satisfied.
|g'(x)|< 1

Fixed Point Method Program in C++ :


#include<iostream>
using namespace std;
#include<math.h>
#define EPSILON 0.0001

double f(double x){


return x*x-6*x+8;
}
double g(double x){
return (x*x+8)/6;
}
void fixed_point(double x0){
double x1;
while(1){
x1=g(x0);
if(fabs(x1-x0)<=EPSILON){
break;
}
x0=x1;
}
cout << "The value of the root is : " << x1;
}

Muataz Abdulsmad
int main()
{
double x0=1; // Initial values assumed
fixed_point(x0);
return 0;
}

Output:
The value of the root is : 1.99984

Muataz Abdulsmad

You might also like