# Numerical integration only with evaluated point

**URL:** <https://discourse.julialang.org/t/numerical-integration-only-with-evaluated-point/63552>\
**Category:** New to Julia\
**Tags:** integral\
**Created:** [June 25, 2021, 1:52pm UTC](https://discourse.julialang.org/t/numerical-integration-only-with-evaluated-point/63552 "2021-06-25T13:52:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 25, 2021, 1:52pm UTC](https://discourse.julialang.org/t/numerical-integration-only-with-evaluated-point/63552/1 "2021-06-25T13:52:00Z")

</div>

Hi,  
I’d like to integrate a function numerically.  
I found some packages, e.g., [QuadGK.jl](https://github.com/JuliaMath/QuadGK.jl), it seems only supports numerical integration with a given function.

In my case, suppose we cannot access the function at arbitrary points.  
That is, we can access only some given data points.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 25, 2021, 2:05pm UTC](https://discourse.julialang.org/t/numerical-integration-only-with-evaluated-point/63552/2 "2021-06-25T14:05:10Z")

</div>

Check this post out about [Romberg](https://discourse.julialang.org/t/romberg-jl-simple-romberg-integration-in-julia/39313) method or [NumericalIntegration.jl](https://github.com/dextorious/NumericalIntegration.jl).

---

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [June 25, 2021, 2:07pm UTC](https://discourse.julialang.org/t/numerical-integration-only-with-evaluated-point/63552/3 "2021-06-25T14:07:13Z")

</div>

You just need to define quadrature weights.  
For example, in FastGaussQuadrature.jl

```julia
using FastGaussQuadrature
p, w = gausslegendre(10)

```

Let’s say you have 10 values at these points, then do an integration

```julia
vals = randn(10)
integral = sum(vals .* w)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 25, 2021, 2:30pm UTC](https://discourse.julialang.org/t/numerical-integration-only-with-evaluated-point/63552/4 "2021-06-25T14:30:25Z")

</div>

> [@vavrines](#):
>
> `FastGaussQuadrature`

Suppose a [change of interval](https://en.wikipedia.org/wiki/Gaussian_quadrature#Change_of_interval) is required when using the Gauss-Legendre quadrature and that it will not handle arbitrary input points, as it might be the case in OP’s question?

---

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [June 25, 2021, 2:44pm UTC](https://discourse.julialang.org/t/numerical-integration-only-with-evaluated-point/63552/5 "2021-06-25T14:44:09Z")

</div>

You can always regularize your Gaussian weights according to the geometric transformation on points, right?  
I have not used the library you mentioned. Maybe it provides a straightforward solution though.

---

<div class="post-metadata">

**Author:** ![EP-Guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ep-guy/32/3442_2.png) [@EP-Guy](https://discourse.julialang.org/u/EP-Guy)\
**Post date:** [June 25, 2021, 3:49pm UTC](https://discourse.julialang.org/t/numerical-integration-only-with-evaluated-point/63552/6 "2021-06-25T15:49:40Z")

</div>

[Romerg](https://github.com/fgasdia/Romberg.jl), mentioned by @rafael.guerra above, is surprisingly accurate for integrating smooth functions but it requires the values being integrated be evenly sampled over the independent variable.

You can always fall back to [trapezoidal integration](https://github.com/francescoalemanno/Trapz.jl) if the points are not evenly sampled, but it’s low accuracy.

I"m not sure how the quadrature weighting method would compare in performance and accuracy. Need to try it out.

---

<div class="post-metadata">

**Author:** ![acwatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acwatt/32/35645_2.png) [@acwatt](https://discourse.julialang.org/u/acwatt)\
**Post date:** [March 13, 2023, 2:17am UTC](https://discourse.julialang.org/t/numerical-integration-only-with-evaluated-point/63552/7 "2023-03-13T02:17:33Z")

</div>

Thanks for the tip, @vavrines! One resource I found was slide 32 of [these](https://people.sc.fsu.edu/~jpeterson/numerical_quadrature) slides from Janet Peterson on numerical quadrature – they explain how to compute the transformed points and weights.

I have implemented these transformations to the quadrature points and weights from FastGaussQuadrature.jl.

````julia
using FastGaussQuadrature, LinearAlgebra

"""Transform the Gauss quadrature points x and weights w from [-1,1] to [a,b]. 

   Ref: https://people.sc.fsu.edu/~jpeterson/numerical_quadrature
"""
function transform_gauss_xw(x, w, a, b)
    x1 = a .+ (b-a)/2 * (x .+ 1)
    w1 = (b-a)/2 * w
    return (x=x1, w=w1)
end

"""Calculate the Gauss quadrature integral of f(x) from `a` to `b`, using `order` quadrature points.

    Example:
    ```
    f(x) = sin(x) * x^2
    LB = 0; UB = 10; n = 10;
    I = GL_integral(f, LB, UB, n)
    ```

    Ref: https://juliaapproximation.github.io/FastGaussQuadrature.jl/stable/gaussquadrature/
"""
function GL_integral(f, a, b, order)
    x, w = gausslegendre(order)
    T = transform_gauss_xw(x, w, a, b)
    x1 = T.x; w1 = T.w;
    I = dot(w1, f.(x1))
    return I
end

````
