问题描述
我正在尝试在 python 中求解非线性方程.我曾尝试使用 Sympy 的求解器,但它似乎在 for 循环语句中不起作用.我正在努力解决输入[N]范围内的变量x.
I am trying to solve for non linear equations in python. I have tried using the solver of the Sympy but it doesn't seem to work in a for loop statement. I am tyring to solve for the variable x over a range of inputs [N].
我在下面附上了我的代码
I have attached my code below
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
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]
answer=[]
x = symbols('x')
for N in range(0,2500,5):
solve([a2*x**2+a1*N*x+a0*N**2-s2*x**2-s1*x-s0-0])
answer.append(x)
print(answer)
可能有比使用 sympy 更有效的方法来解决这个问题 * 任何帮助都将受到高度赞赏.
There could be more efficient ways of solving this problem than using sympy * any help will be much appreicated.
请注意,从 Matlab 过渡后,我还是 Python 新手.我可以在 Matlab 中轻松解决这个问题并附上代码,但我正在用 Python 解决这个问题
Note I am still new to python, after transisitioning from Matlab. I could easliy solve this problem in Matlab and could attach the code, but I am battiling with this in Python
推荐答案
根据 本文档,solve
的输出就是解决方案.x
没有赋值,它仍然只是符号.
According to this documentation, the output of solve
is the solution. Nothing is assigned to x
, that's still just the symbol.
x = symbols('x')
for N in range(0,2500,5):
result = solve(a2*x**2+a1*N*x+a0*N**2-s2*x**2-s1*x-s0-0)
answer.append(result)
这篇关于如何在python中使用for循环求解非线性方程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!