C Program for Muller Method

We are given with a function f(x) on a floating number x and we can have three initial guesses for the root of the function. The task is to find the root of the function where f(x) can be any algebraic or transcendental function.
What is Muller Method?
Muller method was first presented by David E. Muller in 1956. A Muller’s method is root finding algorithm which starts with three initial approximations to find the root and then join them with a second degree polynomial (a parabola), then the quadratic formula is used to find a root of the quadratic for the next approximation. That is if x0, x1 and x 2 are the initial approximations then x3 is obtained by solving the quadratic which is obtained by means of x0, x 1 and x2. Then two values among x0, x1 and x2 which are close to x3 are chosen for the next iteration.
Benefit of learning Muller method?
Muller method, is one of the root-finding method like bisection method, Regula-Flasi Method, Secant Method etc. having advantages such as −
The rate of convergence in Muller method is higher than other methods. Rate of convergence in Muller method is 1.84 whereas it is 1.62 in secant and linear method and 1 for both Regula-flasi and bisection method. Rate of convergence is how much we move closer to the root at each step. So, Muller Method is faster.
As it is slower than Newton-Raphson, which has rate of convergence of 2, but the computation of derivate at each step is better in Muller method.
Hence, Muller Method is an efficient method.
Working of Muller Method −
Let us assume three distinct initial roots x0, x1 and x2.
Now draw a parabola, through the values of function f(x) for the points- x0, x1 and x2.
The equation of the parabola, p(x), will be−
p(x )=c+b( x – x 2)+a ( x – x2)2; where a, b and c are the constants.
Now, find the intersection of the parabola with the x-axis like x3.
How to find the intersection of parabola with the x-axis i.e. x3 −Finding x3, the root of p(x), where, p (x ) = c+b ( x – x 2)+a ( x – x2)2 such that p(x3) = c+b( x3 – x 2)+a ( x3 – x2)2 = 0, apply the quadratic formula to p(x). As we have to take that one which is closer to x2 from the 2 roots, we will use the equation −X3−X2=−2cb±b2−4acnow, as the p(x) should be closer to x2, so we will take that value whose denominator is greater out of two from the above equation.To find a, b and c for the above equation, put x in p(x) as x0, x1, x2 and let these values be p(x0), p(x1) and p(x2) which will be as follows −p (x 0) = c+b ( x0 – x 2)+a ( x0 – x2)2 = f ( x0) p ( x1) = c+b (x 1 – x2)+a( x 1 – x2)2 = f ( x1)
p(x 2) = c+b( x 2– x2) + a( x 2–x2)2 = c = f ( x2)So, we have three equations for the three variables-a, b and c.Now put the following expressions for the x3-x2 and obtain the root of p(x) = x3.
Finding x3, the root of p(x), where, p (x ) = c+b ( x – x 2)+a ( x – x2)2 such that p(x3) = c+b( x3 – x 2)+a ( x3 – x2)2 = 0, apply the quadratic formula to p(x). As we have to take that one which is closer to x2 from the 2 roots, we will use the equation −X3−X2=−2cb±b2−4acnow, as the p(x) should be closer to x2, so we will take that value whose denominator is greater out of two from the above equation.
Finding x3, the root of p(x), where, p (x ) = c+b ( x – x 2)+a ( x – x2)2 such that p(x3) = c+b( x3 – x 2)+a ( x3 – x2)2 = 0, apply the quadratic formula to p(x). As we have to take that one which is closer to x2 from the 2 roots, we will use the equation −
now, as the p(x) should be closer to x2, so we will take that value whose denominator is greater out of two from the above equation.
To find a, b and c for the above equation, put x in p(x) as x0, x1, x2 and let these values be p(x0), p(x1) and p(x2) which will be as follows −p (x 0) = c+b ( x0 – x 2)+a ( x0 – x2)2 = f ( x0) p ( x1) = c+b (x 1 – x2)+a( x 1 – x2)2 = f ( x1)
p(x 2) = c+b( x 2– x2) + a( x 2–x2)2 = c = f ( x2)
To find a, b and c for the above equation, put x in p(x) as x0, x1, x2 and let these values be p(x0), p(x1) and p(x2) which will be as follows −
p (x 0) = c+b ( x0 – x 2)+a ( x0 – x2)2 = f ( x0) p ( x1) = c+b (x 1 – x2)+a( x 1 – x2)2 = f ( x1)
p(x 2) = c+b( x 2– x2) + a( x 2–x2)2 = c = f ( x2)
So, we have three equations for the three variables-a, b and c.Now put the following expressions for the x3-x2 and obtain the root of p(x) = x3.
So, we have three equations for the three variables-a, b and c.
Now put the following expressions for the x3-x2 and obtain the root of p(x) = x3.
If x3 is very much closer to x2 then x3 becomes the root of f(x), else repeat the process of finding the next x3, form the previous x1, x2, x3 as the new x0, x1, x2.
Input: a = 1, b = 3, c = 2
Output: The root is 1.368809

Input: a = 1, b = 5, c = 3
Output: The root is 3.000001
Start
Step 1-> Declare and initialize a const MAX = 10000;
Step 2-> In Function float f(float x)
   Return 1*pow(x, 3) + 2*x*x + 10*x – 20
Step 3-> In function int muller(float a, float b, float c)
   Declare i,result
   Loop For i = 0 and ++i
      Initialize f1 = result returned from calling function f(a)
      Initialize f2 = result returned from calling function f(b)
      Initialize f3 = result returned from calling function f(c)
      Set d1 = f1 - f3
      Set d2 = f2 - f3
      Set h1 = a - c
      Set h2 = b - c
      Set a0 = f3
      Set a1 = (((d2*pow(h1, 2)) - (d1*pow(h2, 2))) / ((h1*h2) * (h1-h2)))
      Set a2 = (((d1*h2) - (d2*h1))/((h1*h2) * (h1-h2)))
      Set x = ((-2*a0) / (a1 + abs(sqrt(a1*a1-4*a0*a2))))
      Set y = ((-2*a0) / (a1-abs(sqrt(a1*a1-4*a0*a2))))
      If x >= y
         result = x + c
      Else
         result = y + c
      End if
      Set m = result*100
      Set n = c*100
      Set m = floor(m) and n = floor(n)
      If m == n
      Break
   End If
   Set a = b and b = c and c = result
   If i > MAX
      Print “Root can't be found using Muller method”
      Break
   End If
   End for
   If i <= MAX
   Print result
Step 4-> In function int main()
   Declare and initialize the inputs a = 1, b = 3, c = 2
   Call the function muller(a, b, c)
Stop
 Live Demo
#include <stdio.h>
#include <math.h>
const int MAX = 10000;
//this function to find f(n)
float f(float x) {
   // f(x) = x ^ 3 + 2x ^ 2 + 10x - 20
   return 1*pow(x, 3) + 2*x*x + 10*x - 20;
}
int muller(float a, float b, float c) {
   int i;
   float result;
   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)
         result = x + c;
      else
         result = y + c;
         // checking for resemblance of x3 with x2 till
         // two decimal places
         float m = result*100;
         float n = c*100;
         m = floor(m);
         n = floor(n);
         if (m == n)
         break;
         a = b;
         b = c;
         c = result;
         if (i > MAX) {
            printf("Root can't be found using Muller method\n");
            break;
         }
   }
   if (i <= MAX)
   printf("The root is %f", result);
   return 0;
}
// main function
int main() {
   float a = 1, b = 3, c = 2;
   muller(a, b, c);
   return 0;
}
The root is 1.368809