# Graphing a simple exponential function

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:

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

What could I do to actually make it work?

• 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
• 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? May 16, 2017 at 6:58

\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}

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

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)$},
]
\addlegendentry{$\frac{9}{10}\cdot\left(-1^{-x}+1\right)$}