Simplefindroot.java
// simplefindroot.java - Find the root of x * x - 2 // Version 1.0 Wont Work- Why? Class SimpleFindroot {public static void main (string [] args) {double a = 0.0, b = 10.0, x, step = 0.001; x = a; / * start at one end * / while (f (x)! = 0.0 && x
BRUTE-Force DIR: Check the symbol of the function value of the midpoint of the current interval, and then use this to replace one endpoint in the interval, so that the interval size is reduced. Findroot.java
// Findroot.java - use bisection to find a zeroclass findroot {public static void main (string [] args) {double a = 0.0, b = 10.0, eps = 0.00001; double root = 0.0, residual; while (b - a > eps) {root = (a b) / 2.0; residual = f (root); if (residual> 0) B = root; // Replace Right Endpoint else a = root; // replace leftpoint} system.out .println ("Root IS" root;} static double f (double x) {return (x * x - 2.0);}}
This ensures that each calculation guarantees the improvement of the interval accuracy, that is, the interval has been shrinking.