# How to draw tangens of arbitrary functions y=f(x) at given point (x,y) of f(x) using PStricks?

I'm relative new using PStricks and I don't know exactly how to draw a tangens of y=f(x) at a given point of the curve. I know, by simple mathematics that can be done by constructing the associate differential triangle at the point, [(x,y), dx, dy]. But, I think that PStricks should have a "easy-way" to do it automatically. Please, can you help me?

-
Welcome! Do you mean tangent? – Marco Daniel Aug 18 '12 at 14:58
yes, that comes from Deutsch – Miguel Toledo González Aug 18 '12 at 15:03

Pstricks consist of several packages. The basic on is pstricks.

A complete list of all available packages with a small description is given at tug.org/PStricks

To plot a function the package pst-plot is recommended. It provides the command \psplot.

Plotting a tangent of given function can easily be done be the command \psplotTangent which is provided by the package pstricks-add.

Here an example of the documentation:

\documentclass[pstricks]{standalone}

\begin{document}
\def\Fp{x RadtoDeg dup dup sin exch 2 mul sin 2 mul add exch 3 mul sin 3 mul add neg}
\psset{plotpoints=1001}
\begin{pspicture}(-7.5,-2.5)(7.5,4)%X\psgrid
\psaxes{->}(0,0)(-7.5,-2)(7.5,3.5)
\psplot[linewidth=3\pslinewidth]{-7}{7}{\F}
\psset{linecolor=red, arrows=<->, arrowscale=2}
\multido{\n=-7+1}{8}{\psplotTangent{\n}{1}{\F}}
\psset{linecolor=magenta, arrows=<->, arrowscale=2}%
\multido{\n=0+1}{8}{\psplotTangent[linecolor=blue, Derive=\Fp]{\n}{1}{\F}}
\end{pspicture}

\end{document}


-
Thanks a lot! That is perfect! – Miguel Toledo González Aug 18 '12 at 15:05
@MiguelToledoGonzález don't forget to accept Marco Daniel's answer (click on the green checkmark to the left). – Gonzalo Medina Aug 18 '12 at 15:11

My answer below adds the infix version to Marco Daniel's answer and provides some easy-to-customize settings as a template.

\documentclass[pstricks,border=0bp,12pt,dvipsnames]{standalone}

\usepackage[nomessages]{fp}

\FPset\TrigLabelBase{4}
\FPeval\XMin{0-pi}
\FPeval\XMax{2*pi}
\FPset\YMin{-3}
\FPset\YMax{3}

\FPeval\DeltaX{pi/TrigLabelBase}
\FPeval\DeltaY{1}

\FPeval\Left{XMin-DeltaX/2}
\FPeval\Right{XMax+DeltaX/2}
\FPeval\Bottom{YMin-DeltaY/4}
\FPeval\Top{YMax+DeltaY/4}

\newlength\Width\Width=12cm
\newlength\Height\Height=6cm

\newlength\urx\urx=15pt
\newlength\ury\ury=15pt
\newlength\llx\llx=-5pt
\newlength\lly\lly=-5pt

\psset
{
algebraic,
urx=\urx,
ury=\ury,
llx=\llx,
lly=\lly,
plotpoints=1000,
trigLabels,
trigLabelBase=\TrigLabelBase,
xAxisLabel=$x$,
yAxisLabel=$y$,
tickcolor=gray,
ticksize=0 -4pt,
labelFontSize=\scriptstyle,
}

% the same as \sum_{i=1}^{3} \frac{\cos(i x)}{i},
% the third arg represent increment step,
\def\f{Sum(i,1,1,3,cos(i*x)/i)}% is the same as \def\f{cos(x)+cos(2*x)/2+cos(3*x)/3}

% the first derivative of \f
\def\fp{Derive(1,\f)}

\begin{document}

\begin{psgraph}[dx=\DeltaX,dy=\DeltaY,linecolor=gray]{->}(0,0)(\Left,\Bottom)(\Right,\Top){\dimexpr\Width-\urx+\llx}{!}%{\dimexpr\Height-\ury+\lly}
\psplot[linecolor=NavyBlue]{\XMin}{\XMax}{\f}
\pstVerb{/xxx {Pi 4 div} def}%
\psset{arrows=<->}
\psplotTangent[linecolor=ForestGreen]{xxx}{3}{\f}% tangent line
\psplotTangent[linecolor=Maroon,Derive={-1/\fp}]{xxx}{3}{\f}% normal line
\end{psgraph}

\end{document}


-