Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MullerMethod {
- private static final double epsilon = 1 * Math.pow(10, -8);
- private static double f(double x) {
- return Math.log(x) + x;
- }
- public static void main(String args[]) {
- double x_0 = 3;
- double x_1 = 2;
- double x_2 = 1;
- double k_0, k_1;
- double h_0, h_1;
- double A, B, C;
- double rootEstimation = 0.0;
- int iteration = 0;
- while (Math.abs(x_0 - x_1) >= epsilon) {
- // Calculate k_0, k_1, h_0, and h_1
- k_0 = f(x_0) - f(x_2);
- k_1 = f(x_1) - f(x_2);
- h_0 = x_0 - x_2;
- h_1 = x_1 - x_2;
- // Calculating the coefficients
- A = (h_1 * k_0 - h_0 * k_1) / (Math.pow(h_0, 2) * h_1 - Math.pow(h_1, 2) * h_0);
- B = (k_0 - A * Math.pow(h_0, 2)) / (h_0);
- C = f(x_2);
- double sign = (B < 0) ? -1 : 1;
- // Plug in to the alternate version of the quadratic formula
- rootEstimation = x_2 - (2 * C) / (B + sign * Math.sqrt(Math.pow(B, 2) - 4 * A * C));
- x_0 = x_1;
- x_1 = x_2;
- x_2 = rootEstimation;
- iteration++;
- System.out.println("Iteration " + iteration + ": " + "A: " + A + " B: " + B + " C: " + C + " Root estimation: " + rootEstimation);
- }
- System.out.println("After " + iteration + " iterations, the estimated root was: " + rootEstimation + ".");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement