Lecture 3A Bisection Method.docx - Example find the root of...

This preview shows page 1 - 4 out of 4 pages.

Example: find the root of f(x) = x 2 − 5 between [0, 4] There is a root between [0, 4] because:: f(0) = 0 2 − 5 = −5 f(4) = 4 2 − 5 = 11 Start: Check opposite sign f(a) and f(b) then farther solve. a = 0; f(a) = -5 b = 4; f(b) = 11 Iteration 1: p = (a + b)/2 = 2 f(p) = 2 2 − 5 = -1 Because f(p) < 0, we replace a with p a = 2; f(a) = -1 b = 4; f(b) = 11 Iteration 2: p = (a + b)/2 = 3 f(p) = 3 2 − 5 = 4
Because f(p) > 0, we replace b with p a = 2; f(a) = -1 b = 3; f(b) = 4 Iteration 3: p = (a + b)/2 = 2.5 f(p) = 2.5 2 − 5 = 1.25 Because f(p) > 0, we replace b with p a = 2; f(a) = -1 b = 2.5; f(b) = 1.25 And so on ....
Java program: // Bisection Method - Solves: x^2 - 3 = 0 public class Bisection01 { public static void main(String[] args) { final double epsilon = 0.00001; double a, b, m, y_m, y_a; a = 0; b = 4; while ( (b-a) > epsilon ) { m = (a+b)/2; // Mid point y_m = m*m - 3.0; // y_m = f(m) y_a = a*a - 3.0; // y_a = f(a) if ( (y_m > 0 && y_a < 0) || (y_m < 0 && y_a > 0) ) { // f(a) and f(m) have different signs: move b b = m; } else { // f(a) and f(m) have same signs: move a a = m; } System.out.println("New interval: [" + a + " .. " + b + "]");

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture