reflecting with respect to a line

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


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}

Tags:

Tikz Pgf