2

I am trying to see if there is any way to simplify the following code: I have several functions, and one line. I would like to draw the tangent of each functions where they intersect the line.

Currently all is done manually: I calculated the coordinate manually, as well as the slope. I was wondering if there was anyway to simplify, especially given the fact that we can get the intersection of the line and the functions using the tikz library intersections.

Thanks in advance for the help.

Here is my code:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
    [declare function={ f(\x,\a)   = \a/\x;}]

    \begin{axis}[xmin= 0, xmax = 5,ymin=0, ymax = 5,]

    \addplot[black,domain=0.3:4.5,name path= IC1] {f(\x,1)}; 
    \addplot[black,domain=0.7:4.5,name path= IC2] {f(\x,3)}; 
    \addplot[black,domain=1:4.5,name path= IC3] {f(\x,5)}; 

    \addplot[blue,dashed,name path = ray1] {3*\x};

    \path [name intersections={of=ray1 and IC1}]; %not used

    \addplot [blue] coordinates {({sqrt(1/3) + 0.2},{sqrt(3*1) -0.6}) ({sqrt(1/3) - 0.2},{sqrt(3*1) +0.6})};
    \addplot [blue] coordinates {({sqrt(3/3) + 0.2},{sqrt(3*3) -0.6}) ({sqrt(3/3) - 0.2},{sqrt(3*3) +0.6})};
    \addplot [blue] coordinates {({sqrt(5/3) + 0.2},{sqrt(3*5) -0.6}) ({sqrt(5/3) - 0.2},{sqrt(3*5) +0.6})};

    \end{axis}

\end{tikzpicture}
\end{document}

enter image description here

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.