如何在给定参数曲线的一点上绘制法线

时间:2019-04-17 08:45:24

标签: python matplotlib sympy

我想在特定点绘制曲线的法线 t_0 = 2 * sp.pi / 5。

曲线由参数方程式x(t)= sin(3t)和y(y)= sin(4t)给出,其中t [0,2pi]。对于这种类型的参数曲线,法线的参数方程式由以下方程式给出:

enter image description here

尝试:

import sympy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib notebook

t,t_0 = sp.symbols('t t_0',real=True)
r_x = sp.sin(3*t)
diff_r_x = sp.diff(r_x, t)
r_y = sp.sin(4*t)#typo has been edited
diff_r_y = sp.diff(r_y, t)

para_eqx = r_x.subs(t, t_0) + diff_r_x.subs(t, t_0)*(t-t_0)#paremeter eq. of the normal defined
para_eqy = r_y.subs(t, t_0) - diff_r_x.subs(t, t_0)*(t-t_0)#paremeter eq. of the normal defined

r_x_normal = para_eqx.subs(t_0, 2*sp.pi/5)#plugging in t_0 = 2*sp.pi/5
r_y_normal = para_eqy.subs(t_0, 2*sp.pi/5)#plugging in t_0 = 2*sp.pi/5

t_range_normal = np.linspace(0, 250, 100) #from here on I have no clear idea on what is wrong.

xmarks = sp.lambdify(t, r_x_normal, "numpy")(t_range_normal)
ymarks = sp.lambdify(t, r_y_normal, "numpy")(t_range_normal)

fig, ax = plt.subplots(1)
complete_curve = ax.plot(xmarks, ymarks, ":", color="grey", alpha=0.5)
piece_of_curve = ax.plot(xmarks[:51], ymarks[:51], color="blue")
ax.plot(xmarks[50], ymarks[50], "o", color="blue")

plt.show()

我正在努力评估这些方程的t值(由t_range_normal给出)。我使用了lambdify,然后使用蓝线在图上绘制法线。

但是,我得到了

enter image description here

哪个不正确。我一定在...上缺少t_range_normal = np.linspace(0,250,100)的东西。

谢谢。

1 个答案:

答案 0 :(得分:3)

下面是您的代码,让我们逐步进行操作:

import numpy as np
import sympy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt

t,t_0 = sp.symbols('t t_0',real=True)
r_x = sp.sin(3*t)
diff_r_x = sp.diff(r_x, t)
r_y = sp.sin(4*t)
diff_r_y = sp.diff(r_y, t)
r_x_eq= r_x.subs(t, t_0)
r_y_eq = r_y.subs(t, t_0)

r_x_eq
Out: sin(3*t_0)
r_y_eq
Out: sin(4*t_0)

r_x_eq.subs(t_0, 2*sp.pi/5)
Out: -sqrt(-sqrt(5)/8 + 5/8)
r_y_eq.subs(t_0, 2*sp.pi/5)
Out: -sqrt(-sqrt(5)/8 + 5/8)

这是正确的,因为您正在单位圆周围进行一整圈,并且sin(0)= sin(360)= sin(720)等。

x和y的参数函数的第二项相同(但符号相反)(根据您在问题中发布的数字):

para_eqx = r_x.subs(t, t_0) + diff_r_x.subs(t, t_0)*(t-t_0)#paremeter eq. for the normal defined
para_eqy = r_y.subs(t, t_0) - diff_r_x.subs(t, t_0)*(t-t_0)#paremeter eq. for the normal defined

因此,您的两个功能是:

r_x_normal = para_eqx.subs(t_0, 2*sp.pi/5)#plugging in t_0 = 2*sp.pi/5
r_x_normal
Out[:]: 3*(-sqrt(5)/4 - 1/4)*(t - 2*pi/5) - sqrt(-sqrt(5)/8 + 5/8)

r_y_normal = para_eqy.subs(t_0, 2*sp.pi/5)#plugging in t_0 = 2*sp.pi/5
r_y_normal
Out[:]: -3*(-sqrt(5)/4 - 1/4)*(t - 2*pi/5) - sqrt(sqrt(5)/8 + 5/8)

因此,对于每个给定的t,它们只会相差一个常数。

t_range_normal = np.linspace(0, 250, 100) #from here on I have no clear idea on what is wrong.

xmarks = sp.lambdify(t, r_x_normal, "numpy")(t_range_normal)
ymarks = sp.lambdify(t, r_y_normal, "numpy")(t_range_normal)

fig, ax = plt.subplots(1)
complete_curve = ax.plot(xmarks, ymarks, ":", color="grey", alpha=0.5)
piece_of_curve = ax.plot(xmarks[:51], ymarks[:51], color="blue")
ax.plot(xmarks[50], ymarks[50], "o", color="blue")

plt.show()