Numerical Integration
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1982] Numerical Integration
- From: Marcelo de Almeida Bueno - pos <bueno at ifi.unicamp.br>
- Date: Mon, 4 Sep 1995 22:22:09 -0400
Hi,
I would like to receive some hints (and tricks) about numerical integration with
Mathematica. I don't know if the internal routines used by mathematica to resolve numerical
integrations are poor (mainly, in dealing with singularities), or if I am not being able to
use all Mathematica capabilities on this subject.
Before I state my problem, let me show one (of many) strange result I have
obtained::
In[1]:=
Integrate[Cos[x1-x2]^2 Sqrt[Sin[x1-x2]^2],{x1,0,Pi},{x2,0,Pi}]
Out[1]:=
0
In[2]:=
NIntegrate[Cos[x1-x2]^2 Sqrt[Sin[x1-x2]^2],{x1,0,Pi},{x2,0,Pi}]
Out[2]:=
2.09439
Take a look at Plot3D[Cos[x1-x2]^2 Sqrt[Sin[x1-x2]^2],{x1,0,Pi},{x2,0,Pi}],
and you will see that the result in Out[1] is absurd!
Another problem is the time to evaluate my integrals. I am using a DX4-100 Mhz with
40 M RAM (Mathematica 2.2.3 for Windows).
Consider the following function:
Out[3]:=
2 2 2
(Sin[x1] (Sqrt[2 R + w - 2 R Cos[x1 - x2]] -
2 x1 - x2 2 2
2 Sqrt[R Sin[-------] ])) / R
2
and the following action,
In[4]:=
R=1;
w=.001;
NIntegrate[%3,{x1,Pi/4,3 Pi/4},{x2,Pi/4,3 Pi/4},WorkingPrecision->17]
NIntegrate::slwcon:
Numerical integration converging too slowly; suspect one of the
following: singularity, oscillatory integrand, or insufficient
WorkingPrecision.
NIntegrate::slwcon:
Numerical integration converging too slowly; suspect one of the
following: singularity, oscillatory integrand, or insufficient
WorkingPrecision.
NIntegrate::slwcon:
Numerical integration converging too slowly; suspect one of the
following: singularity, oscillatory integrand, or insufficient
WorkingPrecision.
General::stop:
Further output of NIntegrate::slwcon
will be suppressed during this calculation.
NIntegrate::ncvb:
NIntegrate failed to converge to prescribed accuracy after 13
recursive bisections in x1 near {x1, x2} = {1.5707, 1.5707}.
Out[4]:=
0.00001076
After 34,684 s!!!
Ok! The answer now is correct, but with RIWIAD (CERN) on the same machine I
obtained the same answer in few minutes!!!
Another example. Consider the function bellow:
Out[5]:=
-(R (5 Sin[x1] - 3 Sin[x1 - 2 x2] - 3 Sin[2 x1 - x2] - 5 Sin[x2]))
------------------------------------------------------------------ +
2 x1 - x2 2
8 Sqrt[R Sin[-------] ]
2
2 2 2
(R + w - 3 R Cos[x1 - x2]) (Sin[x1] - Sin[x2])
------------------------------------------------- -
2 2 2
2 R Sqrt[2 R + w - 2 R Cos[x1 - x2]]
2 2 2
(R + w - 3 R Cos[x1 - x2]) (-Sin[x1] + Sin[x2])
-------------------------------------------------- +
2 2 2
2 R Sqrt[2 R + w - 2 R Cos[x1 - x2]]
R (-5 Sin[x1] + 3 Sin[x1 - 2 x2] + 3 Sin[2 x1 - x2] + 5 Sin[x2])
----------------------------------------------------------------
2 x1 - x2 2
8 Sqrt[R Sin[-------] ]
2
and,
In[6]:=
R=1;
w=.001;
NIntegrate[%5,{x1,Pi/4,3 Pi/4},{x2,3 Pi/4,3 Pi/2}]
1
Power::infy: Infinite expression -------------- encountered.
-90
Sqrt[0. 10 ]
Infinity::indet:
-57
-(0. 10 1 ComplexInfinity)
Indeterminate expression ----------------------------- encountered.
8
1
Power::infy: Infinite expression -------------- encountered.
-90
Sqrt[0. 10 ]
Infinity::indet:
-57
0. 10 1 ComplexInfinity
Indeterminate expression -------------------------- encountered.
8
1
Power::infy: Infinite expression -------------- encountered.
-90
Sqrt[0. 10 ]
General::stop:
Further output of Power::infy
will be suppressed during this calculation.
Infinity::indet:
-57
-(0. 10 1 ComplexInfinity)
Indeterminate expression ----------------------------- encountered.
8
General::stop:
Further output of Infinity::indet
will be suppressed during this calculation.
NIntegrate::rnderr:
Inexact arithmetic has caused {x1, x2} to take the value
{2.35619, 2.35619} where the integrand is singular.
Out[6]:=
Pi 3 Pi 3 Pi 3 Pi
NIntegrate[Out[5], {x1, --, ----}, {x2, ----, ----}]
4 4 4 2
And RIWIAD resolved it!
Thanks in advance,
_/_/ _/_/_/_/_/_/_/_/_/ | Department of Cosmic Rays and Chronology
_/ _/_/ _/ _/ _/ | Institute of Physics "Gleb Wataghin"
_/ _/ _/_/_/_/_/_/_/_/ | State University of Campinas, UNICAMP
_/ _/ _/ _/ | 13083-970 Campinas, Sao Paulo, Brazil
_/ _/ _/ _/ | e-mail: bueno at ifi.unicamp.br
_/ _/ _/_/_/_/ | Home Page: http://www.ifi.unicamp.br/~bueno
| Tel.: (+55) (192) 398112 or 300646
Marcelo de Almeida Bueno | FAX: (+55) (192) 393127 or 300646