0

So I want to make an automatic partial sums plotter on tikz, where I just type in the series and it will plot the partial sum for me.

I'm basing my code off this but it makes things negative. I know how to get rid of the line by using "only marks", but I'm not sure why the function gets negative. (I'm also thinking of doing alternating series, like $-\frac{1}{n}$:

\documentclass[border=5pt]{standalone} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
        % Explicit formula
        \pgfmathdeclarefunction{explicit_sum}{1}{%
            \pgfmathparse{(#1*(#1-1))/2}%
        }
        \pgfplotstableset{
            % define how the 'x' column shall be filled
            % (in this case just with integers counting from 1)
            create on use/x/.style={
                create col/set list={1,...,10}
            },
            % -----
            % now you can either create here a column with your function ...
            create on use/fx/.style={
                create col/expr={(\thisrow{x})}
            },
            % ... and then accumulate the values here ...
            create on use/sum/.style={
                create col/expr accum={
                    \pgfmathaccuma + \thisrow{fx}
                }{0},       % <-- start with `0'
            },
        }
        % here you create a new table using the columns you need and
        % with the first mandatory argument you specify the number of elements
        % the table should have
        \pgfplotstablenew[
            columns={x,fx,sum},
        ]{10}{\loadedtable} \begin{document}
        \begin{tikzpicture}
            \begin{axis}[
                % added for debugging purposes or here to quicker check,
                % it one gets the desired output
                nodes near coords,
            ]
            % Expected
            \addplot+[samples at={1,...,10}] {explicit_sum(1/x))};
        \end{axis}
    \end{tikzpicture} 

\end{document}

I based my code off this: Plotting the sum of a function in TikZ

Stefan Pinnow
  • 29,535
Sat
  • 305
  • 1
    There is nothing wrong with the code: It plots the function it is told to. The function is (1/x)(1/x-1)/2. For x=2, we have (1/2)(1/2-1)/2 = -0.125, which gives a rounded value of -0.13, as printed in the plot. Since 1/x-1 is negative for x>1, the function is negative, too. Mathematically, the formula is only valid for sums of consecutive integers. – gernot Feb 04 '21 at 09:48
  • This question may or may not be related: https://tex.stackexchange.com/questions/429508/sum-inside-addplot-call – Steven B. Segletes Feb 04 '21 at 10:42
  • @ gernot, I see. Yes, it seems that I have to adapt the formulas independently for this to work. I thought I could just write the function at the bottom and then the tikz would do it all for me, but now I see i have to do it independently each time. – Sat Feb 04 '21 at 11:29
  • @Sat, since it seems to be a "mathematical" problem instead of a "LaTeX/PGFPlots" one, could you consider deleting the question? – Stefan Pinnow Nov 02 '21 at 05:51
  • in case you are caring about drawing with any tools, note that Asymptote has built-in function for partial sums – Black Mild Nov 02 '21 at 06:46

0 Answers0