# Can PGF plot the integral of any specified function?

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

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

• Wow, TeX doing integrals... That is ambitious. Though, if you mean numerical, not symbolic (as your question seems to suggest), it is probably pretty doable. (Theoretically, symbolic is of course doable as well - but not worth the effort imho.) Mar 21, 2013 at 7:32
• I don't think the pgf math engine was designed for this. You could use an external program to carry out computations like this.
– user10274
Mar 21, 2013 at 7:36
• Or luatex, though it might be reinventing the wheel... Mar 21, 2013 at 7:44
• Numerical integration (e.g. trapezoid or Simpson's rule) can be done easily enough with a for loop starting by \pgfplotsforeachungrouped. Symbolic integration, though, is a different problem altogether. Mar 21, 2013 at 7:48
• I decided the simplest would be to calculate the symbolic integration on wolframalpha.com Thanks all. Mar 29, 2013 at 13:01

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]
\addplot [line width=3pt, red!50, domain=-2:2] {1/4*x^4};
\addplot [black] table [y expr=\thisrow{int}-4] {\datatableB};
\end{axis}
\end{tikzpicture}

\end{document}

• Very nice. Would it be possible to hide it all inside a style, say \addplot[int] {cos(deg(x))}; (that is automatic definition of the function to be used in the table and \addplot table behind the scene)? Mar 21, 2013 at 8:50
• @cjorssen: That should be possible, but it would take a bit of work...
– Jake
Mar 21, 2013 at 10:20
• Then I guess I should start a bounty to reward that bit of work :) Mar 21, 2013 at 12:13
• @cjorssen: That seems like an abuse of the plot style. IMO the style of a plot shouldn't change the function. But you could hack into pgfmath so that it could parse something like int(cos(deg(x)),x,0,1). Mar 21, 2013 at 12:28
• @MatthewLeingang Here is a feature request for this: sourceforge.net/p/pgf/feature-requests/99 Jun 21, 2016 at 11:27

PSTricks can do it. Here is an example for the default Simpson method (Integral of sin(x)+cos(x):

\documentclass[pstricks,border=15pt]{standalone}
\usepackage{pst-func}

\begin{document}

\begin{pspicture}[linewidth=1pt](-1,-1.5)(7,2.5)
\psaxes{->}(0,0)(-1,-1.2)(6.75,2.5)
\psplot[linecolor=red,algebraic]{0}{6.5}{cos(x)+sin(x)}
\psCumIntegral[plotpoints=500,Simpson=10,

Run it with xelatex. pst-func also knows \psIntegral, see documentation.