# ForwardDiff over closed interval

**URL:** <https://discourse.julialang.org/t/forwarddiff-over-closed-interval/129629>\
**Category:** Optimization (Mathematical)\
**Tags:** forwarddiff\
**Created:** [June 4, 2025, 7:38am UTC](https://discourse.julialang.org/t/forwarddiff-over-closed-interval/129629 "2025-06-04T07:38:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [June 4, 2025, 7:38am UTC](https://discourse.julialang.org/t/forwarddiff-over-closed-interval/129629/1 "2025-06-04T07:38:56Z")

</div>

I need to optimize a function `f(u)` defined over a closed domain u =[0,1]. I was happily using Newton’s method and FowardDiff for this until I tried upgrading to v1.0. Now, ForwardDiff intentionally gives the derivative on the right of u. This means that while `f(u=1.)` is defined, `f(u=Dual(1.,1.))=Dual(NaN,NaN)`.

I see this change marked at the top of the README, but I don’t see any suggestions for how to get the derivative on the left of u or any other “designed” approach to deal with closed domains like this.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 4, 2025, 8:02am UTC](https://discourse.julialang.org/t/forwarddiff-over-closed-interval/129629/2 "2025-06-04T08:02:22Z")

</div>

> [@weymouth](#):
>
> I see this change marked at the top of the README, but I don’t see any suggestions for how to get the derivative on the left of u or any other “designed” approach to deal with closed domains like this.

```julia
function LeftDeriv(f, x)
    ForwardDiff.derivative(t -> -f(-t), -x)
end

```

---

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [June 4, 2025, 8:15am UTC](https://discourse.julialang.org/t/forwarddiff-over-closed-interval/129629/3 "2025-06-04T08:15:27Z")

</div>

I just want to mention this as it was much quicker in my use-case: have you tried avoiding autodiff and just using a designated algorithm for a bounded interval, as described in the section “Minimizing a univariate function over a bounded interval” here: [Minimizing a function · Optim](https://julianlsolvers.github.io/Optim.jl/stable/user/minimization/) ? I know this doesn’t address the OP, but might be useful nonetheless

---

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [June 6, 2025, 1:31pm UTC](https://discourse.julialang.org/t/forwarddiff-over-closed-interval/129629/4 "2025-06-06T13:31:20Z")

</div>

This certainly gives the left derivative. However, you will need logic to use right derivatives on the left boundary and left derivatives on the right.

I ended up allowing u to overshoot the interval and then clamp the results later. Clearly, this will not work for functions which are undefined (or without defined derivatives) outside the interval.

---

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [June 6, 2025, 1:32pm UTC](https://discourse.julialang.org/t/forwarddiff-over-closed-interval/129629/5 "2025-06-06T13:32:43Z")

</div>

My use case is pretty specific, so I’m not using Optim.
