从离散数据插值到曲线的切线 [英] Tangent to curve interpolated from discrete data

查看:57
本文介绍了从离散数据插值到曲线的切线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以从离散数据中找到要切线的曲线.例如:

  x = np.linespace(-100,100,100001)y = sin(x) 

因此,这里的x值是整数,但是如果我们想在 x = 67.875 之类的点处找到切线,该怎么办?

我一直在尝试找出 numpy.interp 是否可行,但是到目前为止还没有运气.我还找到了两个类似的示例,例如

  from scipy import插值导入matplotlib.pyplot作为plt将numpy导入为npx = np.linspace(-100,100,10000)y = np.sin(x)tck,u = interpolate.splprep([y])ti = np.linspace(-100,100,10000)dydx = interpolate.splev(ti,tck,der = 1)plt.plot(x,y)plt.plot(ti,dydx [0])plt.show() 

解决方案

虽然这是使上面的代码可运行的方式,但是它没有提供切线.对于切线,您只需要在单个点上生成导数.但是,您需要在某个地方有一个切线方程,然后实际使用;所以这是一个数学问题.

  from scipy import插值导入matplotlib.pyplot作为plt将numpy导入为npx = np.linspace(-15,15,1000)y = np.sin(x)tck = interpolate.splrep(x,y)x0 = 7.3y0 = interpolate.splev(x0,tck)dydx = interpolate.splev(x0,tck,der = 1)tngnt = lambda x:dydx * x +(y0-dydx * x0)plt.plot(x,y)plt.plot(x0,y0,或")plt.plot(x,tngnt(x),label ="tangent")plt.legend()plt.show() 

应注意,如果您的点足够密集,则根本不需要使用样条线.在那种情况下,获得导数就是取最接近点之间的差.

  from scipy import插值导入matplotlib.pyplot作为plt将numpy导入为npx = np.linspace(-15,15,1000)y = np.sin(x)x0 = 7.3i0 = np.argmin(np.abs(x-x0))x1 = x [i0:i0 + 2]y1 = y [i0:i0 + 2]dydx = np.diff(y1)/np.diff(x1)tngnt = lambda x:dydx * x +(y1 [0] -dydx * x1 [0])plt.plot(x,y)plt.plot(x1 [0],y1 [0],或")plt.plot(x,tngnt(x),label ="tangent")plt.legend()plt.show() 

结果在视觉上将与上面的结果相同.

I was wondering if there's a way to find tangents to curve from discrete data. For example:

x = np.linespace(-100,100,100001)
y = sin(x)

so here x values are integers, but what if we want to find tangent at something like x = 67.875?

I've been trying to figure out if numpy.interp would work, but so far no luck. I also found a couple of similar examples, such as this one, but haven't been able to apply the techniques to my case :( I'm new to Python and don't entirely know how everything works yet, so any help would be appreciated...

this is what I get:

from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-100,100,10000)
y = np.sin(x)
tck, u = interpolate.splprep([y])

ti = np.linspace(-100,100,10000)
dydx = interpolate.splev(ti,tck,der=1)

plt.plot(x,y)
plt.plot(ti,dydx[0])
plt.show()

解决方案

There is a comment in this answer, which tells you that there is a difference between splrep and splprep. For the 1D case you have here, splrep is completely sufficient.

You may also want to limit your curve a but to be able to see the oscilations.

from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-15,15,1000)
y = np.sin(x)
tck = interpolate.splrep(x,y)

dydx = interpolate.splev(x,tck,der=1)

plt.plot(x,y)
plt.plot(x,dydx, label="derivative")

plt.legend()
plt.show()

While this is how the code above would be made runnable, it does not provide a tangent. For the tangent you only need the derivative at a single point. However you need to have the equation of a tangent somewhere and actually use it; so this is more a math question.

from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-15,15,1000)
y = np.sin(x)
tck = interpolate.splrep(x,y)

x0 = 7.3
y0 = interpolate.splev(x0,tck)
dydx = interpolate.splev(x0,tck,der=1)

tngnt = lambda x: dydx*x + (y0-dydx*x0)

plt.plot(x,y)
plt.plot(x0,y0, "or")
plt.plot(x,tngnt(x), label="tangent")

plt.legend()
plt.show()

It should be noted that you do not need to use splines at all if the points you have are dense enough. In that case obtaining the derivative is just taking the differences between the nearest points.

from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-15,15,1000)
y = np.sin(x)

x0 = 7.3
i0 = np.argmin(np.abs(x-x0))
x1 = x[i0:i0+2]
y1 = y[i0:i0+2]
dydx, = np.diff(y1)/np.diff(x1)

tngnt = lambda x: dydx*x + (y1[0]-dydx*x1[0])

plt.plot(x,y)
plt.plot(x1[0],y1[0], "or")
plt.plot(x,tngnt(x), label="tangent")

plt.legend()
plt.show()

The result will be visually identical to the one above.

这篇关于从离散数据插值到曲线的切线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆