程序填空题:Bisection
In mathematics, the bisection method is a root-finding method that applies to any continuous function for which one knows two values with opposite signs. The method consists of repeatedly bisecting the interval defined by these values and then selecting the subinterval in which the function changes sign, and therefore must contain a root. It is a very simple and robust method, but it is also relatively slow. Because of this, it is often used to obtain a rough approximation to a solution which is then used as a starting point for more rapidly converging methods. The method is also called the interval halving method, the binary search method, or the dichotomy method.
Below is a bisection function to find one root of $2x^2+3x-6=0$, within $[0,5]$, with an error lessthan $10^{-3}$. Fill in the blanks to complete it.
c
double f(double x)
{
return ;
}
double binsection(double (*f)(double), double left, double right, double eps)
{
double x = ;
double fx = f(x);
if ( ) {
return x;
}
double fl = f(left);
double fr = f(right);
if ( fl*fx <0 ) {
return binsection(f, , eps);
} else {
return binsection(f, , eps);
}
}
int main()
{
printf("%.2f\n", binsection(f, 0,5,1e-3));
}
答案:
第1空:2*x*x+3*x-6
第2空:(left+right)/2
第3空:fabs(fx)<eps
第4空:left, x
第5空:x, right
Below is a bisection function to find one root of $2x^2+3x-6=0$, within $[0,5]$, with an error lessthan $10^{-3}$. Fill in the blanks to complete it.
c
double f(double x)
{
return ;
}
double binsection(double (*f)(double), double left, double right, double eps)
{
double x = ;
double fx = f(x);
if ( ) {
return x;
}
double fl = f(left);
double fr = f(right);
if ( fl*fx <0 ) {
return binsection(f, , eps);
} else {
return binsection(f, , eps);
}
}
int main()
{
printf("%.2f\n", binsection(f, 0,5,1e-3));
}
答案:
第1空:2*x*x+3*x-6
第2空:(left+right)/2
第3空:fabs(fx)<eps
第4空:left, x
第5空:x, right