Friday, July 20, 2012

C Program for Bisection Method 2



/*Bisection Method*/

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<math.h>

#include<process.h>

#define f(x) log10(x*x*x)-0.7

double value(double);

void main()

{

double x,v,v1,v2,xr,xu,xl,xo=0,ea,et,tv;

double i,p,it=50;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("Finding Root By Using Bisection Method");

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("--------------------------------------------------");

puts("Enter value of xl and xu ");

scanf("%lf %lf ",&xl,&xu);

puts("--------------------------------------------------");

puts("Enter True Value and Iterations");

scanf("%lf %lf",&tv,&it);

puts("--------------------------------------------------");

i=0;

puts("Iteration\t Xr \t\t Ea\t\tEt");

while(1)

{

i++;

v1=f(xl);

v2=f(xu);

if(v1*v2>=0)

{

puts("Invalid Value of xl and xu");

getch();

exit(0);

}

xr=(xl+xu)/2.0;

ea=(xr-xo)*100/xr;

et=(tv-xr)*100/tv;

if(ea<0)

ea=ea*-1;

v=f(xr);

if(v==0||i==it)

{

printf("\n%3d\t\t%6.5lf\t\t%5.6lf\t\t%5.6lf",(int)i,xr,ea,et);

puts("\n----------------------------------------------");

getch();

exit(0);

}

printf("\n%3d\t\t%6.5lf\t\t%5.6lf\t\t%5.6lf",(int)i,xr,ea,et);

if(v1*v<0)

xu=xr;

else

xl=xr;

xo=xr;

}

getch();

}

Output Of Program:-

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Finding Root By Using Bisection Method

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

---------------------------------------------------------------Enter value of xl and xu

1

3

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

Enter True Value and Iterations

1.262803

10

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

Iteration Xr Ea Et

1 2.00000 100.000000 -58.377831

2 1.50000 33.333333 -18.783373

3 1.75000 14.285714 -38.580602

4 1.62500 7.692308 -28.681988

5 1.68750 3.703704 -33.631295

6 1.71875 1.818182 -36.105948

7 1.70312 0.917431 -34.868622

8 1.71094 0.456621 -35.487285

9 1.71484 0.227790 -35.796617

10 1.71289 0.114025 -35.641951

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

















No comments:

Post a Comment