我使用 fsolve 来查找示例正弦函数的零,并且效果很好。但是,我想对数据集做同样的事情。两个浮点列表,后来转换为 numpy.asarray()的数组,包含(x,y)值,即' t '和' ys '。
虽然我找到了一些related questions,但我没有实现其中提供的代码,我试图在这里展示。我们感兴趣的数组存储在2D列表中(data [i] [j],其中' i对应于变量(例如data [0] == t == time == x values)和' j'是沿x轴的所述变量的值(例如data [1] == Force)。请记住,每个数据[i]是一个浮点数组。
你能提供一个示例代码,它带有两个输入(两个提到的数组)并返回其交叉点和一个定义的功能(例如' y = 0')。
我包含了一些关于其他相关问题的测试。 (@HYRY的回答)
我不认为这是相关的,但我通过Anaconda使用 Spyder 。
提前致谢!
"""
Following the answer provided by @HYRY in the 'related questions' (see link above).
At this point of the code, the variable 'data' has already been defined as stated before.
"""
from scipy.optimize import fsolve
def tfun(x):
return data[0][x]
def yfun(x):
return data[14][x]
def findIntersection(fun1, fun2, x0):
return [fsolve(lambda x:fun1(x)-fun2(x, y), x0) for y in range(1, 10)]
print findIntersection(tfun, yfun, 0)
返回下一个错误
File "E:/Data/Anaconda/[...]/00-Latest/fsolvestacktest001.py", line 36, in tfun
return data[0][x]
IndexError: arrays used as indices must be of integer (or boolean) type
完整输出如下:
Traceback (most recent call last):
File "<ipython-input-16-105803b235a9>", line 1, in <module>
runfile('E:/Data/Anaconda/[...]/00-Latest/fsolvestacktest001.py', wdir='E:/Data/Anaconda/[...]/00-Latest')
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile
execfile(filename, namespace)
File "E:/Data/Anaconda/[...]/00-Latest/fsolvestacktest001.py", line 44, in <module>
print findIntersection(tfun, yfun, 0)
File "E:/Data/Anaconda/[...]/00-Latest/fsolvestacktest001.py", line 42, in findIntersection
return [fsolve(lambda x:fun1(x)-fun2(x, y), x0) for y in range(1, 10)]
File "C:\Anaconda\lib\site-packages\scipy\optimize\minpack.py", line 140, in fsolve
res = _root_hybr(func, x0, args, jac=fprime, **options)
File "C:\Anaconda\lib\site-packages\scipy\optimize\minpack.py", line 209, in _root_hybr
ml, mu, epsfcn, factor, diag)
File "E:/Data/Anaconda/[...]/00-Latest/fsolvestacktest001.py", line 42, in <lambda>
return [fsolve(lambda x:fun1(x)-fun2(x, y), x0) for y in range(1, 10)]
File "E:/Data/Anaconda/[...]/00-Latest/fsolvestacktest001.py", line 36, in tfun
return data[0][x]
IndexError: arrays used as indices must be of integer (or boolean) type
答案 0 :(得分:1)
你可以转换&#39;通过插值将数据集(数组)转换为连续函数。 scipy.interpolate.interp1d是一个为您提供结果函数的工厂,您可以将其与根查找算法一起使用。 --edit--一个计算20个样本中sin和cos的交集的例子(我使用了三次样条插值,因为分段线性给出了关于平滑度的警告):
>>> import numpy, scipy.optimize, scipy.interpolate
>>> x = numpy.linspace(0,2*numpy.pi, 20)
>>> x
array([ 0. , 0.33069396, 0.66138793, 0.99208189, 1.32277585,
1.65346982, 1.98416378, 2.31485774, 2.64555171, 2.97624567,
3.30693964, 3.6376336 , 3.96832756, 4.29902153, 4.62971549,
4.96040945, 5.29110342, 5.62179738, 5.95249134, 6.28318531])
>>> y1sampled = numpy.sin(x)
>>> y2sampled = numpy.cos(x)
>>> y1int = scipy.interpolate.interp1d(x,y1sampled,kind='cubic')
>>> y2int = scipy.interpolate.interp1d(x,y2sampled,kind='cubic')
>>> scipy.optimize.fsolve(lambda x: y1int(x) - y2int(x), numpy.pi)
array([ 3.9269884])
>>> scipy.optimize.fsolve(lambda x: numpy.sin(x) - numpy.cos(x), numpy.pi)
array([ 3.92699082])
请注意,插值会给你猜测&#39;关于采样点之间应该有什么数据。没办法说出这些猜测有多好。 (但是对于我的例子,你可以看到它是一个非常好的估计)