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?
The problem is the use of
"%+.6f" % pThis use of Old String Formatting forces
pto be a float.What you need to do is specify the formatting of the numbers shown in the conversion using:
string.format()or f-strings
Modify the header
printaccordingly:Example output: