Hello,

I am trying to find the stationary point (gradient g(x) = 0) of this func:

I am new to Julia! I understand that I should use NLsolve package to solve system of nonlinear eq. Could you please tell me how to take each element of the gradient and solve for g(x) = 0.

Here is the code so far:

Blockquote
f1 = (x) → −13 + x[1] +((5 − x[2])*x[2] - 2)*x[2]
f2 = (x) → −29 + x[1] +((x[2] + 1)*x[2] − 14)*x[2]
f(x) = f1(x)^2 + f2(x)^2
g = (x) → ForwardDiff.gradient(f, x);
h = (x) → ForwardDiff.hessian(f, x);

Thanks for the help!

You can solve `g(x) = 0`

but, really, you could simply minimize (optimize) `f(x)`

. Example using Optimization.jl:

```
using Optimization, ForwardDiff, OptimizationOptimJL
f1(x) = −13 + x[1] + ((5 − x[2]) * x[2] - 2) * x[2]
f2(x) = −29 + x[1] + ((x[2] + 1) * x[2] − 14) * x[2]
f(x) = f1(x)^2 + f2(x)^2
# Initial guess
x0 = [0.0, 0.0]
# Set up the objective function and problem following SciML syntax:
func = OptimizationFunction((x,p) -> f(x), Optimization.AutoForwardDiff())
prob = OptimizationProblem(func, x0, p)
# And solve with, e.g., Newton's method. More examples at https://docs.sciml.ai/Optimization/stable/tutorials/rosenbrock/
sol = solve(prob, Newton())
# And test that the gradient is good:
ForwardDiff.gradient(f, sol)
```

returns

```
2-element Vector{Float64}:
1.141842176366481e-10
-1.1314114090055227e-9
```

which is very close to zero.

Visual check:

```
using Plots
x1s = -20:0.1:20
x2s = -5:0.1:5
contourf(x1s, x2s, [log(f([x1, x2])) for x2 in x2s, x1 in x1s])
scatter!([sol[1]], [sol[2]], marker=:x, color=:green, lab="sol")
title!("log(f(x1,x2))")
xlabel!("x1")
xlabel!("x2")
```

shows that its only a local minima:

2 Likes

Thank you for the response and for the solution!
I am doing a course on optimization and I want to implement the steps myself using Julia without the package.

So could you please show me how to solve g(x) and get the roots?

Thanks!

amrods
February 16, 2023, 6:12am
4
There are many ways you can do that. Check this book with optimization algorithms written in Julia: Algorithms for Optimization .

1 Like

Thank you!
I will check it out