MATLAB: Improper integral

improper integralintegralsintegrationMATLABquadquadgkquadl

Hi!!I need help in the calculation of the integral of the function below
function out = fun(u,a,b)
out = exp(-a.* exp(2.*u)).*exp(-(u + b.^2).^2./2.*b.^2)/...
(sqrt(2.*pi.*b.^2));
end
a=0; b=1; *quad('fun',-Inf,Inf,[],[],a,b);
Is there any idea? where is the mistake?

Best Answer

I don't think quad ever allowed infinite limits. quadgk does. The answer you need depends on how old your MATLAB version is. I am going to express an answer in more recent syntax. If you try
a=0; b=1;
fun = @(u) exp(-a.* exp(2.*u)).*exp(-(u + b.^2).^2./2.*b.^2)/...
(sqrt(2.*pi.*b.^2));
quad(fun,-Inf,Inf)
then you get this message:
Warning: Infinite or Not-a-Number function value encountered.
and the answer is NaN. The likely reason is that there is underflow in these exponential expressions:
>> fun(1000)
ans =
NaN
However, if you plot this function, you can see that this function approaches zero very rapidly. For example,
>> fun(0)
ans =
0
Therefore, you can do this calculation:
quadl(fun,-100,100)
ans =
1.0000
This is 1 to an accuracy of about 10^-8.
Note: the fun = ... line is an anonymous function.