# Visualizing a function defined through a summation

So many times I visited this site to hopefully find an answer to my questions and many times I found what I was looking for, so first of all thanks to the most experts who share their knowledge.

I finished to have one question that I cannot solve: I need to graph the function on the image in its real and imaginary parts on a large enough x domain. To catch the real and imaginary parts actually doesn't scare me, because I can easily find them from the formula, but the problem is how to get a graph from a summation.

I found an answer to a similar problem here How to use a summation in a tikz plot, but I'm a passionate not practical with the deepest coding and I don't know how to remove the condition that the variable should be an integer, because I want a real one, with a certain domain and samples number.

So thanks to everyone that can help and have a good day! EDIT: Thanks for the grammar corrections. As was suggested in the comment I share my attempt, but i criticize it as first because the best I could do was just copying the answer given in the link above, to use it as a "base" and putting all the modifications that I thought were necessary. I use LaTeX since two years almost and I know the basics of TikZ and PgfPlots to do pretty figures, but I never had the necessity of coding in Lua. So the code is the following

\pgfplotsset{compat=newest}
\usepackage{luacode}
\begin{luacode*}
function p(x)
assert(x == math.floor(x), "x must be an integer")
res = 0
for k = -100, 100 do
res=res+((cos(k*x)*k+sin(x*k))/(k^2+1))
end
tex.sprint(res)
end
\end{luacode*}

\begin{document}

\begin{tikzpicture}[
declare function={p(\n) = \directlua{p(\n)};}
]
\begin{axis}[
use fpu=false,
xlabel=$x$, ylabel=$p(x)$,
domain=0:40,
samples=50,
only marks,
]
\end{axis}
\end{tikzpicture}
\end{document}


I perfectly know that it's not the best copying other people's code, but I'm here and I don't know what to do. So I accept every suggestion, even that of going to study that programming language: I just want to understand what tools do I need to improve in LaTeX and to solve problems as that of the question.

Thanks to everyone.

• Welcome to TeX.SE! It would be really helpful, however, if you showed us what you've tried and how it hasn't worked. At this point, you're asking us to do all the work; if you do as much as you can, it's much more likely someone will be able and willing to push you past the edge. – dgoodmaniii Feb 1 at 18:20
• You really want to make the question more specific. You have a sum over infinitely many k values. Clearly, pgf cannot perform such sums. Then you say "with a certain domain and samples number". Assuming you want a finite sum, those who might answer it will still need to know what domain. – Schrödinger's cat Feb 1 at 19:04
• Domain is not an issue, because I just need the qualitative behaviour of that formula. So k can stop at 1000 or 10000, but it is not vital to me and the same for the domain in x: I'll just go by attempts, to just get the "taste" of the phenomenon behind that formula (the formula came out in the Fourier Transform of an emission process). Thanks for the answers. – Rob Feb 1 at 19:20

You do not necessarily need lualatex for these sums. Also I do not understand how you arrived at the expression you wish to plot, my results are somewhat different and shown below. The following code has a new function sum. You can define the summand via the key of the same name. It is assumed to be a function of the summation index (\k, say) and the plot variable (\x, say) in that order. (I had to store this in a pgf key since pgfplots and tikz treat string type arguments passed to a function differently. I chose this because it can be translated to plain TikZ easily.) So in order to plot a sum, you need to define the summand, e.g.

declare function={imp(\k,\x)=-2*cos(\k*\x)/(1+\k*\k);}


and can then say

\addplot+[summand=imp] {-1+sum(100,\x)};


In what follows I post the full example together with what I got for real and imaginary parts.

\documentclass[fleqn]{article}
\usepackage[margin=1in]{geometry}
\usepackage[sumlimits]{amsmath}
\DeclareMathOperator{\re}{Re}
\DeclareMathOperator{\im}{Im}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\newcounter{isum}
\pgfplotsset{summand/.initial=max}
\pgfmathdeclarefunction{sum}{2}{%
\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\edef\myfun{\pgfkeysvalueof{/pgfplots/summand}}%
\pgfmathsetmacro{\mysum}{0}%
\pgfmathsetmacro{\myx}{#2}%
\pgfmathtruncatemacro{\imax}{#1}%
\setcounter{isum}{1}%
\loop
\pgfmathsetmacro{\mysum}{\mysum+\myfun(\value{isum},#2)}%
\ifnum\value{isum}<\imax\relax
\stepcounter{isum}\repeat
\pgfmathparse{\mysum}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\begin{document}
\begin{align}
p(x)&=\sum_{k=-\infty}^\infty\frac{\mathrm{e}^{\mathrm{i}\,k\,x}}{k+\mathrm{i}}
\notag\\
&=\frac{1}{\mathrm{i}}+\sum_{k=1}^\infty\left(
\frac{\mathrm{e}^{\mathrm{i}\,k\,x}}{k+\mathrm{i}}
+
\frac{\mathrm{e}^{-\mathrm{i}\,k\,x}}{-k+\mathrm{i}}\right)\notag\\
&=\frac{1}{\mathrm{i}}+\sum_{k=1}^\infty
\frac{(\mathrm{i}-k)\,\mathrm{e}^{\mathrm{i}\,k\,x}+(\mathrm{i}+k)\,\mathrm{e}^{-\mathrm{i}\,k\,x}}{
-(1+k^2)}\notag\\
&=-\mathrm{i}-\mathrm{i}\,\sum_{k=1}^\infty\frac{2\,\cos(k\,x)}{(1+k^2)}
+\sum_{k=1}^\infty\frac{2k\,\sin(k\,x)}{(1+k^2)}\;,
\end{align}
so
\begin{subequations}
\begin{align}
\re p(x)&=\sum_{k=1}^\infty\frac{2k\,\sin(k\,x)}{(1+k^2)}\;,\\
\im p(x)&=-1-\sum_{k=1}^\infty\frac{2\,\cos(k\,x)}{(1+k^2)}\;.
\end{align}
\end{subequations}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[declare function={imp(\k,\x)=-2*cos(\k*\x)/(1+\k*\k);
\begin{axis}[xlabel=$x$, ylabel={},
domain=1:40,
samples=51,
no markers,
smooth,
]
\legend{$\re p(x)$,$\im p(x)$} 