I am trying to solve for a single (non linear) equation using fsolve in a for loop, however it doesn't seem to work in my code.
From previous assistance from How to solve nonlinear equations using a for loop in python? I managed to get this right for solving for two or more equations, but could not get it to work for a single non linear equation.
*Note that N is the stepped range values we substitute in for using the for loop
from scipy.optimize import fsolve
import numpy as np
f_curve_coefficients = [-7.14285714e-02, 1.96333333e+01, 6.85130952e+03]
S = [0.2122, 0, 0]
a2 = f_curve_coefficients[0]
a1 = f_curve_coefficients[1]
a0 = f_curve_coefficients[2]
s2 = S[0]
s1 = S[1]
s0 = S[2]
def f(variable):
x = variable
first_eq =a2*x**2+a1*N*x +a0*N**2-(s2*x**2+s1*x+s0)
return [first_eq]
for N in range(1,6,1):
roots = fsolve(f,20) # fsolve(equations, X_0)
print(roots)
In Matlab we have a function called fzero which could solve this - not sure if Python has a similar function?
The solution doesn't have to be fsolve - just working on recommendations from users from python forums...
In advance thank you for all the assistance. Really don't know what I would do without stackoverflow!