2

I want to draw the function (sin(2x) + sin(3x))/(2sin(x)) with the command

\draw[blue,domain=-8.5:8.5,smooth, variable=\x, blue] plot ({\x},{(sin(2 * \x) + sin(3 *\x) )/ (2*sin(\x))});

but somehow I'm getting a "You've asked me to divide 0.0' by0.0', but I cannot divide any number by `0.0'" error.

It is rendered perfectly using gnuplot, so it is not supposed to happen "in theory". SO far, I'v tried to lead the following packages, as suggested by similar posts, but it doesn't work :

\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}

Remark : I don't wanna use external calls for instance with gnuplot to draw. This line of code up there is just a sample of my picture, and external calls will make it a nightmare to integrate properly.

I need a bit of help :(

Thanks ;-)

R. Absil
  • 151
  • Somehow, changing the upper command by "\draw[blue,domain=-8.5:8.5,smooth, variable=\x, blue, samples = 1000] plot ({\x},{(sin(2 * \x r) + sin(3 \x r) )/ (2sin(\x r))});" solves the problem... But I have no idea why. – R. Absil May 06 '16 at 08:43
  • 1
    Your function isn't defined at x=0 because the denominator is zero at that point. If you use an even number of samples, the function won't be evaluated at that point, but if you use an odd number of samples, one sample will lie directly on x=0. – Jake May 06 '16 at 08:45
  • Actually, it's quite simple, forcing the number of samples will prevent the function to be evaluated outside of its domain. – R. Absil May 06 '16 at 08:50
  • @Jake Yes, it works, but it's a bit flimsy. I tried with a three-way check, like plot ({\x},{(\x == 0 ? 2.5 : (sin(2 * \x) + sin(3 *\x) )/ (2*sin(\x))}); but it is still failing... (with x=-0.0006, at it) – Rmano May 06 '16 at 09:13

1 Answers1

1

Limiting expressions require a high resolution floating point computation which fpu tries its best to provide but still limited.

You can help pgfplots by increasing the sample numbers (notice the slight artifact at the peak) .

\begin{tikzpicture}
\begin{axis}
\addplot[draw=blue,trig format=rad,domain=-3*pi:3*pi,samples=701] {(sin(2*x) + sin(3*x) )/ (2*sin(x))};
\end{axis}
\end{tikzpicture}

enter image description here

But if everything fails, then use gnuplot backend via enabling --shell-escape and let Gnuplot do the weightlifting.

percusse
  • 157,807
  • Thanks for your answer. I knew the gnuplot solution would work, but since I'm heavily modifying my pic afterwards, I didn't want to use it as a first idea. – R. Absil May 08 '16 at 18:48