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
(1/x)(1/x-1)/2. Forx=2, we have(1/2)(1/2-1)/2 = -0.125, which gives a rounded value of-0.13, as printed in the plot. Since1/x-1is negative forx>1, the function is negative, too. Mathematically, the formula is only valid for sums of consecutive integers. – gernot Feb 04 '21 at 09:48partial sums– Black Mild Nov 02 '21 at 06:46