Introduction
Bisection method is one of the most ancient and surely the simplest method to find the root of a function. But it is relatively time consuming method. First, we must know an interval in which a root lies. Root is found by repeatedly bisecting an interval. Successive values converge on a root of function f(x) when we begin with a pair of values that bracket the root. Let's take the interval (x1, x2) , x3 is halfway between x1 and x2, x4 is halfway between x2 and x3. We always take the next x-value as the midpoint of the last pair that bracket the root : these values bracket the root when there is sign change of f(x) at the two points. We repeat the process until we converge to the root.
Using C programming language to solve a function by Bisection Method
/* To determine the root of a function by Bisection Method */
#include<stdio.h>
#include<math.h>
float f(float x)
{
return x*sin(x) + cos(x); /* A continuous equation between given interval*/
}
int main()
{
float a,b,c,d,e;
printf("\nEnter the interval (a,b)\n");
scanf("%f%f",&a,&b);
printf("\nEnter the tolerance value\n");
scanf("%f",&d);
do
{
c = (a+b)/2;
if(f(c) * f(a) < 0)
b=c;
else
a=c;
}while( fabs(a-b)>d||f(c)==0);
printf("Root is %f",c);
return 0;
}
No comments:
Post a Comment