13

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

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

  • 5
    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.) – mbork Mar 21 '13 at 7:32
  • 2
    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 '13 at 7:36
  • Or luatex, though it might be reinventing the wheel... – mbork Mar 21 '13 at 7:44
  • 2
    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. – jubobs Mar 21 '13 at 7:48
  • I decided the simplest would be to calculate the symbolic integration on wolframalpha.com Thanks all. – skvery Mar 29 '13 at 13:01
13

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 [gray] table {\datatableA};
\addplot [line width=3pt, red!50, domain=0:4] {sin(deg(x))};
\addplot [black] table [y=int] {\datatableA};
\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 [gray] table {\datatableB};
\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)? – cjorssen Mar 21 '13 at 8:50
  • @cjorssen: That should be possible, but it would take a bit of work... – Jake Mar 21 '13 at 10:20
  • Then I guess I should start a bounty to reward that bit of work :) – cjorssen Mar 21 '13 at 12:13
  • 2
    @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). – Matthew Leingang Mar 21 '13 at 12:28
  • @MatthewLeingang Here is a feature request for this: sourceforge.net/p/pgf/feature-requests/99 – student Jun 21 '16 at 11:27
11

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,
   linecolor=blue]{0}{6.5}{ RadtoDeg dup cos exch sin add }
 \end{pspicture}

\end{document}

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.