4

I think a similar question has been asked already, but I do not understand the answer(s). Given the graph of a function, I would like to draw the graph of the inverse function, that is I would like to be able to reflect for example a parabola with respect to the line y=x. Is there any way to achieve this in tikzpicture that is not too complicated? Thanks!

Marmot, given what you wrote below I thought this should be completely straightforward. (Thanks a lot, your answer was crystal clear!) All I did was to (1) copy your code twice and use mini pages (2) change the domain to be (-pi/2,pi/2) for the sine and (-1,1) for the arcsine (3) comment out the arcsine and the sine each in one fo the minipages so as not to have both graphs together.

Why is this not working???

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}

\begin{minipage}{.5\textwidth}
\begin{tikzpicture}[
declare function={ f(\x) = sin(\x*180/pi); %<- enter your function right here
                   a=1;
                   b=1;
                   reflectedx(\x)        = -\x+2*a*(\x*a+f(\x)*b)/(pow(a,2)+pow(b,2));
                   reflectedy(\x)        = -f(\x)+2*b*(\x*a+f(\x)*b)/(pow(a,2)+pow(b,2));
                 },
]
\begin{scope}[local bounding box=plots]
 \draw [blue,thick] plot [domain=-pi/2:pi/2,samples=100,smooth] ({\x}, {f(\x)});
 %\draw [red,thick] plot [domain=-4:4,samples=100,smooth] ({reflectedx(\x)},{reflectedy(\x)});
\end{scope}
\begin{scope}[on background layer]
\draw[thick,-latex] ([xshift=-2mm]plots.west |-0,0) -- ([xshift=4mm]plots.east
|-0,0) node[below left]{$x$};
\draw[thick,-latex] ([yshift=-2mm]plots.south -|0,0) -- ([yshift=4mm]plots.north
-|0,0) node[below left]{$y$};
\end{scope}
\end{tikzpicture}
\end{minipage}

\begin{minipage}{.5\textwidth}
\begin{tikzpicture}[
declare function={ f(\x) = sin(\x*180/pi); %<- enter your function right here
                   a=1;
                   b=1;
                   reflectedx(\x)        = -\x+2*a*(\x*a+f(\x)*b)/(pow(a,2)+pow(b,2));
                   reflectedy(\x)        = -f(\x)+2*b*(\x*a+f(\x)*b)/(pow(a,2)+pow(b,2));
                 },
]
\begin{scope}[local bounding box=plots]
 %\draw [blue,thick] plot [domain=-4:4,samples=100,smooth] ({\x}, {f(\x)});
 \draw [red,thick] plot [domain=-1:1,samples=100,smooth] ({reflectedx(\x)},{reflectedy(\x)});
\end{scope}
\begin{scope}[on background layer]
\draw[thick,-latex] ([xshift=-2mm]plots.west |-0,0) -- ([xshift=4mm]plots.east
|-0,0) node[below left]{$x$};
\draw[thick,-latex] ([yshift=-2mm]plots.south -|0,0) -- ([yshift=4mm]plots.north
-|0,0) node[below left]{$y$};
\end{scope}
\end{tikzpicture}
\end{minipage}
\end{document}
Tanda
  • 349
  • 1
    Please post the reference to the previous question and expand a bit on exactly what you do not understand about the answers. Also, post a MWE to generate the function (and thereby demonstrate that you have a working knowledge of tikzpicture). – Jeffrey J Weimer Sep 05 '18 at 21:34
  • With TiKZ, I don't know, but it is very simple with the \parametricplot command from the pst-plot module of pstricks. – Bernard Sep 05 '18 at 21:36
  • 1
    Or you could just draw the inverse function. I was astonished to learn that high school math DEFINES the inverse using mirror images, which I regard as an irrelevant detail. – John Kormylo Sep 05 '18 at 21:49
  • If this is one of the answers you do not understand could you please be so kind and tell me what needs to be explained better. –  Sep 05 '18 at 22:00
  • John Kormylo, who ever said that high school math defines the inverse using mirror images? – Tanda Sep 06 '18 at 05:29

2 Answers2

5

I apologize in advance if you cannot understand this answer either (but I also do not know which explanation I should add to my previous answer. The only thing that I can see is that the previous answer was using pgfplots. Assuming that this is the problem, here comes a TikZ only answer. (I do, however, believe that you'd be better off if you use pgfplots for this.) All you need to do is to punch in the function you want to "invert" at the marked place. (Of course, this is not really an inverse, just a reflection, if the function is not monotonous as in the example below.)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[
declare function={ f(\x) = sin(\x*180/pi); %<- enter your function right here
                   a=1;
                   b=1;
                   reflectedx(\x)        = -\x+2*a*(\x*a+f(\x)*b)/(pow(a,2)+pow(b,2));
                   reflectedy(\x)        = -f(\x)+2*b*(\x*a+f(\x)*b)/(pow(a,2)+pow(b,2));
                 },
]
\begin{scope}[local bounding box=plots]
 \draw [blue,thick] plot [domain=-4:4,samples=100,smooth] ({\x}, {f(\x)});
 \draw [red,thick] plot [domain=-4:4,samples=100,smooth] ({reflectedx(\x)},{reflectedy(\x)});
\end{scope}
\begin{scope}[on background layer]
\draw[thick,-latex] ([xshift=-2mm]plots.west |-0,0) -- ([xshift=4mm]plots.east
|-0,0) node[below left]{$x$};
\draw[thick,-latex] ([yshift=-2mm]plots.south -|0,0) -- ([yshift=4mm]plots.north
-|0,0) node[below left]{$y$};
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

ADDENDUM: As for why your code does "not work". I am using the standalone class, in which \textwidth is not what it is in other classes. If you switch to article (say), you'll be fine:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}

\begin{minipage}{.5\textwidth}
\begin{tikzpicture}[
declare function={ f(\x) = sin(\x*180/pi); %<- enter your function right here
                   a=1;
                   b=1;
                   reflectedx(\x)        = -\x+2*a*(\x*a+f(\x)*b)/(pow(a,2)+pow(b,2));
                   reflectedy(\x)        = -f(\x)+2*b*(\x*a+f(\x)*b)/(pow(a,2)+pow(b,2));
                 },
]
\begin{scope}[local bounding box=plots]
 \draw [blue,thick] plot [domain=-pi/2:pi/2,samples=100,smooth] ({\x}, {f(\x)});
 %\draw [red,thick] plot [domain=-4:4,samples=100,smooth] ({reflectedx(\x)},{reflectedy(\x)});
\end{scope}
\begin{scope}[on background layer]
\draw[thick,-latex] ([xshift=-2mm]plots.west |-0,0) -- ([xshift=4mm]plots.east
|-0,0) node[below left]{$x$};
\draw[thick,-latex] ([yshift=-2mm]plots.south -|0,0) -- ([yshift=4mm]plots.north
-|0,0) node[below left]{$y$};
\end{scope}
\end{tikzpicture}
\end{minipage}
%
\begin{minipage}{.5\textwidth}
\begin{tikzpicture}[
declare function={ f(\x) = sin(\x*180/pi); %<- enter your function right here
                   a=1;
                   b=1;
                   reflectedx(\x)        = -\x+2*a*(\x*a+f(\x)*b)/(pow(a,2)+pow(b,2));
                   reflectedy(\x)        = -f(\x)+2*b*(\x*a+f(\x)*b)/(pow(a,2)+pow(b,2));
                 },
]
\begin{scope}[local bounding box=plots]
 %\draw [blue,thick] plot [domain=-4:4,samples=100,smooth] ({\x}, {f(\x)});
 \draw [red,thick] plot [domain=-1:1,samples=100,smooth] ({reflectedx(\x)},{reflectedy(\x)});
\end{scope}
\begin{scope}[on background layer]
\draw[thick,-latex] ([xshift=-2mm]plots.west |-0,0) -- ([xshift=4mm]plots.east
|-0,0) node[below left]{$x$};
\draw[thick,-latex] ([yshift=-2mm]plots.south -|0,0) -- ([yshift=4mm]plots.north
-|0,0) node[below left]{$y$};
\end{scope}
\end{tikzpicture}
\end{minipage}
\end{document}

enter image description here

  • Actually, I am having trouble plotting the sine and its inverse. I think it is a matter of scale, but I do not know how to fix it. How can I show you the code? It is too long as a comment and I should not put it as an answer. As you can see I am really a rookie... – Tanda Sep 06 '18 at 07:45
3

Since you asked for reflecting a given graph about y=x, that is, getting graph of the converse function, I recommend 3 ways for doing that. For example the original graph is one of y=ln(x).

  1. Use [rotate=45,yscale=-1,rotate=-45] Options are implemented in the order from the right to the left: first rotate -45 degree to the x-axis, then reflecting about x-axis ([yscale=-1]), and finally you rotate 45 degrees back.

    Options [rotate=-45,xscale=-1,rotate=45] give the same result.

  2. Use [x={(0cm,1cm)},y={(1cm,0cm)}] This is geometric meaning of the inverse functions.

  3. Use ({ln(\x)},\x) instead of (\x,{ln(\x)}) This is the way that TikZ draws graphs.

Note: reflecting about y=-x, use [rotate=45,xscale=-1,rotate=-45] Also, you can reflecting about y=ax+b in the same way (shifting to original point first)! PS: Personally I like the first way. We can apply to not only axis symmetry, but also center symmetry, homothety, translation using rotate, rotate around, scale, scale around, and shift. TikZ is great!

The above three ways give the same picture. I hope that you understand this answer ^^ Below are complete codes.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\pagecolor{yellow!30}
\begin{document}
% The original graph y=ln(x)
\def\originpath{
    plot[domain=.1:4.5,smooth] (\x,{ln(\x)})
}
% reflecting y=ln(x) about y=x
% to get (the graph of) the inverse function y=e^x

% 1st way: use [rotate=-45,xscale=-1,rotate=45]
\begin{tikzpicture}
\draw (-2,-2)--(4,4) node[above]{$y=x$};
\draw[-stealth] (-3,0)--(5,0) node[below=1mm]{$x$};
\draw[-stealth] (0,-3)--(0,5) node[left=1mm]{$y$};
\draw[blue,thick] \originpath node[above]{$y=\ln x$};

\draw[magenta,thick,rotate=45,yscale=-1,rotate=-45] 
\originpath node[above]{$y=e^x$};
\end{tikzpicture}

% 2nd way: use [x={(0cm,1cm)},y={(1cm,0cm)}]
\begin{tikzpicture}
\draw (-2,-2)--(4,4) node[above]{$y=x$};
\draw[-stealth] (-3,0)--(5,0) node[below=1mm]{$x$};
\draw[-stealth] (0,-3)--(0,5) node[left=1mm]{$y$};
\draw[blue,thick] \originpath node[above]{$y=\ln x$};

\draw[magenta,thick,x={(0cm,1cm)},y={(1cm,0cm)}] 
\originpath node[above]{$y=e^x$};
\end{tikzpicture}

% 3rd way: change (\x,{ln(\x)}) to ({ln(\x)},\x)
\begin{tikzpicture}
\draw (-2,-2)--(4,4) node[above]{$y=x$};
\draw[-stealth] (-3,0)--(5,0) node[below=1mm]{$x$};
\draw[-stealth] (0,-3)--(0,5) node[left=1mm]{$y$};
\draw[blue,thick]
plot[domain=.1:4.5,smooth] (\x,{ln(\x)})
node[above]{$y=\ln x$};

\draw[magenta,thick]
plot[domain=.1:4.5,smooth] ({ln(\x)},\x)
node[above]{$y=e^x$};
\end{tikzpicture}

\end{document}
Black Mild
  • 17,569