• Problem statement:-

Write down a 'C' program to implement bisection method.

 

  • Problem analysis:-

Definition:-

This method is used to find a root of a equation.

 

Analysis:-

Let,   f(x) be continuous in a close interval  [a,b]  and if f(a) and

f(b)  are opposite sign, then their exist at least one root of  f(x)=0

in close interval  [a,b].

 

  • ALGORITHM:-

 

This algorithm implements the bisection method.

a is lower limit , b is upper limit .  y is  the value of the  function for      particular a.  p is a variable used for swapping  between a,b.  x is the root.  B(x) is functions which calculate the value of the given equation for x.

The algorithm is.....

{

if (y<0)

{

p=b;

b=a;

a=p;

}

while (1)

{

x=(a+b)/2;

if(fabs(x-o)<=.005)

break;

h=B(x);

if (h<0)

b=x;

else

a=x;

o=x;

}

output(x);

}

B(x)

{

v=pow(x,3)+x*x+x+7;

return (v);

}

 

 

  • Program listing:-

#include

#include

#include

 

void main()

{

float h,x,a,b,y,u,c[12],o,p;

int i,g=0;

float B(float);

clrscr();

for(i=0;i<5;i++)

c[i]=B(i);

for(i=0;i<4;i++)

{

y=c[i];

u=c[i+1];

if(y*u<0)

{

a=i;

b=i+1;

g=0;

break;

}

else

g=1;

}

if(g)

{

for(i=0;i>=-5;i--)

c[-i]=B(i);

for(i=0;i<=5;i++)

{

y=c[i];

u=c[i+1];

if(y*u<0)

{

a=-i;

b=-i-1;

break;

}

}

}

 

printf("the equation is   x^3+x^2+x+7\n\n");

printf("the interval is [ %.2f,%.2f]\n",a,b);

y=B(a);

u=B(b);

i=1;

printf("\nTHE TABLE IS…\n\nITERATION\ta\tb\tx\tF(x)\n");

printf("-----------------------------------------------\n");

if(y<0)

{

p=b;

b=a;

a=p;

}

while(1)

{

x=(a+b)/2;

printf("\n%d\t   %.4f  %.4f  %.4f ",(i++),a,b,x);

if(h<0)

printf("  %.4f",h);

else

printf("   %.4f",h);

 

 

if(fabs(x-o)<=0.005)      /*0.005 is the error code.*/

break;

h=B(x);

if(h<0)

b=x;

else

a=x;

o=x;

}

printf("\n\nthe root is %.4f",x);

getch();

}

float B(float x)

{

float v=pow(x,3)+x*x+x+7;

return (v);

}

 

 

  • Input:-

f(x)=pow(x,3)+x*x+x+7;

 

  • Output screen:-

 

the equation is  x^3+x^2+x+7

 

the interval is [ -2.00,-3.00]

 

THE TABLE IS….

 

ITERATION      a                 b             x                   F(x)

-----------------------------------------------------------------

 

1        -2.0000    -3.0000    -2.5000     -4.8750

2        -2.0000    -2.5000    -2.2500     -1.5781

3        -2.0000    -2.2500    -2.1250     -0.2051

4        -2.0000    -2.1250    -2.0625      0.4177

5        -2.0625    -2.1250    -2.0938      0.1115

6        -2.0938    -2.1250    -2.1094     -0.0455

7        -2.0938    -2.1094    -2.1016      0.0333

8        -2.1016    -2.1094    -2.1055     -0.0060

 

the root is   -2.1055

 

  • Problem discussion:-

 

  1. Bisection method is not applicable for someequations. But this method produces a perfect root  value.

 

  1. The value is more perfect if we assume the error code  0.0005,

that means correct up to three decimal places.


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments