I heard the issue comes when trying to compare symbolic values, for example if i enter e^(8*x) as the initial function in the code, so I've tried evalf()
with the values to turn them into numeric, but they continue to be symbolic. What else could i do?
import math
import sympy as sp
print("Fixed Point Method")
funini = input("Initial function\nx= ") # initial function by user
valini = float(input("Initial value of x\n")) # initial value of x
valini = float(valini) # convert the initial value into a float
errabs = input("Absolute error to obtain\n") # requested absolute error
errabs = float(errabs) # convert the absolute error into a float
i = 0
errorfin = 100
while True:
i += 1
x = sp.symbols('x') # x as a symbolic variable until given a value
x.evalf()
print(x)
funcion = sp.sympify(funini) # convert the function into a symbolic expression
resultado = funcion.subs(x, valini).evalf() # substitute x as a symbolic variable with the initial value
resultado.evalf()
print(resultado)
print(f"For iteration {i}, x= {resultado}")
errorfin = abs((resultado - valini) / resultado).evalf() # ensure errorfin is numeric
print(f"The error is {errorfin}")
valini = resultado # the result becomes the new initial value
valini.evalf()
print(valini)
if i == 10: # max 10 iterations
print("The values converge, enter a new initial x")
break
elif errorfin <= errabs:
print(f"The error is less than the absolute error, the root is {resultado}")
break
Sympy thinks that e
is another variable. Try passing it exp(8*x)
instead. Also, you have a couple of lines where you do something.evalf()
but don't save it to a variable. Those lines aren't doing anything.