# [Tex/LaTex] Can PGF plot the integral of any specified function

pgfplots

How can I use the pgf maths engine to plot the integral of any function?

For example \addplot {int(cos(x))} does not work.

As was said in the comments, you PGF can't compute the antiderivative analytically. If the function is reasonably linear, you can quite easily compute the antiderivative numerically, similar to the approach in Get derivative of a function.

Here's an approach using PGFPlotstable to calculate the function values:

\documentclass[border=5mm]{article}
\usepackage{pgfplots, pgfplotstable}

\pgfplotstablenew[
create on use/x/.style={
create col/expr={\pgfplotstablerow/50}
},
create on use/y/.style={
create col/expr={cos(deg(\thisrow{x}))}
},
create on use/int/.style={
create col/expr={\pgfmathaccuma+(\thisrow{y}+\prevrow{y})/2*(\thisrow{x}-\prevrow{x})}
},
columns={x,y,int}
]
{200}
\datatableA

\begin{document}
\begin{tikzpicture}[trim axis left]
\begin{axis}[no markers, legend style={at={(0.5,-0.20)}, anchor=north}, legend entries={Original function, Analytical antiderivative, Numerical antiderivative}]
\addplot [line width=3pt, red!50, domain=0:4] {sin(deg(x))};
\end{axis}
\end{tikzpicture}\\[3ex]

\pgfplotstablenew[
create on use/x/.style={
create col/expr={\pgfplotstablerow/50-2}
},
create on use/y/.style={
create col/expr={\thisrow{x}^3}
},
create on use/int/.style={
create col/expr={\pgfmathaccuma+(\thisrow{y}+\prevrow{y})/2*(\thisrow{x}-\prevrow{x})}
},
columns={x,y,int}
]
{200}
\datatableB

\begin{tikzpicture}[trim axis left]
\begin{axis}[no markers, samples=500]