It looks like NaN values are generated in the first function when you try to multiply -INF and logical 0’s.
For ease of demonstration, let me go ahead and create a handle for the first function as follows:
f1 = @(mu) (1.0+0.0*mu).*(mu>0)+(1-exp(1.0./mu)).*(mu<0);
Let me also go ahead and create a range of values for “mu”, 0.1, 0.01, 0.001, etc, that are close to 0:
mu = 10.^(-1*[1:10]);
If I plug these values into f1, I get the following:
>> f1(mu)
ans =
1 1 NaN NaN NaN NaN NaN NaN NaN NaN
Let me try to localize the problem slightly more:
>> (1-exp(1.0./mu))
ans =
1.0e+43 *
-0.0000 -2.6881 -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf
At mu = 0.001 and onwards, we find that "exp(1/mu)" evaluates to INF.
Let me now add in the piecewise part of the expression. “mu<0” should give a vector of logical 0’s:
>> (1-exp(1.0./mu)).*(mu<0)
ans =
0 0 NaN NaN NaN NaN NaN NaN NaN NaN
In summary, multiplying INF or -INF by a logical 0 (or just zero, for that matter) evaluates to NaN. As a quick check, we can confirm this quickly via:
>> inf*logical(0)
ans =
NaN
And as a final sanity check, let's try the original integration, but setting the lower limit to 0.01:
>> integral(f1, 0.01, 1)
ans =
0.9900