7

I was wondering if it is possible to recreate the following diagram with tikz enter image description here This is an illustration of the squeeze theorem. The simpler version is to plot the following enter image description here But even with this one I couldn't get the exact plot. Here is My MWE

\documentclass[border=3mm,tikz]{standalone}
\usepackage{pgfplots} 
\begin{document} 
\begin{tikzpicture}
\begin{axis}[
    width=6cm,
 axis lines=middle,
 ticklabel style={fill=white},
 xmin=-1.5,xmax=1.5,
 ymin=-1.2,ymax=1.5,,
 xlabel=$x$,ylabel=$y$,
        ]
\addplot[blue,samples=50,domain=-1.5:-0.2,smooth] {x*x*sin(1/\x r)};
\addplot[blue,samples=1000,domain=-0.2:-0.02]   {x*x*sin(1/\x r)};
\addplot[blue,samples=1000,domain=0.02: 0.20]   {x*x*sin(1/\x r)};
\addplot[blue,samples=50,domain= 0.2: 1.5,smooth] {x*x*sin(1/\x r)};
\addplot[red,samples=50,domain=-1.5:1.5,smooth] {x*x};
\addplot[red,samples=50,domain=-1.5:1.5,smooth] {-x*x};
\end{axis}
\end{tikzpicture}
\end{document} 

Edited: Everyone seems to forget giving a solution to the first diagram. Here is MWE

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture} 
\draw[->] (-0.5,0) -- (6,0) node[below] {$x$};
    \draw[->] (0,-0.5) -- (0,5) node[left] {$y$}; 
      \draw[red,ultra thick] (-.5,1) to[out=-45,in=185,looseness=2] (4,2) to[out=0,in=135,looseness=1] node [at end,below] {$f$} (6,.5);
      \draw[blue,ultra thick] (-.5,2.5) to[out=25,in=160,looseness=1] (4,2) to[out=10,in=180,looseness=1] node [at end,above] {$h$} (6,3);
      \draw[green,ultra thick] (0.5,3.2) to[out=-45,in=175,looseness=1] (4,2) to[out=0,in=180,looseness=1] node [at end,below] {$g$} (6,2.5);
      \draw[dashed] (4,0) -- node[at start,below] {$a$} (4,2) -- node[at end,left] {$L$} (0,2);
    \end{tikzpicture}
\end{document}

Any modification (to recreate the first diagram) is appreciated!

6
  • 1
    That's just a question of domain, isn't it? Try with just \addplot[blue,line join=round,samples=1500,domain=-0.15:0.15] {x*x*sin(1/\x r)}; for the sine curve, and plot the parabolas in that same domain (-0.15:0.15). Oct 24, 2017 at 10:39
  • @TorbjørnT. Still not working.
    – Maryà
    Oct 24, 2017 at 10:48
  • Define "not working". I get imgur.com/a/chPxF with that. Oct 24, 2017 at 10:49
  • 1
    You need to change or remove the settings for ymin, ymax, xmin, xmax of course. Oct 24, 2017 at 10:50
  • Well, it's much easier to fix existing code than starting from scratch making something entirely different ... Oct 24, 2017 at 11:39

4 Answers 4

6

like this?

enter image description here

you need to increase frequency of sinusoidal curve ...

\documentclass[border=3mm,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=6cm,
 axis lines=middle,
 ticklabel style={fill=white},
 xmin=-1.5,xmax=1.5,
 ymin=-1.75,ymax=1.75,
 xlabel=$x$,ylabel=$y$,
 domain=-1.5:1.5,
 samples=200,
 smooth
        ]
\addplot[blue] {x*x*sin(4/\x r)};
\addplot[red] { x*x};
\addplot[red] {-x*x};
\end{axis}
\end{tikzpicture}
\end{document}
5

Here is answer enter image description here

\documentclass[usenames,dvipsnames]{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
\begin{tikzpicture}
\draw[->] (-0.5,0) -- (6,0) node[below] {$x$};
\draw[->] (0,-0.5) -- (0,5) node[left] {$y$};
\draw[red,thick] (-.5,1) to[out=-45,in=185,looseness=2] (4,2) to[out=0,in=135,looseness=1] node [at end,below] {$f$} (6,.6);
\draw[blue,thick] (-.5,2.5) to[out=25,in=160,looseness=1] (4,2) to[out=10,in=180,looseness=1] node [at end,above] {$h$} (6,3);
\draw[OliveGreen,thick] (0.5,3.2) to[out=-45,in=175,looseness=1] (4,2) to[out=0,in=180,looseness=1] node [at end,below] {$g$} (6,2.5);
\draw[dashed] (4,0) -- node[at start,below] {$a$} (4,2) -- node[at end,left] {$L$} (0,2);
\end{tikzpicture}
\end{document} 
1
  • My compliments for everybody...I had voted up some answers.
    – Sebastiano
    Nov 3, 2020 at 12:40
4

A try with MetaPost, both axes sharing the same graduation, but with the function sin(pi/x) instead of sin(1/x). It may at least give some ideas for the settings. To be typeset with LuaLaTeX.

\documentclass{article}
\usepackage{luamplib}
    \mplibsetformat{metafun}
    \mplibnumbersystem{double}
\begin{document}
\begin{mplibcode}
     vardef function(expr xmin, xmax, xstep)(text f_x) =
        save x; x := xmin;
        (x, f_x) forever: hide(x := x + xstep) exitif x > xmax; .. (x, f_x) endfor
        if x - xstep < xmax: hide(x := xmax) .. (x, f_x) fi
    enddef;
    beginfig(1);
        u = v = 6cm;
        xmax = -xmin = .75; ymax = -ymin = .6; xstep = .01;
        vardef f(expr x) = x**2 enddef;
        vardef g(expr x) = f(x)*sin(pi/x) enddef;

        drawarrow (xmin*u, 0) -- (xmax*u, 0);
        label.bot(btex $x$ etex, (xmax*u, 0));
        drawarrow (0, ymin*v) -- (0, ymax*v);
        label.lft(btex $y$ etex, (0, ymax*v));

        path parabola[]; 
        parabola1 = function(xmin, xmax, xstep)(f(x)) xyscaled (u, v);
        parabola[2] = parabola1 reflectedabout (origin, (1, 0)); 
        for i = 1, 2: draw parabola[i]; endfor;
        label.top(btex $y=x^2$ etex, point infinity of parabola1);
        label.bot(btex $y=-x^2$ etex, point infinity of parabola2);
        draw function(xmin, xmax, 1E-4)(g(x)) xyscaled (u, v) withcolor blue;
    endfig;
\end{mplibcode}
\end{document}

enter image description here

0
2

Ah, after three years ... it seems that you like to reproduce your first image, not the second (in the question, this was a bit misleading). It ca be simply done `pgfplots:

\documentclass[margin=3.14159mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{tikzpicture}[
lbl/.style = {fill=white, inner sep=1pt, font=\footnotesize}
                        ]
\begin{axis}[axis lines=middle,%center,
             axis line style= {-Straight Barb},
             xmin=-0.5, xmax=7,
             ymin=-0.5, ymax=6,
             xlabel=$x$, ylabel=$y$,
             label style = {below left},
             xtick=\empty, ytick=\empty,
             every axis plot post/.append style={very thick},
             no marks, smooth]
\addplot coordinates {(-0.5,2.5) (1.5,3) (4.2,2) (5.5,4) (6,4)} 
        node[above] {$h$};
\addplot coordinates {(-0.5,1) (1.5,0.1) (4.2,2) (6,1)}
        node[below] {$g$};
\addplot coordinates {(0.5,3.2) (4.2,2) (6,3)}
        node[below] {$f$};
\draw[dashed] (0,2) node[left] {$L$} -| (4.2,0) node[below] {$a$};
\end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

1
  • 1
    "Ah, after three years" :-)
    – Maryà
    Nov 3, 2020 at 12:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.