Bisection Method (Assignment:1)
Bisection method is one of the m
any root finding methods.
In this method we are given a function f(x) and we approximate 2 roots a and b for the function
such that f(a).f(b)<0.
a and b represent range in which one root may Present.value of function is positive for a and negative for b of vice versa.
representing value of root(x=0,or curve cutting x ax
is at once) zero in between a and b.Then we fin
d another point
c=(a+b)/2
if f(c)==0
then root=c;
else
if f(a).f(c)<0
b=c;
if f(b).f(c)<0
a=c;
and we repeat these steps for the given number of iterations
Code
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
float F(float a)
{
return(pow(a,2)-a-1);
}
void main()
{
float a1,a2,m;
int i,c;
c=0;
cout<<"Enter Value of range\n";
cout<<"Enter value of a\n";
cin>>a1;
cout<<"Enter value of b\n";
cin>>a2;
cout<<"Enter Number Of Iterations..";
cin>>i;
if(F(a1)*F(a2)>0)
{
cout<<"Please enter Valid Roots..\n";
getch();
exit(0);
}
while(c<i)
{
c=c+1;
/* m=(a1+a2)/2;
bisection methode */
//regula falsi methode
m=((a1*F(a2))-(a2*F(a1)))/(F(a2)-F(a1));
if(F(m)==0)
{
cout<<"Exact Root of equation is\n"<<m;
getch();
exit(0);
}
cout<<"count="<<c<<" a1="<<a1<<" a2="<<a2<<" m="<<m<<" F(m=)"<<(F(m))<<"\n";
if(F(a1)*F(m)>0)
{
a1=m;
}
else
{
a2=m;
}
if(fabs(F(m))<0.0001)
//or store absolute value in double type variable..
{
break;
}
}
getch();
}