问题描述
下面的代码示例包含一个方程,它使用 scipy.fsolve 以 x 轴找到零,
1- 如何运行 fsolve 并要求在特定时间间隔内搜索 f.ex。 -75 和 75?
2- 有时需要在结果变为 2000 时找到 x 值,fsolve 如何实现?
from scipy.optimize import fsolve
def func(x,a,b,c):
result = a * x ** 2 + b * x + c
return result
result = fsolve(func,0.01,args = (1,2,3))
print(result)
import matplotlib.pyplot as plt
import numpy as np
# Data for plotting
x = np.arange(-100.0,100.0,0.1)
fig,ax = plt.subplots()
ax.plot(x,func(x,1,3))
ax.set(xlabel='x',ylabel='result',title='Just for illustration')
ax.grid()
plt.show()
以图形方式显示。
解决方法
-
fsolve 不支持边界。如果您有本地化间隔,则首选 brentq。
-
只需找到
的根lambda x,a,b,c: func(x,b c) -2000