Quantcast
Channel: Plotting the sum of a function in TikZ - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by Stefan Pinnow for Plotting the sum of a function in TikZ

$
0
0

If it is allowed to produce an "invisible" table to define the x values and then make the calculations in another column, then you could do the following.

\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,...,100}        },        % -----        % now you can either create here a column with your function ...        create on use/fx/.style={            create col/expr={(\thisrow{x})^2}        },        % ... and then accumulate the values here ...        create on use/sum/.style={            create col/expr accum={                \pgfmathaccuma + \thisrow{fx}            }{0},       % <-- start with `0'        },        % -----        % ... or you accumulate directly with the function        create on use/sum2/.style={            create col/expr accum={                \pgfmathaccuma + (\thisrow{x})^2            }{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    % (so either `sum2' is redundant or (`fx' and `sum') are redundant)    \pgfplotstablenew[        columns={x,fx,sum,sum2},    ]{10}{\loadedtable}\begin{document}%    % maybe it is useful to typeset the table for debugging purposes%    \pgfplotstabletypeset[%        columns={x,fx,sum,sum2},%    ]{\loadedtable}    \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(x)};        % when the table is created, you can use the columns here        \addplot table [x=x,y=sum] {\loadedtable};        \end{axis}    \end{tikzpicture}\end{document}

image showing the result of above code


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>