8

Consider the following MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.18}
\begin{document}
            \begin{tikzpicture}
                \begin{axis}[
                    axis equal,
                    axis lines = middle,
                    xlabel = $x$,
                    ylabel = $y$,
                    xmin = -0.4, xmax = 0.4,
                    ymin = -0.4, ymax = 0.4,
                    grid = both,
                    grid style = {line width=.1pt, draw=gray!10},
                    major grid style = {line width=.2pt, draw=gray!50},
                    samples = 200,
                    legend pos = north west,
                    width = 10cm,
                    height = 8cm
                ]

                % Plot the curve
                \addplot[domain=-0.4:0.4, thick, blue] {x^2*sin(1/x)};
                \addlegendentry{$y = x^2\sin(1/x)$}
                

                % Tangent line at (1, -2)
                \addplot[domain=-0.5:0.5, thick, red] {x^2};
                \addplot[domain=-0.5:0.5, thick, red] {-x^2};
                \addlegendentry{$y=\pm x^2$}
                \end{axis}
            \end{tikzpicture}
\end{document}

Currently, I am getting:

tmp1

But I would like the curve to show like in the following image:

tmp2

What am I missing?

3 Answers 3

12

The trigonometric functions like sin take their argument in degrees, your plot assumes radians, so you have to convert them (that's what the deg function is there for):

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    axis equal,
    axis lines = middle,
    xlabel = $x$,
    ylabel = $y$,
    xmin = -0.4, xmax = 0.4,
    ymin = -0.4, ymax = 0.4,
    grid = both,
    grid style = {line width=.1pt, draw=gray!10},
    major grid style = {line width=.2pt, draw=gray!50},
    samples = 200,
    legend pos = north west,
    width = 10cm,
    height = 8cm
  ]

    % Plot the curve
    \addplot[domain=-0.5:0.5, thick, blue] {x^2*sin(deg(1/x))};
    \addlegendentry{$y = x^2\sin(1/x)$}


    % Tangent line at (1, -2)
    \addplot[domain=-0.5:0.5, thick, red] {x^2};
    \addplot[domain=-0.5:0.5, thick, red] {-x^2};
    \addlegendentry{$y=\pm x^2$}
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • Thanks. Appreciate the clarification! Commented 2 days ago
9

I'm happy to share the code from an old project I worked on with pgfplots. The code includes a plot with multiple functions, including an oscillatory function and a parabola. I have not checked your code but I think that the problem is here: sin(deg(1/x)). Here's the code:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{arrows}
\begin{document}
\begin{figure}[hp]
\centering
\begin{tikzpicture}[line cap=round, line join=round, >=triangle 45, x=1cm, y=1cm]
\begin{axis}[ 
    axis equal,
    axis lines=middle,  
    xmin=-1, xmax=1,   
    ymin=-.25, ymax=1,    
    xtick={-1,0,1},  
    ytick={0,1},  
    samples=1000,  
    width=12cm,  
    height=8cm,  
    legend pos=north west,
    legend style={fill=gray!10,draw=none,cells={anchor=west}},
    enlargelimits=true
]
\addplot[green, thick, smooth,domain=-0.5:0.5] {x^2*(sin(deg(1/x)))};
\addlegendentry{$y = x^2\sin(1/x)$}
\addplot[magenta, thick, dotted, smooth,domain=-0.5:0.5] {x^2}; 
\addplot[domain=-0.5:0.5, dotted, thick, black] {-x^2};
\addlegendentry{$y=\pm x^2$}

\draw [magenta,thick, fill] (0,0) circle [radius=.5pt, scale=1/0.8];
\end{axis}  
\end{tikzpicture}
\end{figure}
\end{document}
2
  • 2
    Thanks for sharing! I will implement some of your settings into my graph. Commented 2 days ago
  • @azetina As you may have noticed, I pointed out that the problem was using deg. Best regards Commented 2 days ago
8

You can use a better tool: happily, also l3fp correctly computes x^2*sin(1/x) as zero when x is zero.

And l3fp assumes radians, which pgfmath doesn't.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{pgfmath-xfp}

\pgfmxfpdeclarefunction{sqsin}{1}{#1^2*sin(1/#1)}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    axis equal,
    axis lines = middle,
    xlabel = $x$,
    ylabel = $y$,
    xmin = -0.4, xmax = 0.4,
    ymin = -0.4, ymax = 0.4,
    grid = both,
    grid style = {line width=.1pt, draw=gray!10},
    major grid style = {line width=.2pt, draw=gray!50},
    samples = 200,
    legend pos = north west,
    width = 10cm,
    height = 8cm
  ]

    % Plot the curve
    \addplot[domain=-0.5:0.5, thick, blue] {sqsin(x)};
    \addlegendentry{$y = x^2\sin(1/x)$}


    % Tangent line at (1, -2)
    \addplot[domain=-0.5:0.5, thick, red] {x^2};
    \addplot[domain=-0.5:0.5, thick, red] {-x^2};
    \addlegendentry{$y=\pm x^2$}
  \end{axis}
\end{tikzpicture}
\end{document}

output

3
  • 2
    Missing parentheses? +1 anyway, I didn't know the pgfmath-xfp package. Commented yesterday
  • 4
    Such a great and useful package! :) Commented yesterday
  • Thanks for sahring :D Commented yesterday

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.