计算3条水平线和三次样条的交点
Calculating the intersection points of 3 horizontal lines and a cubic spline
我有一个问题,类似于那些,我现在发帖。我想计算一个三次样条和 3 条水平线之间的交点。对于所有这些水平线,我都知道 y 值,并且我想找出交点对应的 x 值。
我希望你能帮助我。我相信对于更有经验的编码人员来说,解决起来非常容易!
import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np
x = np.arange(0, 10)
y = np.exp(-x**2.0)
spline = interpolate.interp1d(x, y, kind = "cubic")
xnew = np.arange(0, 9, 0.1)
ynew = spline(xnew)
x1=np.arange(0,10)
y1=1/10*np.ones(10)
x2=np.arange(0,10)
y2=2/10*np.ones(10)
x3=np.arange(0,10)
y3=3/10*np.ones(10)
plt.plot(x,y,'o', xnew, ynew, '-', x1,y1, '-.', x2,y2, '-.', x3,y3, '-.')
plt.show()
for i in range(1,4):
idx = np.argwhere(np.diff(np.sign(spline-y_i))).flatten()
list_idx.append(idx)
print(list_idx)
您可以使用scipy.interpolate.InterpolatedUnivariateSpline的roots()
函数求根。所以首先你必须从函数中减去 y-value
并找到根,这给你 x-value
在那个特定的 y-value
.
import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np
x = np.arange(0, 10)
y = np.exp(-x**2.0)
spline = interpolate.interp1d(x, y, kind = "cubic")
xnew = np.arange(0, 9, 0.1)
ynew = spline(xnew)
x1=np.arange(0,10)
y1=1*np.ones(10)/10
x2=np.arange(0,10)
y2=2*np.ones(10)/10
x3=np.arange(0,10)
y3=3*np.ones(10)/10
plt.plot(x,y,'o', xnew, ynew, '-', x1,y1, '-.', x2,y2, '-.', x3,y3, '-.')
plt.show()
y_val = 0.2
func = np.array(y) - y_val
sub_funct = interpolate.InterpolatedUnivariateSpline(x, func) # to find the roots we need to substract y_val from the function
root = sub_funct.roots() # find roots here
print(root)
当 y_val=0.2
为
时打印 x
值
[1.36192179]
编辑
您可以绘制输出图如下。
plt.arrow(root, y_val, 0, -y_val, head_width=0.2, head_length=0.06)
我有一个问题,类似于那些,我现在发帖。我想计算一个三次样条和 3 条水平线之间的交点。对于所有这些水平线,我都知道 y 值,并且我想找出交点对应的 x 值。 我希望你能帮助我。我相信对于更有经验的编码人员来说,解决起来非常容易!
import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np
x = np.arange(0, 10)
y = np.exp(-x**2.0)
spline = interpolate.interp1d(x, y, kind = "cubic")
xnew = np.arange(0, 9, 0.1)
ynew = spline(xnew)
x1=np.arange(0,10)
y1=1/10*np.ones(10)
x2=np.arange(0,10)
y2=2/10*np.ones(10)
x3=np.arange(0,10)
y3=3/10*np.ones(10)
plt.plot(x,y,'o', xnew, ynew, '-', x1,y1, '-.', x2,y2, '-.', x3,y3, '-.')
plt.show()
for i in range(1,4):
idx = np.argwhere(np.diff(np.sign(spline-y_i))).flatten()
list_idx.append(idx)
print(list_idx)
您可以使用scipy.interpolate.InterpolatedUnivariateSpline的roots()
函数求根。所以首先你必须从函数中减去 y-value
并找到根,这给你 x-value
在那个特定的 y-value
.
import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np
x = np.arange(0, 10)
y = np.exp(-x**2.0)
spline = interpolate.interp1d(x, y, kind = "cubic")
xnew = np.arange(0, 9, 0.1)
ynew = spline(xnew)
x1=np.arange(0,10)
y1=1*np.ones(10)/10
x2=np.arange(0,10)
y2=2*np.ones(10)/10
x3=np.arange(0,10)
y3=3*np.ones(10)/10
plt.plot(x,y,'o', xnew, ynew, '-', x1,y1, '-.', x2,y2, '-.', x3,y3, '-.')
plt.show()
y_val = 0.2
func = np.array(y) - y_val
sub_funct = interpolate.InterpolatedUnivariateSpline(x, func) # to find the roots we need to substract y_val from the function
root = sub_funct.roots() # find roots here
print(root)
当 y_val=0.2
为
x
值
[1.36192179]
编辑
您可以绘制输出图如下。
plt.arrow(root, y_val, 0, -y_val, head_width=0.2, head_length=0.06)