Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
What is the value of (-1)^{1.5} ? –  Paul Gaborit Jun 24 '12 at 16:15

1 Answer 1

up vote 11 down vote accepted

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}
share|improve this answer
    
That's it! Thank you very much :) –  Raubi Jun 24 '12 at 16:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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