## LaTeX forum ⇒ Graphics, Figures & Tables ⇒ Strangely looking trigonometric function plots? Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
ziezi
Posts: 3
Joined: Mon Sep 17, 2018 5:02 pm

### Strangely looking trigonometric function plots?

I'm trying to plot simple trigonometric functions,sin(x), sin^{2}(x), with the following code:

1. \documentclass[a4paper]{article}
2. \usepackage{pgfplots}
3. \pgfplotsset{compat=1.14}
4.
5. \begin{document}
6. \begin{tabular}{@{}c@{}}
7. \begin{tikzpicture}
8. \begin{axis}[
9. width = \axisdefaultwidth,
10. height = 0.3\textheight,
11. xlabel = $x$,
12. ylabel = $\psi(x)$,
13. title = {Wavefunction in periodic potential.},
14. xmin = 0, xmax = 10,
15. ymin = -1.2, ymax = 1.2,
16. xtick = {0, 2, ..., 10},
17. xticklabels = {$0$, $a$, $2a$, $3a$, $4a$, $5a$},
18. ]
19.
20. \addplot [color = red, domain = 0 : 10, smooth] plot {cos(deg(\x*pi))}
21. node [pos = 0.3, above right] {$cos(x)$};
22. \addplot [color = blue, domain = 0 : 10, smooth] plot {sin(deg(\x*pi))}
23. node [pos = 0.9, above left] {$sin(x)$};
24.
25. \addlegendentry{$\psi_{+}(x)$};
26. \addlegendentry{$\psi_{-}(x)$};
27. \end{axis}
28. \end{tikzpicture}
29. \\
30. \begin{tikzpicture}
31. \begin{axis}[
32. width = \axisdefaultwidth,
33. height = 0.3\textheight,
34. xlabel = $x$,
35. ylabel = $\rho(x)$,
36. title = {Probability density in periodic potential.},
37. xmin = 0, xmax = 10,
38. ymin = 0, ymax = 2.5,
39. xtick = {0, 2, ...,10},
40. xticklabels = {$0$, $a$, $2a$, $3a$, $4a$, $5a$},
41. ]
42.
43. \addplot [color = red, domain = 0 : 10, smooth] plot {2*cos(deg(\x*pi))^2}
44. node [pos= 0.3, above right] {$cos^{2}(x)$};
45. \addplot [color = blue, domain = 0 : 10, smooth] plot {2*sin(deg(\x*pi))^2}
46. node [pos = 0.9, above left] {$sin^{2}(x)$};
47.
48. \addlegendentry{$\rho_{+}(x)$};
49. \addlegendentry{$\rho_{-}(x)$};
50. \end{axis}
51. \end{tikzpicture}
52. \end{tabular}
53. \end{document}

and I get strange results. The plots are way off of what they should be, i.e. the $sin(x)$ and $cos(x)$ are not smooth and drifting on the y-axis. The $sin^{2}(x)$ and $cos^{2}(x)$ are horrendous.

The intended result is to plot 5 peaks where the cos(x) has its maximums over a, 2a, 3a, etc. similar to the grey plots.

Why are the plots like this? What am I doing wrong?
Attachments
goal.JPG (69.62 KiB) Viewed 418 times
current.JPG (42.16 KiB) Viewed 423 times
Last edited by ziezi on Mon Sep 17, 2018 5:39 pm, edited 1 time in total.

Tags:

Stefan Kottwitz
Posts: 9285
Joined: Mon Mar 10, 2008 9:44 pm
Perhaps remove the factor pi in the argument:

1. \addplot [color = red, domain = 0 : 10, smooth] plot {cos(deg(x))}
2. node [pos = 0.3, above right] {$cos(x)$};
3. \addplot [color = blue, domain = 0 : 10, smooth] plot {sin(deg(x))}
4. node [pos = 0.9, above left] {$sin(x)$};
5.
6. ...
7.
8. \addplot [color = red, domain = 0 : 10, smooth] plot {2*cos(deg(x))^2}
9. node [pos= 0.3, above right] {$cos^{2}(x)$};
10. \addplot [color = blue, domain = 0 : 10, smooth] plot {2*sin(deg(x))^2}
11. node [pos = 0.9, above left] {$sin^{2}(x)$};

Stefan

Stefan Kottwitz
Posts: 9285
Joined: Mon Mar 10, 2008 9:44 pm
By the way, math operators are commonly written upright, not italic, there are macros for it:

1. node [pos = 0.3, above right] {$\cos(x)$};
2. node [pos = 0.9, above left] {$\sin(x)$};

Stefan

ziezi
Posts: 3
Joined: Mon Sep 17, 2018 5:02 pm
Thank you for the fast response!

You are right, removing pi from the argument makes things better, however, I don't know how to plot specific number of maximums, matched to specific points on the x-axis, corresponding with the maximums of the other plot below.

I've updated the question and included additional information.

Stefan Kottwitz
Posts: 9285
Joined: Mon Mar 10, 2008 9:44 pm
Try this, notice the xticks settings:

1. \documentclass[a4paper]{article}
2. \usepackage{pgfplots}
3. \pgfplotsset{compat=1.14}
4. \pgfmathsetmacro{\PI}{pi}
5. \begin{document}
6. \begin{tikzpicture}
7. \begin{axis}[
8. width = \axisdefaultwidth,
9. height = 0.3\textheight,
10. xlabel = $x$,
11. ylabel = $\rho(x)$,
12. title = {Probability density in periodic potential.},
13. xmin = 0, xmax = 16,
14. ymin = 0, ymax = 2.5,
15. scaled x ticks={real:\PI},
16. xtick scale label code/.code={},
17. xtick distance=pi,
18. xticklabels = {,,$a$, $2a$, $3a$, $4a$, $5a$}
19. ]
20. \addplot [color = red, domain = 0 : 16, samples = 150, smooth] plot {2*cos(deg(x))^2}
21. node [pos= 0.3, above right] {$\cos^{2}(x)$};
22. \addlegendentry{$\rho_{+}(x)$};
23. \addlegendentry{$\rho_{-}(x)$};
24. \end{axis}
25. \end{tikzpicture}
26. \end{document}

cos-plot.png (23.12 KiB) Viewed 407 times

Stefan

ziezi
Posts: 3
Joined: Mon Sep 17, 2018 5:02 pm
Thanks again for the suggestion! It worked great!

I hope that by clicking accept on the last answer I've closed/completed the question.

Stefan Kottwitz