C Program for Muller’s Method

4 Min Read

Muller’s method is an iterative generalization of the secant method for locating the complex roots of a function. It doesn’t require derivative of the function. The C program for Muller’s method requires three initial guesses and, mathematically, the approximation is done by a second degree parabola passing through these points. (Derivation)

The programming effort for Muller’s Method in C language is a bit tedious as it requires three initial approximations. However, the method converges quadratically for almost all the initial guesses. Overall, this method is more effective for locating complex roots – it can effectively generate complex roots of the function, even though the initial approximations or guesses are real.

Muller’s method converges slower than the Newton-Raphson method, but faster than the secant method. The convergence is linear and the overall approach used in the method is quadratic interpolation of the three guesses.

Below is a short and simple source code in C program for Muller’s method to find the root of cos(x) – x*e^x .

Variables:

  • itr – a counter which keeps track of the no. of iterations performed
  • maxmitr – maximum number of iterations to be performed

f(x) = cos(x) – x*e^x

Source Code for Muller’s Method in C:

#include<stdio.h>
#include<math.h>
#define I 2
float f(float x)
{
    return cos(x) - x*exp(x);
}
main ()
{
    int i, itr, maxmitr;
    float x[4], li, di, mu, s, l, allerr;
    printf("\nEnter the three initial guesses:\n");
    for (i=I-2; i<3; i++)
        scanf("%f", &x[i]);
    printf("Enter allowed error and maximum iterations:\n");
    scanf("%f %d", &allerr, &maxmitr);
    for (itr=1; itr<=maxmitr; itr++)
    {
        li = (x[I] - x[I-1]) / (x[I-1] - x[I-2]);
        di = (x[I] - x[I-2]) / (x[I-1] - x[I-2]);
        mu = f(x[I-2])*li*li - f(x[I-1])*di*di + f(x[I])*(di+li);
        s = sqrt ((mu*mu - 4*f(x[I])*di*li*(f(x[I-2])*li - f(x[I-1])*di + f(x[I]))));
        if (mu < 0)
            l = (2*f(x[I])*di)/(-mu+s);
        else
            l = (2*f(x[I])*di)/(-mu-s);
        x[I+1] = x[I] + l*(x[I] - x[I-1]);
        printf("At iteration no. %3d, x = %7.5f\n", itr, x[I+1]);
        if (fabs (x[I+1] - x[I]) < allerr)
        {
        printf("After %3d iterations, the required root is %6.4f\n", itr, x[I+1]);
        return 0;
        }
        for (i=I-2; i<3; i++)
            x[i] = x[i+1];
    }
printf("The required solution does not converge or iterations are insufficient\n");
return 1;
}

Here, x is an array which holds the three initial guesses for the root and the new improved value obtained as I has been defined as 2 in the program. It is done in this way in this program because array subscripts always start from zero and are never negative. Further, the use of I facilitates more readable and understandable expressions.

Input/Output:

Also see,
Muller’s Method Algorithm/Flowchart
Numerical Methods Tutorial Compilation

Muller’s Method was developed by David Muller in 1956. Considered not that useful to find the real roots of simple functions, it is a very effective method for finding complex roots. You can compare the above result obtained from the Muller’s method C program with that obtained from the Regula-Falsi Method.

Share This Article
1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version