python - 如何绘制给定参数曲线在某个点的法线

标签 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) 中遗漏了一些东西

谢谢。

最佳答案

下面是你的代码,让我们一步一步来:

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()

关于python - 如何绘制给定参数曲线在某个点的法线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55723461/

相关文章:

python - numpy.genfromtxt(),skiprows和skip_header有什么区别?

python - 如何获取 libicui18n.so.36 ?

python - 将标签数据添加到卷积神经网络的更好方法?

python - 如何使用 Sympy 进行向量的点/叉乘

python - 求解仅给定几个变量的简单方程组

python - 无法在 python 中使用 XPATH 获取文本值

python - 如何在seaborn.tsplot()中显示SD而不是SEM?

numpy - 用meshgrid和imshow绘图

python - matplotlib 在某些数据集上生成奇怪的 y 轴?

python - 使用 sympy 评估带有数值表达式的字符串?