Principle binary method is a function of the root: If the continuous function f (x) values at the two endpoints of the interval [a, b] of the opposite sign, i.e., f (a) f (b) <0, then it is within the interval at least one root r, i.e. f® = 0.
Step dichotomy is as follows:
The length of the test period, is less than a given threshold, then stopped, the output section midpoint (a + b) / 2; otherwise,
if f (a) f (b) <0, the calculation of the midpoint value f ((a + b ) / 2);
if f ((a + b) / 2) is exactly 0, then (a + b) / 2 is required root; otherwise,
if f ((a + b) / 2) and f (a) the same number, then the roots in the interval [(a + b) / 2 , b], so that a = (a + b) / 2, repeating the cycle;
if f ((a + b) / 2) and f (b) the same number, then the roots in the interval [a, (a + b) / 2], so that b = (a + b) / 2, the cycle is repeated.
The title requires programming, given 3 calculates order polynomial F (X) = A 3 X 3 + A2X 2 + X + A 0. 1 A at a given interval [a, b] in the root.
Input format:
input polynomial given in the first line are sequentially four coefficients a3, a2, a1, a0, a and b are given interval endpoint in the second row sequentially. Topic ensure polynomial in a given interval of memory in a single single.
Output format:
output of the polynomial roots in the interval in a row, two decimal place.
Sample input:
3-1-31
-0.5 0.5
Sample output:
0.33
#include <stdio.h>
#include <math.h>
double f(double a);
double getRoot(double a,double b);
double a3, a2, a1, a0;
int main()
{
double a,b;
scanf("%lf%lf%lf%lf",&a3,&a2,&a1,&a0);
scanf("%lf%lf",&a,&b);
double root=getRoot(a,b);
printf("%.2f\n",root);
return 0;
}
double f(double a)
{
return a3*pow(a,3)+a2*pow(a,2)+a1*a+a0;
}
double getRoot(double a,double b)
{
while((b-a)>0.001)
{
if(f((a+b)/2)==0)
{
return (a+b)/2;
}
else if(f((a+b)/2)*f(a)>0)
{
a=(a+b)/2;
}
else
{
b=(a+b)/2;
}
}
return (a+b)/2;
}