# QuadGK unexpected behavior on first degree polynomial

**URL:** https://discourse.julialang.org/t/quadgk-unexpected-behavior-on-first-degree-polynomial/113134
**Category:** Numerics
**Tags:** question, quadgk, integral
**Created:** [April 18, 2024, 9:38am UTC](https://discourse.julialang.org/t/quadgk-unexpected-behavior-on-first-degree-polynomial/113134 "2024-04-18T09:38:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)
#### Post date: [April 18, 2024, 9:38am UTC](https://discourse.julialang.org/t/quadgk-unexpected-behavior-on-first-degree-polynomial/113134/1 "2024-04-18T09:38:22Z")

</div>

I constructed a tiny example on which my (naive?) use of the quadgk() fails. The example is a first order polynomial in x and y on the triangular domain with vertices (0,0), (1,0) and (0,1). Below a working and failing MWE. What do I overlook? Thx.

```julia
fooworks(x,y) = -8.0*x-8.0*y+4.0 
gnuworks(y) = quadgk(x->fooworks(x,y),0,1-y)[1]
fooint = quadgk_count(y->gnuworks(y),0,1)

```

produces -0.66… as expected output.

```julia
# observe change in coefficient (-4 instead of -8) of the term with y 
foofails(x,y) = -8.0*x-4.0*y+4.0 
gnufails(y) = quadgk(x->foofails(x,y),0,1-y)[1]
fooint = quadgk(y->gnufails(y),0,1)

```

hangs in performing computations.

---

<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: [April 18, 2024, 12:54pm UTC](https://discourse.julialang.org/t/quadgk-unexpected-behavior-on-first-degree-polynomial/113134/2 "2024-04-18T12:54:17Z")

</div>

> [@Domenico\_Lahaye](#):
>
> hangs in performing computations.

You aren’t setting any tolerance in these integrals, so it is using the default _relative_ tolerance of \approx 10^{-8}. This is a problem here because the integral `fooint` is actually zero, so a _relative_ tolerance `|err| ≤ rtol * |int|` can never be satisfied (unless the error is exactly zero), so it will keep refining the integrals until it hits the limits of machine precision (which will take a long time in 2d).

Passing an absolute tolerance `atol=1e-8` to both `quadgk` calls causes the calculation to run very quickly.

(You might also look into IteratedIntegration.jl for performing nested `quadgk` calls more efficiently. Or HCubature.jl via a [change of variables](https://discourse.julialang.org/t/how-to-compute-the-integration-of-the-following-formula-by-hcubature/32269/2) to map to a rectangular domain. Or, if you are really just integrating low-degree polynomials and similar very smooth functions, use a tensor-product of fixed-order Gaussian quadrature rules, or SimplexQuad.jl, or similar.)

---

<div class="post-metadata">

### Author: ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)
#### Post date: [April 18, 2024, 2:25pm UTC](https://discourse.julialang.org/t/quadgk-unexpected-behavior-on-first-degree-polynomial/113134/3 "2024-04-18T14:25:34Z")

</div>

Sincere thanks for the valuable input. Much appreciated.
