Friday, July 20, 2012

C Program for bisection method



/*Bisection Method*/

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<math.h>

#include<process.h>

float **e,n;

float value(float);

void main()

{

float x,v,v1,v2,xr,xu,xl,xo=0,ea,es;

int i,p,it=50;

char ch;

clrscr();

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

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

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

printf("Enter degree\n");

scanf("%f",&n);

e=(float**)malloc(((n+1)*2)*4);

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

printf("Enter coeff.in decreasing order of degree\n");

for(i=0,p=n;i<=n;i++,p--)

{

scanf("%f",&e[i][0]);

e[i][1]=p;

}

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

printf("Enter value of xl and xu\n");

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

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

puts("What would you like to Enter\nEs or Number of sig fig.or Iteration=>E/S/I");

ch=getche();

if(ch=='E'||ch=='e')

{

printf("\nEnter value of Es\n");

scanf("%f",&es);

}

else if(ch=='i'||ch=='I')

{

printf("\nEnter no. of Iteration");

scanf("%d",&it);

}

else

{

printf("\nEnter no. of sig. fig\n");

scanf("%f",&p);

es=0.5*pow(10,(2-p));

}

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

i=0;

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

while(1)

{

i++;

v1=value(xl);

v2=value(xu);

if(v1*v2>=0)

{

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

getch();

exit(0);

}

xr=(xl+xu)/2.0;

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

if(ea<0)

ea=ea*-1;

v=value(xr);

if(v==0||ea<es||i==it)

{

printf("\n%3d\t\t%6.5f\t\t%5.3f",i,xr,ea);

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

getch();

exit(0);

}

printf("\n%3d\t\t%6.5f\t\t%5.3f",i,xr,ea);

if(v1*v<0)

xu=xr;

else

xl=xr;

xo=xr;

}

}

float value(float x)

{

int i;

float s=0;

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

{

s=s+e[i][0]*pow(x,e[i][1]);

}

return s;

}




Output Of Program:-

First:-

Enter degree

2

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

Enter coeff.in decreasing order of degree

-0.4 2.2 4.7

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

Enter value of xl and xu

5 10

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

What would you like to Enter

Es or Number of sig fig.or Iteration=>E/S/I

e

Enter value of Es

1

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

Iteration Xr Ea




1 7.50000 100.000

2 6.25000 20.000

3 6.87500 9.091

4 7.18750 4.348

5 7.03125 2.222

6 7.10938 1.099

7 7.14844 0.546

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































Second:-

Enter degree

5

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

Enter coeff.in decreasing order of degree

0.65 -9 45.5 -88 82.3 -26

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

Enter value of xl and xu

0.5 1

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

What would you like to Enter

Es or Number of sig fig.or Iteration=>E/S/I

e

Enter value of Es

10

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

Iteration Xr Ea




1 0.75000 100.000

2 0.62500 20.000

3 0.56250 11.111

4 0.59375 5.263

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





No comments:

Post a Comment