The principle of two-point method for function root is: If the continuous function f (x) values an ISO at the two endpoints of interval [a, b], that is, F (a) f (b) <0, it has at least 1 root R in this interval, that is, f (r) = 0.
The steps of the two-division method are:
Check interval length, if less than the given threshold, then stop, output interval midpoint (a+b)/2;
If f (a) F (b) is <0, the value f ((A+B)/2) of the midpoint is computed;
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 number as f (a), then the root is in the interval [(A+B)/2, b], the a= (a+b)/2, repeat the cycle;
If f ((A+B)/2) is the same number as F (b), then the root is in the interval [A, (A+B)/2], the b= (a+b)/2, repeat the cycle;
This topic requires that a program be written to compute the root of the given interval [a, b] of the specified 3-order polynomial f (x) =a3x3+a2x2+a1x+a0.
Input format:
Enter the 4 coefficients A3, A2, A1, A0 of the polynomials in order in line 1th, giving the interval endpoints a and B in order in line 2nd. The problem guarantees that the polynomial has a unique single root in a given interval.
Output format:
The root of the polynomial in the range is printed on a single line, accurate to 2 digits after the decimal point.
Input Sample:
3-1-3 1
-0.5 0.5
Output Sample:
0.33
Import Java.text.DecimalFormat;
Import Java.util.Scanner;
public class main{/** * (method description) * @param no * @return no * @throws no */public static void Main (string[] args) {
Scanner Scanner = new Scanner (system.in);
Enter 4 coefficients a3,a2,a1,a0 double a3= scanner.nextdouble ();
Double a2= scanner.nextdouble ();
Double a1= scanner.nextdouble ();
Double a0 = scanner.nextdouble ();
The interval a,b Double A = scanner.nextdouble () is given.
Double b = scanner.nextdouble ();
The topic is guaranteed to have roots, so use the while loop to find the root decimalformat df = new DecimalFormat ("#0.00");
while (Math.Abs (a-b) >0.001)//Note that the judgment here must be a threshold, otherwise it will timeout {//compute FA,FB Double FA = GetValue (a3,a2,a1,a0,a);
Double fb = GetValue (a3,a2,a1,a0,b);
if (fa*fb<0) {//computes the midpoint double middle = (a+b)/2.0;
Calculates the midpoint value double fm=getvalue (a3,a2,a1,a0,middle);
if (DF.FORMAT (FM). Equals ("0")) {System.out.println (Df.format (middle));
return;
//if FM and FA same number if (fm*fa>0) {a= middle;
else {b=middle;
} else if (fa*fb==0) {if (fa==0) System.out.println (Df.format (a));
Else System.out.println (Df.format (b));
return;
else return;
System.out.println (Df.format (a+b)/2)); //Calculated function value public static double GetValue (double a3,double a2,double a1,double a0,double x) {return A3*math.pow (x, 3) +a
2*math.pow (x, 2) +a1*math.pow (x, 1) +a0;
}
}