Medulla Oblongata
Medulla Oblongata

Reputation: 3961

Discontinuity in Matlab integral

I'm trying to numerically integrate a discontinuous function in Matlab.

fun = @(z) 1./((z+1).^2+4) * 1./(exp(-z)-1);
q = integral(fun,-Inf,Inf,'Waypoints',[0])

There is a discontinuity at z=0, but I'm not sure how to use the Waypoints option to specify this. I get an error message:

Warning: Reached the limit on the maximum number of intervals in use. Approximate bound on error is 7.4e-01. The integral may not exist, or it may be difficult to approximate numerically to the requested accuracy.

How can I calculate this integral accurately?

Upvotes: 0

Views: 1056

Answers (2)

rst
rst

Reputation: 2714

Adding a remark to @Ander Biguri's correct answer.

There are other functions with singularities, e.g. 1/((x+1)*sqrt(x)) (look at improper integrals)

integral(@(x) 1./((x+1).*sqrt(x)), 0, inf)

ans =

    3.1416

and even function that converge with singularities not at the border of the integrating range

integral(@(x) 1./(x.^2).^(1/3), -1, 1)

ans =

    6.0000

So MATLAB does everything right. Your function does not converge.

Maybe you are interested in Cauchy's principal value, but this is another topic.

Upvotes: 0

Ander Biguri
Ander Biguri

Reputation: 35525

From the docs "Use waypoints to indicate any points in the integration interval that you would like the integrator to use."

Probably the only point in that equation you don't want to use us zero.... as the function is undefined at that value (its limits from the left and rigth are different, thus its not infinite, its undefined).

Wolfram Alpha claims that the integral does not exists.

So, when MATLAB says

"The integral may not exist, or it may be difficult to approximate numerically to the requested accuracy"

It is.... because it may not exist!


I guess you could always do something like:

q = integral(fun,-Inf,-0.001)+integral(fun,0.001,Inf);

but I am not sure how correct this is....

Upvotes: 2

Related Questions