9

Is there any possibility to plot an alternating sequence?

Picture: Alternating sequence:

enter image description here

I don't need exacly the same output as above.

I tried the following:

\begin{tikzpicture}
  \begin{axis}[
  ymin = -1,
  ymax = 1,
  xmin = 0,
  xmax = 4,
  axis x line=center,
  axis y line=left]
    \addplot[domain=1:5, only marks]{(-1)^x};
  \end{axis}
\end{tikzpicture}

What I got is:

enter image description here

Any idea?

  • What is the value of (-1)^{1.5} ? – Paul Gaborit Jun 24 '12 at 16:15
11

You need to make sure that you're evaluating your function at the right points. If you've set the domain to domain=1:5, you should set the number of samples to samples=5, then you'll get points for x=1,2,...,5.

Alternatively, you can explicitly provide a list of sampling points using samples at={1,...,5}.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[
  ymin = -1,
  ymax = 1,
  xmin = 0,
  xmax = 5,
  axis x line=center,
  axis y line=left]
    \addplot[samples at={1,...,5}, only marks]{(-1)^x};
  \end{axis}
\end{tikzpicture}

\end{document}

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.