2

Finding the inverse for this graph seems to be challenging so I now want to reflect the red part of the graph in the line y=x.

graph

How would do this in latex?

Thank you!

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\renewcommand{\thesection}{\arabic{section}}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{fancyhdr}
\usepackage{bigints}
\usepackage{color}
\usepackage{tcolorbox}
\usepackage{color,xcolor}
\usepackage{booktabs,array}
\usepackage{hyperref}
\usepackage{graphicx}
\usetikzlibrary{arrows}
\usepackage{polynom}
\usepackage{wallpaper}
\usetikzlibrary{shapes.geometric}
\usepgfplotslibrary{fillbetween}
\newenvironment{tightcenter}{
\setlength\topsep{0pt}
\setlength\parskip{0pt}
\begin{center}}{\end{center}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
axis line style=thick,
axis line style={->},
%minor tick num=5,
%grid=both,
%grid style={line width=.1pt, draw=gray!30},
%major grid style={line width=.2pt,draw=gray!50},
%ticks=none,
xmin=-0.5,
xmax=4.6,
ymin=-0.3,
ymax=1.1,
height=12cm,
width=15cm,
%label style={font=\normalsize},
xlabel=$x$,
ylabel=$y$,
ticks=none,
clip=false,
%every tick label/.append style={font=\tiny},
ylabel style={
    anchor=south,
    at={(ticklabel* cs:1.0)},
    yshift=1pt
},
xlabel style={
    anchor=west,
    at={(ticklabel* cs:1.0)},
    xshift=1pt
}
]
%\addplot[name path=func1,thick,color=black,samples=120,domain=-0.05:4.5] {2*x*exp(-2*x+1)};
\addplot[name path=func2,thick,color=black,samples=120,domain=-0.325:4.5] {2*x^2*exp(-2*x+1)};
\addplot[name path=func2,thick,color=red,samples=120,domain=1:4.5] {2*x^2*exp(-2*x+1)};
%
\node[below] at (axis cs:-0.07,0) {$O$};
%\addplot fill between[
%of = func1 and func2,
%soft clip={domain=0:1},
%every even segment/.style  = {gray,opacity=.3}
%];
\end{axis}
\end{tikzpicture}
\end{document}
Henri Menke
  • 109,596
Will Kim
  • 2,032
  • The x and y units of your graph is not balanced anymore, the line y=x does not have an angle of 45 degrees, so what do you expect then –  May 18 '19 at 06:22
  • I actually wanted to just sketch the inverse function of the curve restricted on the domain $[1,\infty)$ hence mentioned the reflection. Can this be done? – Will Kim May 18 '19 at 06:23
  • It can be done with transformation, but it is literally impossible to imagine reflections on a system in which the length of x unit vector is longer than the length of y unit vector –  May 18 '19 at 06:26
  • Related: https://tex.stackexchange.com/questions/72712/plot-inverse-function-in-pgfplots –  May 18 '19 at 06:37
  • See here for the reflection at arbitrary lines. –  May 18 '19 at 07:50
  • Or here .... –  May 18 '19 at 07:57
  • 1
    Too many unnecessary packages are loaded in your code. – Display Name May 18 '19 at 08:56
  • Also see https://tex.stackexchange.com/questions/449560/reflecting-with-respect-to-a-line/491516#491516 – Black Mild May 18 '19 at 17:54
  • @TheoldJouleV it is indeed impossible to image, as unit vectors are defined to have length 1, so you will never find x and y unit vectors that have different lengths from each other. But I don't see why you should not be able to reflect on arbitrary planes in a Euclidean space. – bers May 28 '19 at 06:15

3 Answers3

7

First of all, pure TikZ is shorter in this case – no need of such a long code. This is your graph

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=15cm/5.1,y=12cm/1.4]
\draw[->] (-.5,0) -- (4.6,0) node[below] {$x$};
\draw[->] (0,-.3) -- (0,1.1) node[left] {$y$};
\path (0,0) node[below left] {$O$};
\draw[thick] plot[samples=120,domain=-0.325:4.5]      (\x,{2*\x*\x*exp(-2*\x+1)});
\draw[thick,color=red] plot[samples=120,domain=1:4.5] (\x,{2*\x*\x*exp(-2*\x+1)});
\end{tikzpicture}
\end{document}

Secondly, your diagram is not balanced. See the gradient of the line y = x (blue) below for details. How should one reflect something on that line?

enter image description here

So I think I should balance it. Then reflecting is not difficult, here I use the trick described in page 368 of the manual.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1.5,>=stealth]
\draw[->] (-.5,0) -- (4.6,0) node[below] {$x$};
\draw[->] (0,-.5) -- (0,4.6) node[left] {$y$};
\path (0,0) node[below left] {$O$};
\draw[thick] plot[samples=120,domain=-0.325:4.5]      (\x,{2*\x*\x*exp(-2*\x+1)});
\draw[thick,color=red] plot[samples=120,domain=1:4.5] (\x,{2*\x*\x*exp(-2*\x+1)});
\draw (0,0) -- (1.3,1.3);
\begin{scope}[x={(0cm,1cm)},y={(1cm,0cm)}]
\draw[thick] plot[samples=120,domain=-0.325:4.5]       (\x,{2*\x*\x*exp(-2*\x+1)});
\draw[thick,color=blue] plot[samples=120,domain=1:4.5] (\x,{2*\x*\x*exp(-2*\x+1)});
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

  • I have not seen scope trick before. Could one also just change the places in the plotting? That is (f(x),x) instead of (x,f(x))? – mickep May 18 '19 at 17:50
  • @mickep I just use {scope} to make sure everything outside is not affected. You can read about this trick in the manual, as I stated. –  May 18 '19 at 18:05
5

If you need the visible reflection then you can use package pst-optic. Run the example with pdflatex --shell-escape <file>:

\documentclass{article}
\usepackage{pst-plot,pst-optic}
\usepackage{auto-pst-pdf}
\begin{document}

\psset{xunit=1.5,yunit=5}
\def\f(x){2*x*x*Euler^(-2*x+1)}

\begin{pspicture}(-5,-0.5)(5,1.5)
  \psaxes[labels=none,arrows=->](0,0)(-4.5,-0.1)(4.75,1.25)[$x$,-90][$y$,0]
  \psplot[algebraic,plotpoints=500,linewidth=1.5pt]{-0.325}{1}{\f(x)}
  \psplot[algebraic,plotpoints=500,linecolor=red,linewidth=1.5pt]{1}{4.5}{\f(x)}
  \psline[linestyle=dashed](1,1)
  \symPlan(0,0)(1,1){% reflect to given line
    \psplot[algebraic,plotpoints=500,linewidth=1.5pt]{-0.325}{1}{\f(x)}%
    \psplot[algebraic,plotpoints=500,linecolor=red,linewidth=1.5pt]{1}{4.5}{\f(x)}}
\end{pspicture}

\end{document}

enter image description here

user187802
  • 16,850
1

This allows you to reflect the function on an arbitrary line and without being restricted in your choice of compilers. a=b=1 is the x=y line.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[
declare function={ f(\x) = 2*\x*\x*exp(-2*\x+1); %<- 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 [thick] plot [domain=-0.325:1,samples=51,smooth] ({\x}, {f(\x)});
 \draw [blue,thick] plot [domain=1:4,samples=51,smooth] ({\x}, {f(\x)});
 \draw [thick] plot [domain=-0.325:1,samples=51,smooth] ({reflectedx(\x)},{reflectedy(\x)});
 \draw [red,thick] plot [domain=1:4,samples=51,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