Pages

This blog is under construction

Wednesday, December 19, 2018

C program to find the roots of an equation, using Bisection method

Bisection Method

This is a very simple method. Identify two points x = a and x = b such that f (a) and f (b) are having opposite signs. Let f (a) be negative and f (b) be positive. Then there will be a root of f (x) = 0 in between a and b. Let the first approximation be the mid point of the interval (a, b). i.e

If f (x1) = 0, then x1 is a root, other wise root lies between a and x1 or x1 and b according as f (x1) is positive or negative. Then again we bisect the interval and continue the process until the root is found to desired accuracy. Let f (x1) is positive, then root lies in between a and x1. The second approximation to the root is given by,

If f (x2) is negative, then next approximation is given by


Similarly we can get other approximations. This method is also called Bolzano method.

Examples:
Find a real root of the equation f (x) = x 3 – x – 1 = 0, using 

Bisection method.


SOLUTION:

First find the interval in which the root lies, by trail and error 

method.

f (1) =13 – 1 – 1 = –1, which is negative

f (2) = 23 – 2 – 1 = 5, which is positive  

A root of f (x) = x 3 – x – 1 = 0 lies in between 1 and 2.   

x1 = (1+2)/2= 3/2 = 1.5

 f (x1) = f (1.5) = (1.5)3 – 1.5 – 1 = 0.875, which is positive.

Hence, the root lies in between 1 and 1.5 

x2 = (1+1.5)/2=2.5/2 = 1.25

f (x2) = f (1.25) = (1.25)3 – 1.25 – 1 = – 0.29, which is negative.

Hence, the root lies in between 1.25 and 1.5

x3 = (1.25+1.5)/2=1.375

Similarly, we get x4 = 1.3125, x5 = 1.34375, x6 = 1.328125 etc.

C Program:
#include<stdio.h>
float f(float);
void main()
{
float a,b,c,d,t;
int n,i,j,flag=0;

printf("\nEnter the number of iterations:");
scanf("%d",&n);
printf("\nEnter the interval for root");
scanf("%f%f",&a,&b);
if(f(a)*f(b)<0)
{ printf("\n\ta\t b\t root\t\tf(x)");
for(j=0;j<n;j++)
{ c=(a+b)/2;
printf("\n\n%10.6f %10.6f %10.6f %10.6f",a,b,c,f(c));
if(f(c)<0)
a=c;
else if(f(c)>0)
b=c;
else
{
flag=1;
goto xyz;
}
}
xyz:
if(flag==0)
printf("\n\nApproximate value of the root in 6th iteration is:%f",c);
else
printf("\n\nThe Exact value of the root is:%f ",c);
}
else
printf("\nRoot not lies between the given interval");
 }

 float f(float x)
{return(x*x*x-x-1);  //function f (x) = x 3 – x – 1 = 0,
}

Output:

Enter the number of iterations:6                                                       
Enter the interval for root 
1                                                                                              
2                                                                                                                                             
        a             b                root              f(x)                                                                                                                                          
  1.000000   2.000000   1.500000   0.875000                                                                                                                                        
  1.000000   1.500000   1.250000  -0.296875                                                                                                                                         
  1.250000   1.500000   1.375000   0.224609                                                                                                                                         
  1.250000   1.375000   1.312500  -0.051514                                                                                                                                      
  1.312500   1.375000   1.343750   0.082611                                                                                                                                        
  1.312500   1.343750   1.328125   0.014576                                                                                                                                    
Approximate value of the root in 6th iteration is:1.328125  

No comments:

Post a Comment