Quadgk not working for potential convert from MATLAB to Julia?

In MATLAB, quadgk(@(s) (exp(-s.^2)-1)./s.^2,0,1) gives me 0.8615, consistent with the correct answer, 1 - 1/e -sqrt(pi) erf(1).

In Julia, quadgk(s-> exp(-s.^2-1.0)./s.^2,0.0,1.0) gives

ERROR: DomainError with 4.7733380679681323e-153:
integrand produced Inf in the interval (0.0, 9.546676135936265e-153)

which suggests that it’s getting very hung up about the behaviour near s=0, even though Gaussian quadrature isn’t meant to evaluate the integrand at s=0.

Of course, this isn’t the actual integral that I’m interested in, but it illustrates the point.

Am I missing something subtle here? I really want to try using Julia, but this is not a great start.


In general you might have to be careful about functions like that where you divide by zero. But in this case you just have a typo in your command…


O ffs. I’m such an idiot. Thanks.