# Optimizing using objective with an integral in it

**URL:** <https://discourse.julialang.org/t/optimizing-using-objective-with-an-integral-in-it/77162>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [February 27, 2022, 7:09pm UTC](https://discourse.julialang.org/t/optimizing-using-objective-with-an-integral-in-it/77162 "2022-02-27T19:09:09Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 27, 2022, 9:39pm UTC](https://discourse.julialang.org/t/optimizing-using-objective-with-an-integral-in-it/77162/4 "2022-02-27T21:39:54Z")

</div>

> [@samerb](#):
>
> I adjusted your code to replace the `y` 's in your second line with `foo` and `f` .

Ha. That was an obvious typo. I’ve edited the post.

The issue is that your user-defined function needs to be differentiable:

```julia
julia> using ForwardDiff, QuadGK

julia> f(y) = quadgk(x -> x, 0, y, rtol=1e-8)[1]
f (generic function with 1 method)

julia> ForwardDiff.derivative(f, 0.0)
ERROR: StackOverflowError:
Stacktrace:
[1] cachedrule( #unused#::Type{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Float64}, Float64, 1}}, n::Int64 ) (repeats 79984 times)
@ QuadGK ~/.julia/packages/QuadGK/ENhXl/src/gausskronrod.jl:253

```

Take a read of the posts here:

> [@Array issue with optimzing a function with quadgk](https://discourse.julialang.org/t/array-issue-with-optimzing-a-function-with-quadgk/41837):
>
> I’m trying to minimize a function that involves an integral, the integrand of which is one of the optimizing parameters. I can compute the function and get the gradient with Zygote, but I’m getting an odd conversion error when I try to run an optimization routine. Might anyone know what is going on? using Distributions using Random using ForwardDiff using Zygote using QuadGK using Optim theta\_lower = 1.0 theta\_upper = 5.0 uniform\_dist = Uniform(theta\_lower, theta\_upper) function linearCost(θ)…

---

_[View the full topic](https://discourse.julialang.org/t/optimizing-using-objective-with-an-integral-in-it/77162)._
