所以我想在 tikz 上制作一个自动部分总和绘图仪,我只需输入系列,它就会为我绘制部分总和。
我的代码就是基于此,但它却让结果变成负数。我知道如何通过使用“仅标记”来消除该线,但我不确定函数为什么会变成负数。(我也在考虑做交替级数,例如$-\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}
我的代码基于此:在 TikZ 中绘制函数总和