Reputation: 49
I want to draw the normal of a curve at a specific point t_0 = 2*sp.pi/5.
The curve is given by the parametric equations x(t) = sin(3t) and y(y) = sin(4t) where t[0, 2pi]. For this type of parametric curve, the parameter equations for the normal line are given by the following equations:
Attempt:
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()
I am struggling to evaluate these equations for values of t (given by t_range_normal). I used lambdify, and then plot the normal on the figure using a blue line.
However, I get:
Which is incorrect. I must be missing something from t_range_normal = np.linspace(0, 250, 100) on...
Thank you.
Upvotes: 1
Views: 336
Reputation: 6483
Below is your code, let's go through it step by step:
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)
Which is correct as you are doing a full round around the unit circle and sin(0) = sin(360) = sin(720) etc. etc.
The second term of your parametric function is the same (but with opposed sign) for x and y (according to the figure you posted in your question):
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
Hence your two functions are:
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)
Hence, for each given t
they will differ only by a constant term.
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()
Upvotes: 3