## 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:

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
\documentclass[a4paper]{article}\usepackage{pgfplots}\pgfplotsset{compat=1.14} \begin{document}\begin{tabular}{@{}c@{}}\begin{tikzpicture}	\begin{axis}[		width = \axisdefaultwidth,		height = 0.3\textheight,		xlabel = $x$,		ylabel = $\psi(x)$,		title = {Wavefunction in periodic potential.},		xmin = 0, xmax = 10,		ymin = -1.2, ymax = 1.2,		xtick = {0, 2, ..., 10},        	xticklabels = {$0$, $a$, $2a$, $3a$, $4a$, $5a$},	] 	\addplot [color = red, domain = 0 : 10, smooth]  plot {cos(deg(\x*pi))} 		node [pos = 0.3, above right] {$cos(x)$};	\addplot [color = blue, domain = 0 : 10, smooth]  plot {sin(deg(\x*pi))} 		node [pos = 0.9, above left] {$sin(x)$}; 	\addlegendentry{$\psi_{+}(x)$};	\addlegendentry{$\psi_{-}(x)$};	\end{axis}\end{tikzpicture}\\\begin{tikzpicture}	\begin{axis}[		width = \axisdefaultwidth,		height = 0.3\textheight,		xlabel = $x$,		ylabel = $\rho(x)$,		title = {Probability density in periodic potential.},		xmin = 0, xmax = 10,		ymin = 0, ymax = 2.5,		xtick = {0, 2, ...,10},        	xticklabels = {$0$, $a$, $2a$, $3a$, $4a$, $5a$},	] 	\addplot [color = red, domain = 0 : 10, smooth] plot {2*cos(deg(\x*pi))^2} 		node [pos= 0.3, above right] {$cos^{2}(x)$};	\addplot [color = blue, domain = 0 : 10, smooth] plot {2*sin(deg(\x*pi))^2} 		node [pos = 0.9, above left] {$sin^{2}(x)$}; 	\addlegendentry{$\rho_{+}(x)$};	\addlegendentry{$\rho_{-}(x)$};	\end{axis}\end{tikzpicture}\end{tabular}\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 1207 times
current.JPG (42.16 KiB) Viewed 1212 times
Last edited by ziezi on Mon Sep 17, 2018 5:39 pm, edited 1 time in total.

Tags:

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

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
\addplot [color = red, domain = 0 : 10,  smooth]  plot {cos(deg(x))}    node [pos = 0.3, above right] {$cos(x)$};\addplot [color = blue, domain = 0 : 10, smooth]  plot {sin(deg(x))}    node [pos = 0.9, above left] {$sin(x)$}; ... \addplot [color = red, domain = 0 : 10,  smooth] plot {2*cos(deg(x))^2}    node [pos= 0.3, above right] {$cos^{2}(x)$};\addplot [color = blue, domain = 0 : 10, smooth] plot {2*sin(deg(x))^2}    node [pos = 0.9, above left] {$sin^{2}(x)$};

Stefan
LaTeX.org admin

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

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
node [pos = 0.3, above right] {$\cos(x)$};node [pos = 0.9, above left] {$\sin(x)$};

Stefan
LaTeX.org admin

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
Site Admin
Posts: 9602
Joined: Mon Mar 10, 2008 9:44 pm
Try this, notice the xticks settings:

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
\documentclass[a4paper]{article}\usepackage{pgfplots}\pgfplotsset{compat=1.14}\pgfmathsetmacro{\PI}{pi}\begin{document}\begin{tikzpicture}	\begin{axis}[		width = \axisdefaultwidth,		height = 0.3\textheight,		xlabel = $x$,		ylabel = $\rho(x)$,		title = {Probability density in periodic potential.},		xmin = 0, xmax = 16,		ymin = 0, ymax = 2.5,		scaled x ticks={real:\PI},		xtick scale label code/.code={},		xtick distance=pi,	    	xticklabels = {,,$a$, $2a$, $3a$, $4a$, $5a$}	] 	\addplot [color = red, domain = 0 : 16, samples = 150, smooth] plot {2*cos(deg(x))^2} 		node [pos= 0.3, above right] {$\cos^{2}(x)$};	\addlegendentry{$\rho_{+}(x)$};	\addlegendentry{$\rho_{-}(x)$};	\end{axis}\end{tikzpicture}\end{document}

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

Stefan
LaTeX.org admin

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
Site Admin
Posts: 9602
Joined: Mon Mar 10, 2008 9:44 pm
Yes, that's perfect. Clicking the checkmark at that post that was the solution, marks the whole topic as solved (so supporters see it) and makes that solution the hyperlink when one clicks the checkmark at the top, so google searchers can jump to the solution.

Stefan
LaTeX.org admin

Return to “Graphics, Figures & Tables”

### Who is online

Users browsing this forum: No registered users and 7 guests