03-1. Two-point method to find a polynomial-type single

Source: Internet
Author: User

The principle of the dichotomy method is: if the continuous function f (x) in the interval [a, b] two endpoints of the value of the difference, that is F (a) f (b) <0, then it has at least 1 root R in this interval, that is, f (r) = 0.

The steps of the dichotomy are:

    • Check the interval length, if it is less than the given threshold, stop, the output interval midpoint (a+b)/2;
    • If f (a) f (b) <0, the value of the midpoint is computed F ((A+B)/2);
    • If f ((A+B)/2) is exactly 0, then (a+b)/2 is the root of the requirement;
    • If f ((A+B)/2) is the same as F (a), then the root is in the interval [(A+B)/2, b], making a= (A+B)/2, repeating the cycle;
    • If f ((A+B)/2) is the same as F (b), then the root is in the interval [A, (A+B)/2], making b= (A+B)/2, repeating the cycle;

      This topic requires a program to calculate the root of a given 3-order polynomial f (x) =a3x3+a2x2+a1x+a0 within a given interval [a, b].

      Input Format:

      Enter the 4 coefficients A3, A2, A1, A0 of the polynomial in the order given in the 1th row, giving the interval endpoints a and B sequentially in line 2nd. The topic guarantees that the polynomial has a unique single root within a given interval.

      output Format:

      The root of the polynomial within the interval is output in a row, which is exactly 2 digits after the decimal point.

      Input Sample:

      3-1-3 1-0.5 0.5
      Sample output:
#include <iostream> #include <cstdio> #include <cmath>using namespace Std;const double threshold=0.01 ; The threshold double a3,a2,a1,a0;double f (double x) {    return  x * (x * (A3 * x + A2) + A1) + a0;} int main () {    double a,b,mid;    CIN >> A3 >> A2 >> A1 >> A0;    Cin >> a >> b;    while (B-a>=threshold)    {        mid= (a+b)/2;        if (f (mid) ==0) break            ;        else if (f (mid) *f (a) >0)            A=mid;        else if (f (mid) *f (b) >0)            b=mid;    }    Mid= (A+B)/2;    printf ("%.2lf\n", mid);    return 0;}

03-1. Two-point method to find a polynomial-type single

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.