# Gradient not zero when optimizing function

**URL:** https://discourse.julialang.org/t/gradient-not-zero-when-optimizing-function/66972
**Category:** Optimization (Mathematical)
**Tags:** optimization
**Created:** [August 25, 2021, 11:34am UTC](https://discourse.julialang.org/t/gradient-not-zero-when-optimizing-function/66972 "2021-08-25T11:34:47Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [August 25, 2021, 11:34am UTC](https://discourse.julialang.org/t/gradient-not-zero-when-optimizing-function/66972/1 "2021-08-25T11:34:47Z")

</div>

I am optimizing a function. The algorithm finds a solution, but the gradient at the solution is not close to zero. What can be done in that case?

```julia
sol3 = optimize(θ -> -obj(θ), sol1.minimizer, NewtonTrustRegion(),
        Optim.Options(extended_trace=true, store_trace=true, show_trace=true, iterations=10_000, g_tol=1e-6);
        autodiff=:finite)

julia> sol3
 * Status: success

 * Candidate solution
    Final objective value: 2.505956e+05

 * Found with
    Algorithm: Newton's Method (Trust Region)

 * Convergence measures
    |x - x'| = 0.00e+00 ≤ 0.0e+00
    |x - x'|/|x'| = 0.00e+00 ≤ 0.0e+00
    |f(x) - f(x')| = 0.00e+00 ≤ 0.0e+00
    |f(x) - f(x')|/|f(x')| = 0.00e+00 ≤ 0.0e+00
    |g(x)| = 1.70e+07 ≰ 1.0e-06

 * Work counters
    Seconds run: 4957 (vs limit Inf)
    Iterations: 42
    f(x) calls: 33
    ∇f(x) calls: 35
    ∇²f(x) calls: 3

julia> FiniteDiff.finite_difference_gradient(obj, sol3.minimizer) |> norm
2.1403981052614696e7

```
