The Half Interval Method
#include
#include
double halfInterval(double XL, double XR, int n);
double factorial(int z);
double P(int n, double x);
int ITMAX=100;
int counter;
main(){
int n,v;
printf("Enter Degree: ");
scanf("%d", &n);
double Q, XL, XR,xl,xr;
double i, root, froot;
Q=0.5/(n+1);
v=0;
for(i=-1; i<=1 ; i=i+Q){
if((P(n,i)*P(n,(i+Q)))<0){
XL=i;
XR=i+Q;
v=v+1;
root=halfInterval(XL,XR,n);
froot=P(n,root);
printf("root%d:%1.16e Iterations:%d f(root%d):%1.16e ",v,root,counter,v,froot);
}
}
return 0;
}
double factorial(int z){
int c;
double f=1.0;
for(c=1; c<=z; c++){
f=f*c;
}
return f;
}
double halfInterval(double XL, double XR, int n){
double XN;
double epsilon=1*(pow(10.0,-14));
counter=0;
int i=0;
for(i=1; i<=ITMAX; i++){
XN=XL+((XR-XL)/2);
counter=counter+1;
if((P(n,XL)*P(n,XN))<0){
XR=XN;}
else{
XL=XN;
}
if((fabs(P(n,XR)-P(n,XL)))
}
}
return XN;
}
double P(int n, double x){
int k,m;
double summation,PP;
m=n/2;
summation=0;
for(k=0; k<=m; k++){
PP=(pow(-1,k))*(factorial((2*n)-(2*k)));
PP=PP/(pow(2,n)*factorial(k)*factorial(n-k)*factorial(n-(2*k)));
PP=PP*pow(x,(n-(2*k)));
summation=summation+PP;
}
return summation;
}
download file now