# Parametric Plots Using Latex

I want to create a plot of the following parametric equations:

x(t)=t*sin(t), y(t)=t*cos(t)
-3pi/2 <= t <= 3pi/2


Also, I want to show the tangent line on the same chart, which is:

y-o = (-pi/2)(x-(pi/2))


I've never really played around with parametric plots and I am not particular on package. Whatever is easiest to use. Can anyone help me out on how I can do this? Thanks!

For fun, the pstricks way:

\documentclass[svgnames, x11names, border=3pt]{standalone}%
\usepackage[utf8]{inputenc}
\usepackage{pst-plot, pst-math, pst-text, auto-pst-pdf}%

\begin{document}

\psset{arrowinset=0.12, algebraic, plotstyle=curve, plotpoints=1000}%
\def\paracurve{t*sin(t)|t*cos(t)}
\def\tsup{\pscalculate{3*\psPiH}}
\begin{pspicture*}(-5.5,-5)(4,5)%
\psset{labels=none}
\psaxes[linecolor=LightSteelBlue3, ticksize =2pt -2pt, ]{->}(0,0)(-5.5,-5)(4,5)[$x$, -120][$y$, -135]
\uput[dl](0,0){$O$}
\uput[d](\psPiH, 0){$\frac{\pi}{2}$} \uput[-120](-\tsup, 0){$-\frac{3\pi}{2}$}
\uput[120](0,\psPi){$\pi$} \uput[-120](0,-\psPi){$-\pi$}
\parametricplot[linewidth=1.2pt, linecolor=IndianRed]{-\tsup}{\tsup}{\paracurve}
\psplotTangent[linecolor=Goldenrod1, linewidth=0.4pt]{\psPiH}{6}{\paracurve}
\uput[l](0,2.467){$\bigl(\frac{\pi}{2}\bigr)^{\!\scriptscriptstyle2}$}
\end{pspicture*}

\end{document}


• This one is perfect, since it sort of matches some of the other stuff I have done with pstricks. Love it. – azdatasci Oct 15 '19 at 16:52

Drawing the plot is very simple:

\draw plot[variable=\t,domain=-3*pi/2:3*pi/2,smooth,samples=51]
({\t*sin(\t)},{\t*cos(\t)});


where \t is the parameter. Here we use trig format=rad to switch to radians. For your convenience I added a style tangent at that attaches the tangent at a given t value. The components are just given by the t derivatives of your x(t) and y(t). The length is stored in tangent length. To get a tangent of length 2 at t=pi, say, you can write

\draw[blue,tangent length=2,tangent at=pi];


Full code:

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
[/utils/exec=\pgfmathsetmacro{\mylength}{%
veclen(#1*cos(#1)+sin(#1),-1*#1*sin(#1)+cos(#1))/\pgfkeysvalueof{/tikz/tangent
length}}]
({#1*sin(#1)},{#1*cos(#1)}) ++
({(#1*cos(#1)+sin(#1))/(2*\mylength)},{(-1*#1*sin(#1)+cos(#1))/(2*\mylength)})
-- ++ ({(-2*#1*cos(#1)-2*sin(#1))/(2*\mylength)},
{(2*#1*sin(#1)-2*cos(#1))/(2*\mylength)})}},
tangent length/.initial=1]
\draw plot[variable=\t,domain=-3*pi/2:3*pi/2,smooth,samples=51]
({\t*sin(\t)},{\t*cos(\t)});
\draw[tangent at=2];
\draw[blue,tangent length=2,tangent at=pi];
\draw[red,tangent length=3,tangent at=-pi/2];
\end{tikzpicture}
\end{document}


One can also attach tangents using decorations.markings but in your case the curve's parametrization is known so it might be better to do it analytically.

• I like this one and its flexibility to add tangent lines. I'm using pstricks for other stuff already, so the solution below is a natural implementation right now, but I'm going to keep this one in mind for future things. I like it. – azdatasci Oct 15 '19 at 16:54

An attempt with MetaPost, included in a LuaLaTeX program, using some macros of my own. The tangent is computed by MetaPost itself, no need to know its equation.

\documentclass[border=2mm]{standalone}
\usepackage{luatex85,luamplib,amsmath}
\mplibsetformat{metafun}
\mplibtextextlabel{enable}
\mplibnumbersystem{double}
\begin{document}
\begin{mplibcode}

vardef param_fcn (expr tmin, tmax, tstep)(text f_t)(text g_t) =
save t; t := tmin;
(f_t, g_t)
forever: hide(t := t+tstep) exitif t > tmax;
.. (f_t, g_t)
endfor
if t - tstep <> tmax: hide(t := tmax) .. (f_t, g_t) fi
enddef;

vardef f(expr t) = t * sin t enddef;
vardef g(expr t) = t * cos t enddef;

beginfig(1);

u = cm;
tmax = -tmin = 3*pi/2; tstep = .01;
path curve;  curve = param_fcn(tmin, tmax, tstep)(f(t))(g(t));
draw curve scaled u withcolor red;

s := pi/2; pair M, P, Q;
M = (f(s),g(s));
pair tgt; tgt = unitvector(direction (s-tmin)/tstep of curve);
P = M + 4tgt; Q = M - 4tgt; draw (P--Q) scaled u withcolor green;

xmax = ymax = 4.5; xmin = -5; ymin = -ymax ; label.llft("$O$", origin);
drawarrow (xmin*u,0) -- (xmax*u, 0); label.bot("$x$", (xmax*u,0));
drawarrow (0, ymin*u) -- (0, ymax*u); label.lft("$y$", (0,ymax*u));

for i = -3 upto 2:
if i<>0:
xi := i/2*pi*u;
draw (xi, -2bp) -- (xi, 2bp); fi
endfor;

for j = -2 upto 2:
if j<>0:
yj := j/2*pi*u;
draw (-2bp, yj) -- (2bp, yj); fi
endfor;

label.bot("$\pi$", (pi*u,0)); label.bot("$\dfrac{\pi}{2}$", (pi*u/2,0));
label.bot("$\dfrac{-\pi}{2}$", (-pi/2*u,0));
label.bot("$-\pi$", (-pi*u, 0)); label.llft("$\dfrac{-3\pi}{2}$", (-3pi/2*u,0));
label.urt("$\pi$", (0, pi*u)); label.lrt("$-\pi$", (0, -pi*u));

endfig;

\end{mplibcode}
\end{document}