3

As I'm quite new to LaTeX. I wanted to graph the following function

F_{(x)} = \dfrac{9}{10}\cdot(-1)^x+1 

or basically:

Exponential Function

I tried

\usepackage{pgfplots}
\usepackage[margin=0.5in]{geometry}

\pgfplotsset{width=10cm,compat=1.9}
\usepgfplotslibrary{external}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = $x$,
    ylabel = {$f(x)$},
]
\addlegendentry{$x^2 - 2x - 1$}
%Here the blue parabola is defined
\addplot [
    domain=-10:10,
    codomain=0:1,
    samples=100,
    ]
    {9/10(-1^(-x)+1)};
\addlegendentry{$x^2 + 2x + 1$}

\end{axis}
\end{tikzpicture}
\end{document}

I got quite horrible results: Result

What could I do to actually make it work?

3
  • 1
    The equation you present as an image and the one you implemented are different (sign in the exponent). May 16, 2017 at 3:17
  • Also the function is complex valued which pgfplots cannot handle. You have to plot real and imaginary part separate. May 16, 2017 at 3:26
  • 2
    What's your definition for (-1)^x for real x? You can define it for rationals with odd denominator, but is not continuous at any point of the domain. How can you expect it can be drawn?
    – egreg
    May 16, 2017 at 6:58

2 Answers 2

7

enter image description here

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

\begin{align*}
  f(x) &= \frac{9}{10} (-1)^x + 1
\intertext{Apply the rules of powers.}
  &= \frac{9}{10} e^{x \ln(-1)} + 1
\intertext{Here $\ln(-1)$ is the complex logarithm}
  \ln(-1) &= \ln(e^{i\pi}) = i \pi
\intertext{One has}
  f(x) &= \frac{9}{10} e^{i \pi x} + 1
\intertext{Split into real and imaginary part:}
  \operatorname{Re}[f(x)] &= \frac{9}{10} \cos(\pi x) + 1 \\
  \operatorname{Im}[f(x)] &= \frac{9}{10} \sin(\pi x)
\end{align*}

\begin{tikzpicture}
  \begin{axis}[
    axis lines=left,
    xlabel=$x$,
    ylabel={$f(x)$},
    domain=-10:10,
    samples=200,
    no markers]
    \addplot { 9/10 * cos(deg(pi*x)) + 1 };
    \addplot { 9/10 * sin(deg(pi*x)) };
  \end{axis}
\end{tikzpicture}

\end{document}
2
  • What if I prefer -i\pi as the logarithm of -1? ;-)
    – egreg
    May 16, 2017 at 6:58
  • 1
    @egreg Well, what if I actually preferred \ln(-1) = -3 i \pi? All branches are beautiful! May 16, 2017 at 7:03
4

The problem is that the function is only defined for an integer input. But you are trying to plot many more dots in between which are undefined. I have changed the number of samples so that x is always an integer.

It seems to be important to put an explicit * between 10 and bracket.

Furthermore I needed to add braces around the -1 and comment out the codomain option (because my system does not know it, maybe I am using an older version than you do).

Also note that this is the function from the code which is different from the one of the screenshot (here the +1 is in brackets). I have updated the \addlegendentry.

\documentclass[border=.5cm]{standalone}
\usepackage{pgfplots}

\pgfplotsset{width=10cm,compat=1.9}
\usepgfplotslibrary{external}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
            axis lines = left,
            xlabel = $x$,
            ylabel = {$f(x)$},
        ]
        \addplot [
            domain=-10:10,
            %codomain=0:1,
            samples=21,
            only marks,
        ]
        {9/10*((-1)^(-x)+1)};
        \addlegendentry{$\frac{9}{10}\cdot\left(-1^{-x}+1\right)$}
    \end{axis}
    \end{tikzpicture}
\end{document}

screenshot

For a continuous version of the function (from your screenshot, not the code) see Henri Menke's answer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .