2

I am trying to draw a figure using tikz. As the figure involves graph of a trigonometric functions, I was hoping that Latex/Tikz could compute coordinates directly.

I tried the following code:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.3}

\begin{document}

\begin{tikzpicture}[scale=1, every node/.style={scale=1.4}]

%axes
\draw[->, >=stealth] (-1,0) -- (7,0);
\draw (7,0) node[right] {$x$};
\draw[->, >=stealth] (0,-1) -- (0,5);
\draw (0,5) node[above] {$y$};  

\draw[blue, very thick] plot[domain=1:6.28] ( {\x} , {1+\x/2+sin(\x r)} );

\draw[black, dashed] (1,0) -- (1,1+1/2+sin(1 r));
\draw[black, dashed] (6.28,0) -- (6.28,1+3.14+sin(6.28 r)); 
\end{tikzpicture}

\end{document}

My main problem is when drawing the two vertical dashed line. Latex returns

Package tikz Error: Giving up on this path. Did you forget a semicolon?. ...black, dashed] (1,0) -- (1,1+1/2+sin(1 r))

and similarly

Package tikz Error: Giving up on this path. Did you forget a semicolon?. ...hed] (6.28,0) -- (6.28,1+3.14+sin(6.28 r))

as errors. The figure appears as I want though (because the error arises at the end of the path).

Why did I get this error? My code includes a semicolon at the end of every line.

If the two lines in question are problematic, is there a way I can get Tikz to compute numerical values?

  • What's the r in sin(1 r)? – Herr K. Feb 25 '18 at 6:27
  • @HerrK. Don't I need it for radians? sin(\x) is for sin using \x in degrees, while sin(\x r) is for radians. I simply substituted the value 1 in the expression of my function. – Taladris Feb 25 '18 at 6:29
  • 2
    I tried the following and it worked: 1. comment out the last two lines (that draw the dashed lines); 2. delete the r in sin(\x r) and compile --- this should produce a blue straight line; 3. add the r back and compile again --- this should produce the correct sine curve; 4. uncomment the last two lines, and also "brace" the y-coordinates in both commands (the part that involve sin's); 5. compile one last time --- this should produce the desired result. I don't know why it works, but it seems to do the trick. – Herr K. Feb 25 '18 at 7:02
1

Just put the coordinates into a group.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.3}

\begin{document}

\begin{tikzpicture}[scale=1, every node/.style={scale=1.4}]

%axes
\draw[->, >=stealth] (-1,0) -- (7,0);
\draw (7,0) node[right] {$x$};
\draw[->, >=stealth] (0,-1) -- (0,5);
\draw (0,5) node[above] {$y$};  

\draw[blue, very thick] plot[domain=1:6.28] ( {\x} , {1+\x/2+sin(\x r)} );

\draw[black, dashed] (1,0) -- (1,{1+1/2+sin(1 r)});
\draw[black, dashed] (6.28,0) -- (6.28,{1+3.14+sin(6.28 r)}); 
\end{tikzpicture}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.