Error in finding complex solution using Muller method in python

42 Views Asked by At

So, this is my code..

def mullet_method(f,p0,p1,p2,TOL,N0):
    h1=p1-p0
    h2=p2-p1
    d1=(f(p1)-f(p0))/h1
    d2=(f(p2)-f(p1))/h2
    a=(d2-d1)/(h2+h1)
    i=3
    
    s=" "
    print("-"*100)
    print("n", s*6, "Pₙ₋3", s*8, "Pₙ₋2", s*9, "Pₙ-1", s*9, "Pₙ", s*9, "f(Pₙ)",s*5, "Tolerance")
    
    while(i<=N0):
        b = d2+h2*a
        d=(b**2-4*f(p2)*a)**(1/2)
        
        if (abs(b-d)<abs(b+d)):
            e=b+d
        else:
            e=b-d
            
        h=-2*f(p2)/e
        p=p2+h
        
       #print(i,p0,p1,p2,p,f(p))
        print( "{:0>2}".format(str(i)), s,\
               "%+.6f"%p0, s,\
               "%+.6f"%p1, s,\
               "%+.6f"%p2, s,\
                "%f"%p, s,\
              "%+.6f"%f(p), s,\
               "%+.6f" %h)
        
        if((abs(h)<=TOL)):
            return
        
        p0=p1
        p1=p2
        p2=p
        h1=p1-p0
        h2=p2-p1
        d1=(f(p1)-f(p0))/h1
        d2=(f(p2)-f(p1))/h2
        a=(d2-d1)/(h2+h1)
        i+=1

When i try to input my equation which is f(x) = x^4 − 7.79075x^3 +14.7445x^2 + 2.511x −1.674 = 0, it says must be real number and not complex, any adjustment need to be done to get the imaginary number solution as output?

1

There are 1 best solutions below

0
rioV8 On

The problem is the use of "%+.6f" % p
This use of Old String Formatting forces p to be a float.

What you need to do is specify the formatting of the numbers shown in the conversion using:

string.format()

print("{i:0>2} {p0:< 20.6f} {p1:< 20.6f} {p2:< 20.6f} {p:< 20.6f} {fp:< 20.6f} {h:< 20.6f}.format(i=i, p0=p0, p1=p1, p2=p2, p=p, fp=f(p), h=h)")

or f-strings

print(f"{i:0>2} {p0:< 20.6f} {p1:< 20.6f} {p2:< 20.6f} {p:< 20.6f} {f(p):< 20.6f} {h:< 20.6f}")

Modify the header print accordingly:

print(f"{'n':3} {'Pn-3':20} {'Pn-2':20} {'Pn-1':20} {'Pn':20} {'f(Pn)':20} {'Tolerance':20}")

Example output:

n   Pn-3                 Pn-2                 Pn-1                 Pn                   f(Pn)                Tolerance
-------------------------------------------------------------------------------------------------------------------------------
03 -1.000000             0.000000             5.000000             3.688018             2.330332            -1.311982
04  0.000000             5.000000             3.688018             3.538575             3.428107            -0.149442
05  5.000000             3.688018             3.538575             3.798830-0.324708j   0.490528+1.379416j   0.260255-0.324708j
06  3.688018             3.538575             3.798830-0.324708j   3.976492-0.309120j   0.039683-0.289896j   0.177662+0.015588j
07  3.538575             3.798830-0.324708j   3.976492-0.309120j   3.948063-0.317763j  -0.015641+0.007459j  -0.028429-0.008643j
08  3.798830-0.324708j   3.976492-0.309120j   3.948063-0.317763j   3.948548-0.316123j  -0.000008-0.000058j   0.000486+0.001640j
09  3.976492-0.309120j   3.948063-0.317763j   3.948548-0.316123j   3.948542-0.316124j   0.000000+0.000000j  -0.000006-0.000000j