i made the changes... and this program is working fine.
but i am providing the intervals myself in the function bisection method. i dont have to do that i have to pass a function within the main function to calculate the interval,and frankly speaking i have no idea how to do that. please give me some ideas....
here is the changed program code..
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 50
#define eps 1e-6
void ReadPolynomial(int);
void BisectionMethod(int,double,double);
int arr[MAX];
void main()
{
int deg;
double a,b;
clrscr();
printf("\nEnter degree of equation : ");
scanf("%d",°);
ReadPolynomial(deg);
BisectionMethod(deg,a,b);
getch();
}
void ReadPolynomial(int deg)
{
int i,j,terms;
terms = deg+1;
for(i=0;i<terms;i++)
{
printf("\nEnter coefficient of x raised to the power %d : ",i);
scanf("%d",&arr[i]);
}
}
void BisectionMethod(int deg,double a,double b)
{
double x1,dx,fa,fx1,f;
int nstep,i,j,terms;
a=0.0, b=1.0;
terms=deg+1;
dx=b-a;
nstep=0;
f=1.0;
fa=fx1=0.0;
printf("\n\n\nBISECTION METHOD : \nn\ta\t\tb\t\tx1\n");
while(fabs(dx)>eps)
{
x1=(a+b)/2;
for(i=0;i<terms;i++)
{
for(j=0;j<i;j++)
f=f*a;
f=f*arr[i];
fa+=f;
}
for(i=0;i<terms;i++)
{
for(j=0;j<i;j++)
f=f*x1;
f=f*arr[i];
fx1+=f;
}
if((fa*fx1)<0)
{
b=x1;
dx=b-a;
}
else
{
a=x1;
dx=b-a;
}
nstep++;
printf("\n%d\t%lf\t%lf\t%lf",nstep,a,b,x1);
}
printf("\nFinal Root : %lf", x1);
}