# QuadGK returns NaNs

**URL:** https://discourse.julialang.org/t/quadgk-returns-nans/105308
**Category:** Numerics
**Tags:** question, quadgk, integral
**Created:** [October 23, 2023, 10:11am UTC](https://discourse.julialang.org/t/quadgk-returns-nans/105308 "2023-10-23T10:11:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![aner-sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aner-sanchez/32/202677_2.png) [@aner-sanchez](https://discourse.julialang.org/u/aner-sanchez)
#### Post date: [October 23, 2023, 10:11am UTC](https://discourse.julialang.org/t/quadgk-returns-nans/105308/1 "2023-10-23T10:11:30Z")

</div>

Hello everyone 🙂

Would someone know why

```julia
using QuadGK
n = 538
_, w, _ = kronrod(n,1e-11,40); @show w

```

returns a vector of NaN weights at n=538 and not before?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 23, 2023, 5:58pm UTC](https://discourse.julialang.org/t/quadgk-returns-nans/105308/2 "2023-10-23T17:58:05Z")

</div>

> [@aner-sanchez](#):
>
> Would someone know why

This is a bug: [spurious underflow in kronrod for large n · Issue #93 · JuliaMath/QuadGK.jl · GitHub](https://github.com/JuliaMath/QuadGK.jl/issues/93)

I should have a fix pushed shortly.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 23, 2023, 6:10pm UTC](https://discourse.julialang.org/t/quadgk-returns-nans/105308/3 "2023-10-23T18:10:02Z")

</div>

That being said, you will run into other floating-point problems for large n, unless you go to higher precision; Laurie’s algorithm for the Gauss–Kronrod weights is not really designed to scale to n more than a few hundred, I think.

---

<div class="post-metadata">

### Author: ![lxvm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lxvm/32/50010_2.png) [@lxvm](https://discourse.julialang.org/u/lxvm)
#### Post date: [October 24, 2023, 12:54pm UTC](https://discourse.julialang.org/t/quadgk-returns-nans/105308/4 "2023-10-24T12:54:31Z")

</div>

Adding link to associated discussion on StackOverflow: [integration - Optimal quadrature rule for heavy tail measure - Computational Science Stack Exchange](https://scicomp.stackexchange.com/questions/43374/optimal-quadrature-rule-for-heavy-tail-measure)

Summary: splitting the ray with an endpoint singularity into an interval with endpoint singularity and a ray may lead to more efficient quadratures

---

<div class="post-metadata">

### Author: ![lxvm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lxvm/32/50010_2.png) [@lxvm](https://discourse.julialang.org/u/lxvm)
#### Post date: [October 24, 2023, 1:24pm UTC](https://discourse.julialang.org/t/quadgk-returns-nans/105308/5 "2023-10-24T13:24:46Z")

</div>

Since the measure of interest is a power law, using Gauss quadrature with a Jacobi weight function should perform well for an interval with the endpoint singularity. Computing those quadrature rules is a feature built into [QuadGK](https://juliamath.github.io/QuadGK.jl/stable/weighted-gauss/), and I wrote a wrapper specifically for Jacobi weights called [QuadJGK](https://github.com/lxvm/QuadJGK.jl). It can be used to compute a 10-point Gauss rule for polynomials orthogonal w.r.t the weight `(1-x)^0.0 * (1+x)^-0.8` for the interval `[-1, 1]` like this

```julia
using QuadJGK
x, w = QuadJGK.jacobigauss(JacobiSpace(0.0, -0.8), 10)

```

That package is still a work in progress, and it does allow h-adaptive quadrature when Kronrod rules exist for the Jacobi weight, but they do not always exist, according to [this paper](https://www.ams.org/mcom/1988-51-183/S0025-5718-1988-0942152-3/).
