
我很好奇是否可以xfp
使用 tex 语言包来计算积分?
\fpeval{\int_{-1}^{1}\frac{1}{\sqrt{1 - x^2}}dx}}
只是抛出这个想法。我知道我可以通过 python/Matlab/mathematica/calculator/hand 轻松完成此操作,但我喜欢 LaTeX。
答案1
模数编码错误这是一个 6 步辛普森规则数值近似(由于每个步骤的终点都要评估两次,因此编码效率不高)
如果编码更仔细的话,l3fp 可以得到比这更准确的结果,但这对于今天早上来说已经足够了:-)
\documentclass{article}
\usepackage{xfp}
\def\fx#1{(1/(sqrt(1 - (#1)^2)))}
\def\simp#1#2#3{((((#2)-(#1))/6)*(#3{#1}+ 4*#3{(#1+(#2))/2} + #3{#2}))}
\begin{document}
\typeout{\fpeval{
2*(
\simp{-0.99}{-0.9}{\fx}
+\simp{-0.9}{-0.8}{\fx}
+\simp{-0.8}{-0.6}{\fx}
+\simp{-0.6}{-0.4}{\fx}
+\simp{-0.4}{-0.2}{\fx}
+\simp{-0.2}{0}{\fx}
)
}}
\end{document}
生产
2.888405590181524
这与这:
答案2
这是基于该pst-ode
软件包的解决方案。被积函数关于对称X=0,因此积分可以从0开始,然后将结果乘以2。
排版如下pdflatex --shell-escape
:
\documentclass{article}
\pagestyle{empty}
%%%%%%%%%%%%%%%%%%%%%%% solve ODE in auxiliary document %%%%%%%%%%%%%%%%%%%%%%%%
\begin{filecontents}[overwrite]{solve.tex}
\documentclass{article}
\usepackage{pst-ode}
\begin{document}
% arguments:
% algebraicAll --> all arguments in algebraic notation
% saveData --> also write result into file `result.dat'
% `result' --> PostScript variable that takes result
% 2*y[0] --> output format in `result' and `result.dat'
% 0, 1 --> integration interval t_0, t_e
% 2 --> number of saved output points t_0, t_e
% 0 --> initial value
% 1/sqrt(...) --> right-hand side of ODE
\pstODEsolve[algebraicAll,saveData]{result}{2*y[0]}{0}{0.9999999}{2}{0}{
1 / sqrt(1-t^2)
}
dummy text
\end{document}
\end{filecontents}
\immediate\write18{latex solve}
\immediate\write18{dvips solve}
\immediate\write18{ps2pdf -dNOSAFER solve.ps}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newread\resfile\immediate\openin\resfile=result.dat
\immediate\read\resfile to \dummy % read and throw away initial value
\immediate\read\resfile to \result % read definite integral value
\begin{document}
\[
\int_{-1}^{1} \frac{1}{\sqrt{1 - x^2}} dx \approx \result
\]
\end{document}