<pre name= "code" class= "CPP" >/* the polynomial single (20) dichotomy method to find the root of the function is: if the continuous function f (x) in the interval [a, b] two end value of the difference, that is F (a) F (b) < 0, it has at least 1 root R in this interval, i.e. f (r) = 0. The steps of the dichotomy are: Check the interval length, if it is less than the given threshold, then stop, the output interval midpoint (a+b)/2; if f (a) f (b) <0, then the value of the midpoint is calculated F ((a+b)/2), and if f ((A+B)/2) is exactly 0, then (a+b Otherwise, 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 loop, 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 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, and is accurate to 2 digits after the decimal point. Input Sample: 3-1-3 1-0.5 0.5 Output example: 0.33*/#include <iostream> #include <sstream> #include <string>using Namespace Std;void Input (double a[4], double& left, double& right) {string line; Getline (CIN, line, ' \ n '); Istringstream S1 (line); int i = 0; while (S1 >> a[i++]); line = ""; Getline (CIN, line, ' \ n '); Istringstream S2 (line); S2 >> left; S2 >> Right;} Double F (double a[4], double x) {return a[0] * x*x*x + a[1] * x*x + a[2] * x + a[3];} void Getexproot (double a[4], double left, double right) {double mid; Double MID, left= f (A, left), right = f (A, right); Mid = (left + right)/2; MID = F (A, mid); Dbl_epsilon is the minimum error of the double precision//Flt_epsilon is the minimum error of the single precision number//is defined in float.h, there is no if in Linux (MID >-dbl_epsilon &&A mp Mid < Dbl_epsilon) {//Note that it is not advisable to use return mid; method, as this is the recursive function printf ("%.2f\n", mid); Return } else if (Mid*left > 0) {left = MID; Getexproot (A, left, right); } else if (Mid*right > 0) {right = MID; Getexproot (A, left, right); }}void Run () {double a[4]; Double left, right; Input (A, left, right); Getexproot (A, left, right);} int main (void) {Run (); return 0;}
Two-part method for finding the polynomial single