Exercise 5 dichotomy dichotomy zero explanations demand function

There function: \ (F (X) = X ^ + 6.375. 3 \ X ^ 2 + Times. 3 \ X Times - 26 is \)
known \ (f (1.0) \ lt 0, f (2.0) \ gt 0 \) and equation \ (f (x) = 0 \) in the interval \ ([1.0, 2.0] \) has only one root, the root obtains please use dichotomy.

Input Format

no.

Output Format

The output of the equation in the section \ ([1.0, 2.0] \) roots. After the required rounded to the decimal point \ (3 \) bits.

Sample input

no.

Sample Output

Not available.

Topic analysis

This question relates to the algorithm: Two points.
We previously used an integer-half, and the question head using a dichotomy between the real numbers, and so half integer processing is slightly different in the details, but the original aim, we need to know this road dichotomous thinking subject, then the solution This question will become very easy.
Title has taught us \ (F (1.0) \ lt 0, F (2,0) \ gt 0 \) , and our image is a continuous function, so that in the interval \ ([1.0, 2.0] \) between certain there is a feasible solution \ (x_0 \) satisfies \ (f (x_0) = 0 \) , then we can half the.
We start command \ (1.0 L = \) , so \ (R & lt = 2.0 \) (Note that the \ (L \) and \ (R & lt \) are real numbers).
We then recycled to \ (R - L \ lt 10 ^ {- 5} \) In this case, we can ensure that at the end of cycle \ (L \) and \ (R & lt \) hold the results to three decimal places is the same of.
Then we take each \ (MID = (L + R & lt) / 2 \) , there are two cases:

  • Situation. 1: \ (F (MID) \ gt 0 \) , the answer in this case described interval \ ([mid, R] \ ) in order we \ (L = MID \) ;
  • Case 2: \ (f (MID) \ Le 0 \) , in fact, \ (f (mid) = 0 \) when we can directly exit the loop, but in order to perform the same processing and case 1, we let \ ( MID = R & lt \) .

This loop process, we can guarantee that at the end of cycle \ (L \) and \ (R & lt \) before \ (4 \) decimals by no more than \ (10 ^ {--5} \) , then the time to select \ ( L \) or \ (R \) and two significant results array is our answer.

Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
double f(double x) {
    return x*x*x + 6.375*x*x + 3.0*x - 26.0;
}
int main() {
    double L = 1.0, R = 2.0;
    while (R - L >= 1e-5) { // 1e-4是科学计数法,表示1乘10的-4次方
        double mid = (L + R) / 2.0;
        if (f(mid) > 0.0) R = mid;
        else L = mid;
    }
    printf("%.3lf\n", R); // 因为R-L<=1e-4,所以这个时候输出L和R都一样
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11450628.html