# How to deal with vectors in NLsolve.jl

**URL:** <https://discourse.julialang.org/t/how-to-deal-with-vectors-in-nlsolve-jl/107488>\
**Category:** General Usage\
**Tags:** nlsolve\
**Created:** [December 12, 2023, 10:01am UTC](https://discourse.julialang.org/t/how-to-deal-with-vectors-in-nlsolve-jl/107488 "2023-12-12T10:01:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)\
**Post date:** [December 12, 2023, 10:01am UTC](https://discourse.julialang.org/t/how-to-deal-with-vectors-in-nlsolve-jl/107488/1 "2023-12-12T10:01:00Z")

</div>

Hi guys!

I didn’t find a way to deal with vector using pkg `NLSolve` in the following problem.

Let q=[x,y] and p=[u,v] and defining the functions f and g as f(q) = q / (√(q'\*q))^3,\ g(p) = p. Now, giving q\_0=[0.4,0] and p\_0=[0,0.5]. I need to solve by Newton-Raphson method the following

F(q\_0,p\_0,q,p) = [q - p\_0 + 0.5 \cdot f((q\_0+q)/2) , p - q\_0 - 0.5 \cdot g((p\_0+p)/2)]

```julia
using ForwardDiff

f(q) = q / (√(q'*q))^3;
g(p) = p;   
    
F(q₀,p₀,q,p) = [q - p₀ + 0.5 * f((q₀+q)/2) , p - q₀ - 0.5 * g((p₀+p)/2)]
ini_gauss = [q₀,p₀]
nlsolve((q,p)-> F(q₀,p₀,q,p), ini_gauss, autodiff = :forward, method=:newton, ftol=1e-14) 

```

 ![Screenshot from 2023-12-12 10-57-34](https://global.discourse-cdn.com/julialang/original/3X/1/c/1c55e2a81078ca6f800be5b2aa2a2b33edd8fde8.png)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 12, 2023, 10:43am UTC](https://discourse.julialang.org/t/how-to-deal-with-vectors-in-nlsolve-jl/107488/2 "2023-12-12T10:43:51Z")

</div>

Your parameters can’t be a vector of vectors, you need to do something like

```julia
ini_gauss = [q₀..., p₀...]

```

i.e. you need to flatten this structure. You’ll also need to adjust that closure, i.e.

```julia
(q,p)-> F(q₀,p₀,q,p)

```

should be

```julia
qp -> F(q₀,p₀, view(qp, 1:2), view(qp, 3:4))

```

and similarly, `F` should return a flat vector instead of a vector of vectors:

```julia
function F(q₀,p₀,q,p) 
    p′ = q - p₀ + 0.5 * f((q₀+q)/2) 
    q′ = p - q₀ - 0.5 * g((p₀+p)/2)
    [p′..., q′...]
end

```

Note, the above code is not very optimized and there’s a lot that could be done to make it faster. However, at least with the initial conditions you gave, it seems this system of equations does not converge to a solution with any of the solvers I tried, so I wonder if there’s a problem with your initial conditions or your equations.

---

<div class="post-metadata">

**Author:** ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)\
**Post date:** [December 17, 2023, 9:49pm UTC](https://discourse.julialang.org/t/how-to-deal-with-vectors-in-nlsolve-jl/107488/3 "2023-12-17T21:49:17Z")

</div>

It works, great! Many thanks @Mason
