Graphics, Figures & TablesStrangely looking trigonometric function plots?

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

Strangely looking trigonometric function plots?

Post by ziezi »

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

Code: Select all

\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
goal.JPG (69.62 KiB) Viewed 4140 times
current.JPG
current.JPG (42.16 KiB) Viewed 4145 times
Last edited by ziezi on Mon Sep 17, 2018 5:39 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

Strangely looking trigonometric function plots?

Post by Stefan Kottwitz »

Perhaps remove the factor pi in the argument:

Code: Select all

\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
User avatar
Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

Strangely looking trigonometric function plots?

Post by Stefan Kottwitz »

By the way, math operators are commonly written upright, not italic, there are macros for it:

Code: Select all

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

Strangely looking trigonometric function plots?

Post by ziezi »

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.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

Strangely looking trigonometric function plots?

Post by Stefan Kottwitz »

Try this, notice the xticks settings:

Code: Select all

\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
cos-plot.png (23.12 KiB) Viewed 4129 times
Stefan
LaTeX.org admin
ziezi
Posts: 3
Joined: Mon Sep 17, 2018 5:02 pm

Strangely looking trigonometric function plots?

Post by ziezi »

Thanks again for the suggestion! It worked great!

I hope that by clicking accept on the last answer I've closed/completed the question.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

Strangely looking trigonometric function plots?

Post by Stefan Kottwitz »

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
Post Reply