Open In App

Program for Muller Method

Improve
Improve
Like Article
Like
Save
Share
Report

Given a function f(x) on floating number x and three initial distinct guesses for root of the function, find the root of function. Here, f(x) can be an algebraic or transcendental function.
Examples: 
 

Input : A function f(x) = x^3 + 2x^2 + 10x - 20         and three initial guesses - 0, 1 and 2 .Output : The value of the root is 1.3688 or          any other value within permittable deviation          from the root.         Input : A function f(x) = x^5 - 5x + 2         and three initial guesses - 0, 1 and 2 .Output :The value of the root is 0.4021 or         any other value within permittable deviation         from the root. 


 

Muller Method


Muller Method is a root-finding algorithm for finding the root of a equation of the form, f(x)=0. It was discovered by David E. Muller in 1956.
It begins with three initial assumptions of the root, and then constructing a parabola through these three points, and takes the intersection of the x-axis with the parabola to be the next approximation. This process continues until a root with the desired level of accuracy is found .
 

Why to learn Muller’s Method?


Muller Method, being one of the root-finding method along with the other ones like Bisection Method, Regula – Falsi Method, Secant Method and Newton – Raphson Method. But, it offers certain advantages over these methods, as follows – 
 

  1. The rate of convergence, i.e., how much closer we move to the root at each step, is approximately 1.84 in Muller Method, whereas it is 1.62 for secant method, and linear, i.e., 1 for both Regula – falsi Method and bisection method . So, Muller Method is faster than Bisection, Regula – Falsi and Secant method.
  2. Although, it is slower than Newton – Raphson’s Method, which has a rate of convergence of 2, but it overcomes one of the biggest drawbacks of Newton-Raphson Method, i.e., computation of derivative at each step. 
     


So, this shows that Muller Method is an efficient method in calculating root of the function.
 

Algorithm And Its Working


 

  1. Assume any three distinct initial roots of the function, let it be x0, x1 and x2
     
  2. Now, draw a second degree polynomial, i.e., a parabola, through the values of function f(x) for these points – x0, x1 and x2
    The equation of the parabola, p(x), through these points is as follows- 
    p(x) = c + b(x – x_2  ) + a(x – x_2  )^2  , where a, b and c are constants.
     


  1.  
  2. After drawing the parabola, then find the intersection of this parabola with the x-axis, let us say x3
     
  3. Finding the intersection of parabola with the x-axis, i.e., x3: 
    • To find x_3  , the root of p(x), where p(x) = c + b(x – x_2  ) + a(x – x_2  )^2  , such that p(x_3  ) = c + b(x_3  – x_2  ) + a(x_3  – x_2  )^2  = 0, apply the quadratic formula to p(x).Since, there will be two roots, but we have to take that one which is closer to x_2  .To avoid round-off errors due to subtraction of nearby equal numbers, use the following equation:
      x_3 - x_2 = \frac{-2c}{b\pm \sqrt{b^{2}-4ac}}
      Now, since, root of p(x) has to be closer to x_2  , so we have to take that value which has a greater denominator out of the two values possible from the above equation.
    • To find a, b and c for the above equation, put x in p(x) as x_0  , x_1  and x_2  , and let these values be p(x_0  ), p(x_1  ) and p(x_2  ), which are as follows-
      p(x_0  ) = c + b(x_0  – x_2  ) + a(x_0  – x_2  )^2  = f(x_0  ). 
      p(x_1  ) = c + b(x_1  – x_2  ) + a(x_1  – x_2  )^2  = f(x_1  ). 
      p(x_2  ) = c + b(x_2  – x_2  ) + a(x_2  – x_2  )^2  = c = f(x_2  ). 
       
    • So, we have three equations and three variables – a, b, c. After solving them to found out the values of these variables, we get the following values of a, b and c- 
       
c = p(x_2) = f(x_2) .b = (d_2*(h_1)^2 - d_1*(h_2)^2 ) / ( h_1h_2 * (h_1 - h_2)) .a = (d_1*(h_2) - d_2*(h_1)) / ( h_1h_2 * (h_1 - h_2)).

  • where, 
    d_1  = p(x_0  ) – p(x_2  ) = f(x_0  ) – f(x_2
    d_2  = p(x_1  ) – p(x_2  ) = f(x_1  ) – f(x_2
    h_1  = x_0  – x_2
    h_2  = x_1  – x_2
     
  • Now, put these values in the expression for x_3  – x_2  , and obtain x_3
    This is how root of p(x) = x_3  is obtained. 
     
  1. If x_3  is very close to x_2  within the permittable error, then x_3  becomes the root of f(x), otherwise, keep repeating the process of finding the next x_3  , with previous x_1  , x_2  and x_3  as the new x_0  , x_1  and x_2
     


 

C++

// C++ Program to find root of a function, f(x)
#include<bits/stdc++.h>
using namespace std;
 
const int MAX_ITERATIONS = 10000;
 
// Function to calculate f(x)
float f(float x)
{
    // Taking f(x) = x ^ 3 + 2x ^ 2 + 10x - 20
    return 1*pow(x, 3) + 2*x*x + 10*x - 20;
}
 
void Muller(float a, float b, float c)
{
    int i;
    float res;
 
    for (i = 0;;++i)
    {
        // Calculating various constants required
        // to calculate x3
        float f1 = f(a);
        float f2 = f(b);
        float f3 = f(c);
        float d1 = f1 - f3;
        float d2 = f2 - f3;
        float h1 = a - c;
        float h2 = b - c;
        float a0 = f3;
        float a1 = (((d2*pow(h1, 2)) - (d1*pow(h2, 2)))
                    / ((h1*h2) * (h1-h2)));
        float a2 = (((d1*h2) - (d2*h1))/((h1*h2) * (h1-h2)));
        float x = ((-2*a0) / (a1 + abs(sqrt(a1*a1-4*a0*a2))));
        float y = ((-2*a0) / (a1-abs(sqrt(a1*a1-4*a0*a2))));
 
        // Taking the root which is closer to x2
        if (x >= y)
            res = x + c;
        else
            res = y + c;
 
        // checking for resemblance of x3 with x2 till
        // two decimal places
        float m = res*100;
        float n = c*100;
        m = floor(m);
        n = floor(n);
        if (m == n)
            break;
        a = b;
        b = c;
        c = res;
        if (i > MAX_ITERATIONS)
        {
            cout << "Root cannot be found using"
                 << " Muller's method";
            break;
        }
    }
    if (i <= MAX_ITERATIONS)
         cout << "The value of the root is " << res;
}
 
// Driver main function
int main()
{
    float a = 0, b = 1, c = 2;
    Muller(a, b, c);
    return 0;
}

                    

Java

// Java Program to find root of a function, f(x)
import java.io.*;
import static java.lang.Math.*;
 
class Muller
{
    static final int MAX_ITERATIONS = 10000;
 
    // function to calculate f(x)
    static double f(double x)
    {
        // Taking f(x) = x ^ 3 + 2x ^ 2 + 10x - 20
        return 1*pow(x, 3) + 2*x*x + 10*x - 20;
    }
 
    static void Muller(double a, double b, double c)
    {
        int i;
        double res;
 
        for (i = 0;; ++i)
        {
            // Calculating various constants required
            // to calculate x3
            double f1 = f(a);
            double f2 = f(b);
            double f3 = f(c);
            double d1 = f1 - f3;
            double d2 = f2 - f3;
            double h1 = a - c;
            double h2 = b - c;
            double a0 = f3;
            double a1 = (((d2*pow(h1, 2)) - (d1*pow(h2, 2)))
                        / ((h1*h2) * (h1-h2)));
            double a2 = (((d1*h2) - (d2*h1))/((h1*h2) * (h1-h2)));
            double x = ((-2*a0)/(a1 + abs(sqrt(a1*a1-4*a0*a2))));
            double y = ((-2*a0)/(a1-abs(sqrt(a1*a1-4*a0*a2))));
 
            // Taking the root which is closer to x2
            if (x >= y)
                res = x + c;
            else
                res = y + c;
 
            // checking for resemblance of x3 with x2 till
            // two decimal places
            double m = res*100;
            double n = c*100;
            m = floor(m);
            n = floor(n);
            if (m == n)
                break;
            a = b;
            b = c;
            c = res;
            if (i > MAX_ITERATIONS)
            {
                System.out.println("Root cannot be found using" +
                                   " Muller's method");
                break;
            }
        }
        if (i <= MAX_ITERATIONS)
            System.out.println("The value of the root is " + res);
    }
 
    // Driver main function
    public static void main(String args[])
    {
        double a = 0, b = 1, c = 2;
        Muller(a, b, c);
    }
}

                    

Python3

# Python3 Program to find root of
# a function, f(x)
import math;
 
MAX_ITERATIONS = 10000;
 
# Function to calculate f(x)
def f(x):
 
    # Taking f(x) = x ^ 3 + 2x ^ 2 + 10x - 20
    return (1 * pow(x, 3) + 2 * x * x +
                           10 * x - 20);
 
def Muller(a, b, c):
 
    res = 0;
    i = 0;
 
    while (True):
     
        # Calculating various constants
        # required to calculate x3
        f1 = f(a); f2 = f(b); f3 = f(c);
        d1 = f1 - f3;
        d2 = f2 - f3;
        h1 = a - c;
        h2 = b - c;
        a0 = f3;
        a1 = (((d2 * pow(h1, 2)) -
               (d1 * pow(h2, 2))) /
              ((h1 * h2) * (h1 - h2)));
        a2 = (((d1 * h2) - (d2 * h1)) /
              ((h1 * h2) * (h1 - h2)));
        x = ((-2 * a0) / (a1 +
             abs(math.sqrt(a1 * a1 - 4 * a0 * a2))));
        y = ((-2 * a0) / (a1 -
            abs(math.sqrt(a1 * a1 - 4 * a0 * a2))));
 
        # Taking the root which is
        # closer to x2
        if (x >= y):
            res = x + c;
        else:
            res = y + c;
 
        # checking for resemblance of x3
        # with x2 till two decimal places
        m = res * 100;
        n = c * 100;
        m = math.floor(m);
        n = math.floor(n);
        if (m == n):
            break;
        a = b;
        b = c;
        c = res;
        if (i > MAX_ITERATIONS):
            print("Root cannot be found using",
                            "Muller's method");
            break;
        i += 1;
    if (i <= MAX_ITERATIONS):
        print("The value of the root is",
                          round(res, 4));
 
# Driver Code
a = 0;
b = 1;
c = 2;
Muller(a, b, c);
     
# This code is contributed by mits

                    

C#

// C# Program to find root of a function, f(x)
using System;
 
class Muller1
{
    static int MAX_ITERATIONS = 10000;
 
    // function to calculate f(x)
    static double f(double x)
    {
        // Taking f(x) = x ^ 3 + 2x ^ 2 + 10x - 20
        return 1*Math.Pow(x, 3) + 2*x*x + 10*x - 20;
    }
 
    static void Muller(double a, double b, double c)
    {
        int i;
        double res;
 
        for (i = 0;; ++i)
        {
            // Calculating various constants required
            // to calculate x3
            double f1 = f(a);
            double f2 = f(b);
            double f3 = f(c);
            double d1 = f1 - f3;
            double d2 = f2 - f3;
            double h1 = a - c;
            double h2 = b - c;
            double a0 = f3;
            double a1 = (((d2*Math.Pow(h1, 2)) - (d1*Math.Pow(h2, 2)))
                        / ((h1*h2) * (h1-h2)));
            double a2 = (((d1*h2) - (d2*h1))/((h1*h2) * (h1-h2)));
            double x = ((-2*a0)/(a1 + Math.Abs(Math.Sqrt(a1*a1-4*a0*a2))));
            double y = ((-2*a0)/(a1-Math.Abs(Math.Sqrt(a1*a1-4*a0*a2))));
 
            // Taking the root which is closer to x2
            if (x >= y)
                res = x + c;
            else
                res = y + c;
 
            // checking for resemblance of x3 with x2 till
            // two decimal places
            double m = res*100;
            double n = c*100;
            m = Math.Floor(m);
            n = Math.Floor(n);
            if (m == n)
                break;
            a = b;
            b = c;
            c = res;
            if (i > MAX_ITERATIONS)
            {
                Console.WriteLine("Root cannot be found using" +
                                " Muller's method");
                break;
            }
        }
        if (i <= MAX_ITERATIONS)
            Console.WriteLine("The value of the root is " + Math.Round(res,4));
    }
 
    // Driver main function
    static void Main()
    {
        double a = 0, b = 1, c = 2;
        Muller(a, b, c);
    }
}
// this code is contributed by mits

                    

PHP

<?php
// PHP Program to find root of a function, f(x)
 
$MAX_ITERATIONS = 10000;
 
// Function to calculate f(x)
function f($x)
{
    // Taking f(x) = x ^ 3 + 2x ^ 2 + 10x - 20
    return 1*pow($x, 3) + 2*$x*$x + 10*$x - 20;
}
 
function Muller($a, $b, $c)
{
    global $MAX_ITERATIONS;
    $res=0;
 
    for ($i = 0;;++$i)
    {
        // Calculating various constants required
        // to calculate x3
        $f1 = f($a);
        $f2 = f($b);
        $f3 = f($c);
        $d1 = $f1 - $f3;
        $d2 = $f2 - $f3;
        $h1 = $a - $c;
        $h2 = $b - $c;
        $a0 = $f3;
        $a1 = ((($d2*pow($h1, 2)) - ($d1*pow($h2, 2)))
                    / (($h1*$h2) * ($h1-$h2)));
        $a2 = ((($d1*$h2) - ($d2*$h1))/(($h1*$h2) * ($h1-$h2)));
        $x = ((-2*$a0) / ($a1 + abs(sqrt($a1*$a1-4*$a0*$a2))));
        $y = ((-2*$a0) / ($a1-abs(sqrt($a1*$a1-4*$a0*$a2))));
 
        // Taking the root which is closer to x2
        if ($x >= $y)
            $res = $x + $c;
        else
            $res = $y + $c;
 
        // checking for resemblance of x3 with x2 till
        // two decimal places
        $m = $res*100;
        $n = $c*100;
        $m = floor($m);
        $n = floor($n);
        if ($m == $n)
            break;
        $a = $b;
        $b = $c;
        $c = $res;
        if ($i > $MAX_ITERATIONS)
        {
            echo"Root cannot be found using Muller's method";
            break;
        }
    }
    if ($i <= $MAX_ITERATIONS)
        echo "The value of the root is ".round($res,4);
}
 
// Driver main function
 
    $a = 0;
    $b = 1;
    $c = 2;
    Muller($a, $b, $c);
     
// This code is contributed by mits
?>

                    

Javascript

<script>
// JavaScript Program to find root of a function, f(x)
 
const MAX_ITERATIONS = 10000;
 
// Function to calculate f(x)
function f(x)
{
    // Taking f(x) = x ^ 3 + 2x ^ 2 + 10x - 20
    return 1*Math.pow(x, 3) + 2*x*x + 10*x - 20;
}
 
function Muller(a, b, c)
{
    let i;
    let res;
 
    for (i = 0;;++i)
    {
        // Calculating various constants required
        // to calculate x3
        let f1 = f(a);
        let f2 = f(b);
        let f3 = f(c);
        let d1 = f1 - f3;
        let d2 = f2 - f3;
        let h1 = a - c;
        let h2 = b - c;
        let a0 = f3;
        let a1 = (((d2*Math.pow(h1, 2)) - (d1*Math.pow(h2, 2)))
                    / ((h1*h2) * (h1-h2)));
        let a2 = (((d1*h2) - (d2*h1))/((h1*h2) * (h1-h2)));
        let x = ((-2*a0) / (a1 + Math.abs(Math.sqrt(a1*a1-4*a0*a2))));
        let y = ((-2*a0) / (a1-Math.abs(Math.sqrt(a1*a1-4*a0*a2))));
 
        // Taking the root which is closer to x2
        if (x >= y)
            res = x + c;
        else
            res = y + c;
 
        // checking for resemblance of x3 with x2 till
        // two decimal places
        let m = res*100;
        let n = c*100;
        m = Math.floor(m);
        n = Math.floor(n);
        if (m == n)
            break;
        a = b;
        b = c;
        c = res;
        if (i > MAX_ITERATIONS)
        {
            document.write("Root cannot be found using"
                + " Muller's method");
            break;
        }
    }
    if (i <= MAX_ITERATIONS)
        document.write("The value of the root is " + res.toFixed(4));
}
 
// Driver main function
    let a = 0, b = 1, c = 2;
    Muller(a, b, c);
 
 
 
// This code is contributed by Surbhi Tyagi.
</script>

                    

Output: 
 

The value of the root is 1.3688

Auxiliary Space: O(1)
Advantages 
 

  • Can find imaginary roots.
  • No need to find derivatives.


Disadvantages 
 

  • Long to do by hand, more room for error.
  • Extraneous roots can be found.


Reference- 
 

  1. Higher Engineer Mathematics by B.S. Grewal. 
     




 



Last Updated : 27 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads