Can't draw a trig function

Is there something wrong?

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=0.50]
\draw[thick, ->] (-10,0) -- (10,0)node[pos=1,below]{$x$};
\draw[thick, ->] (0,-3) -- (0,2)node[pos=1,left]{$y$};

\draw[thick, red, samples=100, domain={-3}:{-.01}] plot (\x,{(\x*cos((\x)r)-sin((\x)r))/(\x-sin((\x)r))});

\draw[thick, red, samples=100, domain={.01}:{3}] plot (\x,{(\x*cos((\x)r)-sin((\x)r))/(\x-sin((\x)r))});

\node[circle, inner sep=1pt, fill=white, draw=black] at (0,1){};
\node[circle](d) at (4,1){$y=\frac{\eta\mu x}{x}$};
\end{tikzpicture}
\end{document}

• Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
– Community Bot
Oct 16 at 15:48
• i wrote this code and doesnt work to plot the trig function. which is my fault? Oct 16 at 15:49
• I made your fragment into a test document, it gives the error ! Dimension too large. Oct 16 at 16:01
• thanks for help me. what does it mean dimensions too large. i put x\in[-1,1] but doesnt work again. Oct 16 at 16:16
• Avoid dividing by 0, use a simple syntax and add trig format = rad. Or consider to use pgfplots instead of pure TikZ.
– cis
Oct 16 at 16:18

Welcome to TeX.SE!!!

It looks like the problem could be related to TikZ precision. The denominator approaches to fast to 0 at x=0, and the subtraction doesn't help. A workaround: draw by hand the point that joins the two domains.

Something like this:

\documentclass[tikz,border=2mm]{standalone}

\begin{document}
\begin{tikzpicture}[scale=0.50]
\draw[thick, ->] (-10,0) -- (10,0) node[pos=1,below]  {$x$};
\draw[thick, ->] (0,-3)  -- (0,2)  node[pos=1,left]   {$y$};
\draw[thick, red] plot [samples=100, domain=-10:-0.5] (\x,{(\x*cos(\x r)-sin(\x r))/(\x-sin(\x r))})
-- (0,-2) -- plot [samples=100, domain=0.5:10]   (\x,{(\x*cos(\x r)-sin(\x r))/(\x-sin(\x r))});
\node[circle, inner sep=1pt, fill=white, draw=red] at (0,-2) {};
\node[circle] (d) at (4,1) {$y=\frac{\eta\mu x}{x}$};
\end{tikzpicture}
\end{document}


I think that the output looks good:

Edit: A cleaner version, suggested by Black Mild in the comments. It produces almost the same output (the only change is the scale now only over the x-axis):

\documentclass[tikz,border=2mm]{standalone}

\begin{document}
\begin{tikzpicture}[xscale=.5,thick,samples=100]
\draw[->] (-10,0) -- (10,0) node[below] {$x$};
\draw[->] (0,-3)  -- (0,2)  node[left]  {$y$};
\def\myf{(\x*cos(\x r)-sin(\x r))/(\x-sin(\x r))}
\draw[red] plot [domain=-10:-0.5] (\x,{\myf}) -- (0,-2) -- plot [domain=0.5:10] (\x,{\myf});
\path (0,-2) node[circle, inner sep=1pt, fill=white, draw=red] {}
(4,1)  node[circle] {$y=\frac{\eta\mu x}{x}$};
\end{tikzpicture}
\end{document}

• Do you mind I clean a bit? \documentclass[tikz,border=2mm]{standalone} \begin{document} \begin{tikzpicture}[xscale=.5,thick,samples=100] \draw[->] (-10,0) -- (10,0) node[below]{$x$}; \draw[->] (0,-3) -- (0,2) node[left] {$y$}; \def\myf{(\x*cos(\x r)-sin(\x r))/(\x-sin(\x r))} \draw[red] plot [domain=-10:-0.5] (\x,{\myf}) --(0,-2)-- plot [domain=0.5:10] (\x,{\myf}); \path (0,-2) node[circle,inner sep=1pt,fill=white,draw=red]{} (4,1) node[circle]{$y=\frac{\eta\mu x}{x}$}; \end{tikzpicture} \end{document} Oct 16 at 17:38
• @BlackMild, I added your version, which is better, of course!! Oct 16 at 18:14

With Asymptote, we can put the "critical" value y=-2 in the definition of the function, so we can get the graph without tricks

graph(f,-10,10,n=200,operator..)


where n=200 is the number of sample points, and operator.. means curvy connecting that is necessary in this case.

// http://asymptote.ualberta.ca/
unitsize(8mm,2cm);
size(8cm);
import graph;

real f(real x) {
if (x==0) return -2;
else
return (x*cos(x)-sin(x))/(x-sin(x));
};

draw(graph(f,-10,10,n=200,operator..),red);
xaxis("$x$",Arrow(TeXHead));
yaxis("$y$",-2.5,1.5,Arrow(TeXHead));
label("The graph of $y=\frac{x\cos x-\sin x}{x-\sin x}$",point(N),2N);
label("$-2$",(0,-2),SW);

shipout(bbox(5mm,invisible));


Appendix The function f(x) is asymptotic to cos(x) when |x| is big.

unitsize(1cm,4cm);
size(8cm);
import graph;
import math;   //for drawline

real f(real x) {
if (x==0) return -2;
else
return (x*cos(x)-sin(x))/(x-sin(x));
};

draw(graph(cos,-20,20,n=200,operator..),lightblue+1pt);
draw(graph(f,-20,20,n=200,operator..),red+.5pt);
xaxis("$x$",Arrow(TeXHead));
yaxis("$y$",-2.5,2.5,Arrow(TeXHead));

drawline((0,1),(1,1),gray+.2pt);
drawline((0,-1),(1,-1),gray+.2pt);
drawline((0,-2),(1,-2),gray+.2pt);

label(scale(.7)*"$1$",(0,1),NE);
label(scale(.6)*"$-1$",(0,-1),SW);
label(scale(.6)*"$-2$",(0,-2),SW);
label("$\color{red}y=\frac{x\cos x-\sin x}{x-\sin x}$"+" and "+"$\color{blue}y=\cos x$",point(N),2N);

shipout(bbox(5mm,invisible));

• Maybe return (x==0) ? -2 : (x*cos(x)-sin(x))/(x-sin(x));. Oct 17 at 16:37
• @NguyenVanChi1998 Yes, that is more concise syntax real f(real x) { return (x==0) ? -2 : (x*cos(x)-sin(x))/(x-sin(x)); };  Oct 17 at 16:41