我用f找到了一个例子窦性函数的零点,并且工作得很好。但是,我也想对dataset做同样的处理。两个浮点数列表,后来转换为包含(x,y)值的numpy.asarray(),数组,即't‘和'ys’。
虽然我找到了一些相关问题,但是我未能实现它们中提供的代码,就像我试图在这里展示的那样。我们感兴趣的数组存储在2D列表中(datai,其中'i‘对应于变量(例如data==t==time==x值),'j’是沿x轴(例如data1==Force)的所述变量的值。请记住,每个datai都是一个浮点数数组。
您能提供一个示例代码,它接受、两个输入、(两个提到的数组),并用定义的函数(例如,'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发布于 2014-12-15 13:27:21
您可以通过插值将数据集(数组)“转换”为连续函数。scipy.interpolate.interp1d是一个为您提供结果函数的工厂,然后您可以在根查找算法中使用该函数。-编辑--一个从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])请注意,插值将给您‘猜测’的数据应该是什么之间的取样点。无法判断这些猜测有多好。(但就我的例子而言,您可以看到这是一个相当不错的估计)
https://stackoverflow.com/questions/27478705
复制相似问题