# How to solve non-linear system with input array?

**URL:** <https://discourse.julialang.org/t/how-to-solve-non-linear-system-with-input-array/28336>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [September 3, 2019, 4:52pm UTC](https://discourse.julialang.org/t/how-to-solve-non-linear-system-with-input-array/28336 "2019-09-03T16:52:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alvrg](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@alvrg](https://discourse.julialang.org/u/alvrg)\
**Post date:** [September 3, 2019, 4:52pm UTC](https://discourse.julialang.org/t/how-to-solve-non-linear-system-with-input-array/28336/1 "2019-09-03T16:52:37Z")

</div>

Hi,

I want to find the roots of a system of non-linear equations like this one:

```julia
function f!(F::Vector{Float64},
            Δ::Vector{Float64},
            L::Int64,
            N::Int64,
            g::Float64,
            Γ::Float64,
            Z::AbstractMatrix{Float64})
    F[1:L] .= Z*Δ
    for i=1:L
        F[i] += g^2*N*(L-N)*Γ - 2Δ[i] + g*sum(Z[i, :])*Δ[i] - Δ[i]^2
    end
    F[L+1] = sum(Δ) + 2N
    return F
end

```

My problem is that I wanted to use [`NLsolve`](https://github.com/JuliaNLSolvers/NLsolve.jl#nlsolvejl) to solve this problem, but the function `f!` has an argument `Z` that is an array and, therefore, `NLsolve` can’t handle it. There seems to be an [issue](https://github.com/JuliaNLSolvers/NLsolve.jl/issues/214) already filed about this but there has not been any response yet in some months.

Do you know what solver I could use to find the roots of my function?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 3, 2019, 5:00pm UTC](https://discourse.julialang.org/t/how-to-solve-non-linear-system-with-input-array/28336/2 "2019-09-03T17:00:47Z")

</div>

> [@alvrg](#):
>
> but the function `f!` has an argument `Z` that is an array

Do you mean that your unknowns are the matrix `Z`, and NLsolve expects a vector? That is easy — you can simply use `reshape` to convert NLsolve’s vector into a matrix of the corresponding size or vice versa.

If `Z` is a fixed parameter, and not a variable that you are solving for, it is even easier — just use a closure to wrap the parameter in the function that you pass to NLsolve.

---

<div class="post-metadata">

**Author:** ![alvrg](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@alvrg](https://discourse.julialang.org/u/alvrg)\
**Post date:** [September 3, 2019, 5:27pm UTC](https://discourse.julialang.org/t/how-to-solve-non-linear-system-with-input-array/28336/3 "2019-09-03T17:27:42Z")

</div>

Sorry! It’s the second one, `F` preallocates the output, `Δ` are the unknown variables, and the other arguments are fixed.

By closure do you mean something like this

```julia
function main()
    L = 10
    Z = randn(L, L)

    clos(F, Δ) = f!(F, Δ, L, Z)
end

```

and pass `clos` as the function to solve? Is it not going to affect performance that the function depends on variables outside it?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 3, 2019, 5:52pm UTC](https://discourse.julialang.org/t/how-to-solve-non-linear-system-with-input-array/28336/4 "2019-09-03T17:52:06Z")

</div>

> [@alvrg](#):
>
> By closure do you mean something like this […] and pass `clos` as the function to solve? Is it not going to affect performance that the function depends on variables outside it?

Yes, and no that doesn’t hurt performance in general. Closures are the standard way to pass additional parameters through a higher-order function.

---

<div class="post-metadata">

**Author:** ![alvrg](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@alvrg](https://discourse.julialang.org/u/alvrg)\
**Post date:** [September 3, 2019, 10:15pm UTC](https://discourse.julialang.org/t/how-to-solve-non-linear-system-with-input-array/28336/5 "2019-09-03T22:15:58Z")

</div>

Great, it works, thanks!! 😃
