如何使用pgf
数学引擎绘制任何函数的积分?
例如\addplot {int(cos(x))}
不起作用。
答案1
正如评论中所说,PGF 无法通过分析计算反导数。如果函数是相当线性的,则可以很容易地通过数值方式计算反导数,类似于 获得函数的导数。
以下是使用 PGFPlotstable 计算函数值的方法:
\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}
答案2
PSTricks 可以做到这一点。以下是默认 Simpson 方法(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}
用 运行它xelatex
。pst-func
还知道\psIntegral
,请参阅文档。