integration of a piecewise function

3 visualizaciones (últimos 30 días)
Richard
Richard el 11 de Sept. de 2017
Respondida: Sarah Mohamed el 14 de Sept. de 2017
Can anyone help me understand why
integral(@(mu) (1.0+0.0*mu).*(mu>0)+(1-exp(1.0./mu)).*(mu<0),0,1)
gives a "NaN", while
integral(@(mu) (1.0+0.0*mu).*(mu>0)+(1-(1.0./mu)).*(mu<0),0,1)
gives the correct answer "1.0"?
It's bugging me for so long.
Background, I started with a two variate function
psi_MMS =@(x,mu) (1.0+0.0*x+0.0*mu).*(mu>0) + (1-exp((Tau-x)./(mu+1e-16))).*(mu<0);
And I wanted to integrate it over mu to get a function of x:
phi0_MMS =@(x) integral(@(mu) psi_MMS(x,mu), -1,1);
Thanks!

Respuestas (1)

Sarah Mohamed
Sarah Mohamed el 14 de Sept. de 2017
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

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by