# Finding the minimizer region of a function

**URL:** <https://discourse.julialang.org/t/finding-the-minimizer-region-of-a-function/103819>\
**Category:** Numerics\
**Tags:** optimization\
**Created:** [September 13, 2023, 4:31pm UTC](https://discourse.julialang.org/t/finding-the-minimizer-region-of-a-function/103819 "2023-09-13T16:31:55Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [September 15, 2023, 8:10am UTC](https://discourse.julialang.org/t/finding-the-minimizer-region-of-a-function/103819/9 "2023-09-15T08:10:16Z")

</div>

Consider one-dimensional “slices” across the function (using the above MWE as a starting point), say along `x1 = 0`:

```julia
Q(x2) = t -> R([t, x2])
S(x1) = t -> R([x1, t])

xc = 0.0

F = S(xc)
p1 = plot(scatter(; x=xs, y=F.(xs), name="Function at $(xc)"));
p1

G = t -> ForwardDiff.derivative(F, t)
p2 = plot(scatter(; x=xs, y=G.(xs), name="Gradient at $(xc)"));

H = t -> ForwardDiff.derivative(G, t)
p3 = plot(scatter(; x=xs, y=H.(xs), name="2nd deriv. at $(xc)"));

[p1; p2; p3]

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/e/5e5e2567303ca5855bba65ea5df66a9f029d84e9.png)

By convexity, the gradient is monotonic non-decreasing, where the “vacuole” is the “step” in the centre. If we know an interior point of the middle step, then tracking when the function changes indicates the boundary of those regions. Of course, the numerical behaviour means that tuning tolerances for distinguishing “zero” from “non-zero” becomes dependent on the function.

```julia
nonzero_tol = 0.1
E_L = Tuple[]
E_R = Tuple[] # {Float64,Float64}

x_scan = -2:0.05:1;
y_scan = -2:0.05:2;

for y1 in y_scan
    G = t -> ForwardDiff.derivative(S(y1), t)
    D = collect(zip(x_scan, G.(x_scan)))
    left_edge = D[findlast(x -> x[2] < -nonzero_tol, collect(D))]
    right_edge = D[findfirst(x -> x[2] > nonzero_tol, collect(D))]
    push!(E_L, (y1, left_edge...))
    push!(E_R, (y1, right_edge...))
end

traceE_L = scatter(x=getindex.(E_L,2), y=getindex.(E_L,1))
traceE_R = scatter(x=getindex.(E_R,2), y=getindex.(E_R,1))

fig = plot(trace1, Layout( yaxis=attr(scaleanchor="x"), showlegend=false))
add_trace!(fig, traceE_L)
add_trace!(fig, traceE_R)
display(fig)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/1/d1752f09546ca4e9c0c4c31e78953480d02415be.jpeg)

---

_[View the full topic](https://discourse.julialang.org/t/finding-the-minimizer-region-of-a-function/103819)._
