11

I'm trying to draw the function described in the title on the interval $[-0.05,0.05]$, but all I get is a straight line:

$f(x) = x^2\sin(1/x) + \frac x 2$

This is my code:

\documentclass[a4paper,11pt,finnish]{report}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    clip=false,
    axis line style = thick,
    axis x line = middle,
    axis y line = middle,
    ticks = none,
    xlabel=$x$,
    xlabel style ={anchor=west},
    ylabel=$y$,
    ylabel style ={anchor=south}
    ]
    \addplot[domain=-0.05:-0.01,samples=1000] {x^2 * sin(1/x) + x/2};
    \addplot[domain=0.01:0.05,samples=1000] {x^2 * sin(1/x) + x/2}
        node [pos = 0.95,above left] {$f(x)$};
    \node at(axis cs: 0,0) [circle,fill,scale=0.3]  {};
\end{axis}
\end{tikzpicture}
\end{document}

What I would like to see is the fluctuations in the function present in this interval. Is this simply a matter of precision, or something else?

2
  • 3
    The sin function expects input in degrees, you're telling it to plot over a set of radian values where the plot is non-linear, but if those numbers are taken to be degrees the plot is linear. This answer should cover things sufficiently.
    – Dai Bowen
    Commented Jan 27, 2018 at 13:02
  • Using radians was the answer to my problem.
    – sesodesa
    Commented Jan 27, 2018 at 15:43

2 Answers 2

10

sin uses degrees. You can use gnuplot (needs -shell-escape); note that, for gnuplot, sin expects radians.

\documentclass[a4paper,11pt]{report}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    clip=false,
    axis line style = thick,
    axis x line = middle,
    axis y line = middle,
    ticks = none,
    xlabel=$x$,
    xlabel style ={anchor=west},
    ylabel=$y$,
    ylabel style ={anchor=south}
    ]
    \addplot [domain=-0.05:-0.01,samples=1000] gnuplot {x^2 * sin(1/x) + x/2};
    \addplot [domain=0.01:0.05,samples=1000] gnuplot {x^2 * sin(1/x) + x/2}
        node [pos = 0.95,above left] {$f(x)$};
    \node at(axis cs: 0,0) [circle,fill,scale=0.3]  {};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Or you can convert into radians:

\documentclass[a4paper,11pt]{report}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    clip=false,
    axis line style = thick,
    axis x line = middle,
    axis y line = middle,
    ticks = none,
    xlabel=$x$,
    xlabel style ={anchor=west},
    ylabel=$y$,
    ylabel style ={anchor=south}
    ]
    \addplot [domain=-0.05:-0.01,samples=1000] {x^2 * sin((1/x)r) + x/2};
    \addplot [domain=0.01:0.05,samples=1000] {x^2 * sin((1/x)r) + x/2}
        node [pos = 0.95,above left] {$f(x)$};
    \node at(axis cs: 0,0) [circle,fill,scale=0.3]  {};
\end{axis}
\end{tikzpicture}
\end{document}
7

In the meantime, here is a way to do it with MetaPost, for whom it may interest, which could serve to show what kind of curve it is. Code included in a LuaLaTeX program for convenience.

By the way, whether with PGFplots, MetaPost or anything else, one should take care to choose an sufficiently small increment for x, if it is the same along the whole x-axis. 10^(-3), for example, would not be small enough.

\documentclass[border=3mm]{standalone}
\usepackage{luatex85, luamplib}
    \mplibsetformat{metafun}
    \mplibnumbersystem{double}
\begin{document}
\begin{mplibcode}
beginfig(1);
    u = 100cm; v = 200cm; 
    xmax = .05; xstep = .05*1E-3; ymax = .0275; 
    drawarrow (-xmax*u, 0) -- (xmax*u, 0); label.bot(btex $x$ etex, (xmax*u, 0));
    drawarrow (0, -ymax*v) -- (0, ymax*v); label.lft(btex $y$ etex, (0, ymax*v));
    path curve;
    curve = function(2, "x", "x**2 * sin(1/x) + .5x", xstep, xmax, xstep) xyscaled (u, v);
    draw curve; draw curve rotated 180; 
endfig;
\end{mplibcode}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .