Testing improper integrals with Mathematica
Testing improper integrals with Mathematica
Intro
Intro
Studying space curves, I often need
to estimate convergence/divergence of infinite integrals, without necessarily obtaining an exact value.
On the one hand, output of Mathematica meets mathematical standards;
e.g. both these examples considered divergence:
to estimate convergence/divergence of infinite integrals, without necessarily obtaining an exact value.
On the one hand, output of Mathematica meets mathematical standards;
e.g. both these examples considered divergence:
In[]:=
Integrate[1/Log[x+2],{x,0,∞}]
1
Log[2+x]
Out[]=
∞
∫
0
1
Log[2+x]
In[]:=
Integrate[Cos@x,{x,0,∞}]
Out[]=
∞
∫
0
But often this is not enough! The behavior of curves depends on finite and infinite limits of integrals.
In these examples we can get more satisfying results:
In these examples we can get more satisfying results:
In[]:=
Limit[Integrate[1/Log[x+2],x],x->∞]
Out[]=
∞
In[]:=
Limit[Integrate[Cos@x,x],x->∞]
Out[]=
Indeterminate
I decided to write a template for a module that analyzes in different ways integrals with infinite limits,
and tries to find the most useful result.
and tries to find the most useful result.
Important notes
Important notes
Integrand assumed to be smooth and continuous, at least for x >
Functions run in time constrained mode with user defined
x
min
Functions run in time constrained mode with user defined
t
max
Possible built-in's and it's outputs
Possible built-in's and it's outputs
General possible otputs are Timeout and finite numerical value.
More specific cases:
More specific cases:
◼
Integrate[f@x,{x,xMin,∞}]:
Message Integrate::idiv
Expression itself (Head === Integrate)
Message Integrate::idiv
Expression itself (Head === Integrate)
◼
Limit[Integrate[f@x,x],x∞] and
Limit[AsymptoticIntegrate[f@x,x,x∞], x∞]
For these functions, if result is a finite number it is not value of integral,
but rather test that it's convergence.
Infinity
Signed Infinity
Indeterminate
Interval
Expression (Head === Limit)
Limit[AsymptoticIntegrate[f@x,x,x∞], x∞]
For these functions, if result is a finite number it is not value of integral,
but rather test that it's convergence.
Infinity
Signed Infinity
Indeterminate
Interval
Expression (Head === Limit)
◼
NIntegrate[f@x,{x,xMin,∞}]
Always finite num with possible messages:
::slwcon
::ncvb
etc.
Always finite num with possible messages:
::slwcon
::ncvb
etc.
Module
Module
Input:
f - integrand as pure function
tMax - maximal time to wait response
xMin - lower bound of integral
rMax - MaxRecursion value for NIntegrate
f - integrand as pure function
tMax - maximal time to wait response
xMin - lower bound of integral
rMax - MaxRecursion value for NIntegrate
In[]:=
ClearAll[infinityIntegration];infinityIntegration[f_,tMax_,xMin_,rMax_]:=Module[{res,extractMsgTag=First@Select[StringSplit@#,StringContainsQ["::"]]&,(*Messages*)msgTmOut=StringTemplate["`1` method time out"],msgUnDef=StringTemplate["Result of `1` method is undefined"],msgResDef=StringTemplate["Result of `1` method is `2`"],msgResDefErr=StringTemplate["Result of `1` method is `2` with error `3`"],msgResDefErrWrn=StringTemplate["Result of method `1` is `2` with error `3` with warnings `4`"]},(*TryIntegrate*)res=Quiet@TimeConstrained[Integrate[f@x,{x,xMin,∞}],tMax,"TimeOut"];Which[Head@res===Integrate,Print@msgUnDef["Integrate"],NumericQ@res,Print@msgResDef["Integrate",EngineeringForm@res],res==="TimeOut",Print@msgTmOut["Integrate"]];(*TryLimit@Integrate*)res=Quiet@TimeConstrained[Limit[Integrate[f@x,x],x->∞],tMax,"TimeOut"];Which[Head@res===Limit,Print[msgUnDef["LimitIntegrate"]],NumericQ@res,Print@msgResDef["LimitIntegrate","integral convergence"],Abs@res===Infinity,Print@msgResDef["LimitIntegrate","integral divergence"],res===Indeterminate||Head@res===Interval,Print@msgResDef["LimitIntegrate","integral has no value"],res==="TimeOut",Print[msgTmOut["LimitIntegrate"]]];(*TryAsymptoticIntegrate*)res=Quiet@TimeConstrained[Limit[AsymptoticIntegrate[f@x,x,x->∞],x->∞],tMax,"TimeOut"];Which[Head@res===Limit,Print[msgUnDef["AsymptoticIntegrate"]],NumericQ@res,Print@msgResDef["AsymptoticIntegrate","integral convergence"],Abs@res===Infinity,Print@msgResDef["AsymptoticIntegrate","integral divergence"],res===Indeterminate||Head@res===Interval,Print@msgResDef["AsymptoticIntegrate","integral has no value"],res==="TimeOut",Print[msgTmOut["AsymptoticIntegrate"]]];(*TryNIntegrate*)Block[{tags,errs,$Messages={}},res=EvaluationData@NIntegrate[f@x,{x,xMin,∞},AccuracyGoal->6,PrecisionGoal->6,MaxRecursion->rMax,IntegrationMonitor:>((errs=Through[#1@"Error"])&)];tags=extractMsgTag/@res["MessagesText"];If[tags==={},Print[msgResDefErr["NIntegrate",EngineeringForm@res["Result"],EngineeringForm@Total@errs]],Print[msgResDefErrWrn["NIntegrate",EngineeringForm@res["Result"],EngineeringForm@Total@errs,tags]]]];];
Examples
Examples
In[]:=
infinityIntegration[&,5,1,20]
-2
#
Result of Integrate method is 1
Result of LimitIntegrate method is integral convergence
Result of AsymptoticIntegrate method is integral convergence
Result of NIntegrate method is 1. with error 159.595*10^-18
In[]:=
infinityIntegration+Log[1+]&,10,1,30
Log@ArcTan@#
2
#
2
#
Result of Integrate method is undefined
Result of LimitIntegrate method is integral convergence
Result of AsymptoticIntegrate method is integral convergence
Result of NIntegrate method is 102.576*10^-3 with error 193.209*10^-9
In[]:=
infinityIntegration(+1)^-1Log[#^2+2]&,5,1,20
3
#
Integrate method time out
LimitIntegrate method time out
Result of AsymptoticIntegrate method is integral convergence
Result of NIntegrate method is 239.818*10^-3 with error 273.278*10^-9
In[]:=
infinityIntegration[(Sin[1-Exp[-#]]-Sin@1)&,10,0,30]
Result of Integrate method is -712.965*10^-3
Result of LimitIntegrate method is integral convergence
Result of AsymptoticIntegrate method is integral convergence
Result of NIntegrate method is -712.965*10^-3 with error 164.857*10^-9
In[]:=
infinityIntegration[Cos[#]Log[1+#^2]&,20,1,30]
Result of Integrate method is undefined
◼
