# Solve system of non-linear equation

**URL:** <https://discourse.julialang.org/t/solve-system-of-non-linear-equation/94706>\
**Category:** New to Julia\
**Tags:** nlsolve\
**Created:** [February 16, 2023, 3:45am UTC](https://discourse.julialang.org/t/solve-system-of-non-linear-equation/94706 "2023-02-16T03:45:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Students](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/students/32/46015_2.png) [@Students](https://discourse.julialang.org/u/Students)\
**Post date:** [February 16, 2023, 3:45am UTC](https://discourse.julialang.org/t/solve-system-of-non-linear-equation/94706/1 "2023-02-16T03:45:36Z")

</div>

Hello,

I am trying to find the stationary point (gradient g(x) = 0) of this func:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/1/c10961091e2f8afa2a2f9b9575223baff4e36a74.png)

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!

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [February 16, 2023, 4:45am UTC](https://discourse.julialang.org/t/solve-system-of-non-linear-equation/94706/2 "2023-02-16T04:45:02Z")

</div>

You can solve `g(x) = 0` but, really, you could simply minimize (optimize) `f(x)`. Example using Optimization.jl:

```julia
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

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

```

which is very close to zero.

Visual check:

```julia
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:

![discourse_nonlinearsolve](https://global.discourse-cdn.com/julialang/original/3X/5/5/5546d777e069c826da7215f0721eceecf8e8a4c9.png)

---

<div class="post-metadata">

**Author:** ![Students](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/students/32/46015_2.png) [@Students](https://discourse.julialang.org/u/Students)\
**Post date:** [February 16, 2023, 5:21am UTC](https://discourse.julialang.org/t/solve-system-of-non-linear-equation/94706/3 "2023-02-16T05:21:54Z")

</div>

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!

---

<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:** [February 16, 2023, 6:12am UTC](https://discourse.julialang.org/t/solve-system-of-non-linear-equation/94706/4 "2023-02-16T06:12:04Z")

</div>

There are many ways you can do that. Check this book with optimization algorithms written in Julia: [Algorithms for Optimization](https://algorithmsbook.com/optimization/).

---

<div class="post-metadata">

**Author:** ![Students](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/students/32/46015_2.png) [@Students](https://discourse.julialang.org/u/Students)\
**Post date:** [February 16, 2023, 6:26am UTC](https://discourse.julialang.org/t/solve-system-of-non-linear-equation/94706/5 "2023-02-16T06:26:34Z")

</div>

Thank you!  
I will check it out
