0

I am curious if it is possible to evaluate an integral using the xfp package using tex language?

\fpeval{\int_{-1}^{1}\frac{1}{\sqrt{1 - x^2}}dx}}

Just throwing out the idea. I know I can easily do this by python/Matlab/mathematica/calculator/hand but I love LaTeX.

3
  • 1
    No, xfp doesn't do that. You have to solve the integral analytically yourself, then you can feed xfp the remaining arithmetics for it to give you the final value Commented Apr 15, 2021 at 20:11
  • 1
    fp eval just evaluates a single floating point expressions but you also have loops and conditionals so you could implement a numerical approximation to the interval Commented Apr 15, 2021 at 20:11
  • Awesome! Thanks for the quick answers. Again, this is all hypothetical speaking. Commented Apr 15, 2021 at 20:14

2 Answers 2

1

Modulo coding errors this is a 6 step simpsons rule numeric approximation (not coded very efficiently as the end points of each step are evaluated twice)

l3fp could get a more accurate result than this if coded more carefully, but this is enough for this morning:-)

\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}

producing

2.888405590181524

Which isn't quite the same as this:

enter image description here

0

Here is a solution based on the pst-ode package. The integrand is symmetrical about x=0, so integration can be started at 0 and the result is multiplied by 2.

enter image description here

Typeset with 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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .