[Python] TypeError: cannot determine truth value of Relational when entering symbolic values

Discussão em 'Python' iniciado por Stack, Março 21, 2025.

  1. Stack

    Stack Membro Participativo

    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 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

    Continue reading...

Compartilhe esta Página