Is pgfplots able to draw a function such as $f(x) = x^2 \sin(1/x) + x/2$?
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}
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}
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}