# NLsolve.jl for different input and output dimensions?

**URL:** <https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202>\
**Category:** Optimization (Mathematical)\
**Tags:** question, nlsolve, rootfinding\
**Created:** [April 28, 2022, 10:29am UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202 "2022-04-28T10:29:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [April 28, 2022, 10:29am UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/1 "2022-04-28T10:29:24Z")

</div>

Hi all,

I am currently trying out NLsolve.jl to find roots of given functions, which works great at the moment, but I realized that I can’t seem to find a way to specify a dimension for the output different from the input… Is there a way to do so, or are there other packages that allow this?

Thanks!

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [April 28, 2022, 12:06pm UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/2 "2022-04-28T12:06:15Z")

</div>

Hi, as far as I know, NLsolve.jl solves equations of the form

0=f\_1(x\_1, x\_2, x\_3, \ldots)\\ 0=f\_2(x\_1, x\_2, x\_3, \ldots)\\ 0=f\_3(x\_1, x\_2, x\_3, \ldots)\\ \ldots

If the number of equations does not match the number of variable x\_i your system is either under- or overdetermined. Are you certain the problem you are looking at is actually a root-finding problem? (or perhaps an optimization?)

---

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [April 29, 2022, 7:37am UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/3 "2022-04-29T07:37:11Z")

</div>

Actually, I know a solution is not unique, but in a set of dimension at least k ; that is why I would like to solve n+k equations, to get the local unicity again. I don’t know if this is the more appropriate way though.

I tried making changes of coordinates to drop dimensions, but this didn’t work well in my particular case.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [April 29, 2022, 8:01am UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/4 "2022-04-29T08:01:10Z")

</div>

NLsolve will only give you point solutions and therefore requires k=0.

I don’t know of a package which would give you the entire solutions set for k\>0, however:

You could add some cost function to indicate which point solution (out of the complete solution set) you are interested in. Then solve it as an optimization problem with your n equations as side conditions.

EDIT: Or if you just want any solution you can just append k zeros to the output and NLsolve will hopefully converge to one solution out of your k-dimensional set.

---

<div class="post-metadata">

**Author:** ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)\
**Post date:** [April 29, 2022, 8:57am UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/5 "2022-04-29T08:57:01Z")

</div>

You might want to try this out: [Interval constraint programming tutorial](https://juliaintervals.github.io/pages/tutorials/tutorialConstraintProgramming/)

What motivates your need? What problem do you want to solve?

---

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [May 2, 2022, 8:46am UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/6 "2022-05-02T08:46:28Z")

</div>

Characterising the entire solution set is a big part of the problem indeed… I was counting on NLsolve to find at least one root. I thought of the cost function, but doesn’t it require (if the equations f\_j(x\_i) = 0 are side conditions) the initial point to satisfy them already ?  
As for the k zeros, my problem is overdetermined, so I should add k free variables to the input maybe. Didn’t try this one out, I will give it a shot.

---

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [May 2, 2022, 8:53am UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/7 "2022-05-02T08:53:19Z")

</div>

I will look at this, thanks !

I need to find the roots of a function in \mathbb{R}^{18} (I can put bounds, so that I have [-a; a]^{18} instead), but as I said, the roots are actually in continuous sets, and the function is costly to evaluate (about a second per call). I looked into NOMAD.jl, (blackbox optimization) but it didn’t succeed, so I am currently trying other methods.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [May 2, 2022, 8:59am UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/8 "2022-05-02T08:59:49Z")

</div>

If the problem is overdetermined (but you are certain a solution exists) you could add some free variables to match the number of conditions?

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [May 2, 2022, 10:39am UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/9 "2022-05-02T10:39:26Z")

</div>

I looks like you could formulate this as a nonlinear least squares problem.

\min F(x), \mbox{ where } F(x) = \sum f\_i^2(x)

and use an optimizer. If the input dimension is N and the output is M, the M \> N is the overdetermined case and, while you may not have a solution, the optimizer will tell you something. In the underdetermined (M \< N) case a least squares code would likely find one solution.

I am not clear on what kind of problem you have. I see that N = 18. Is M = 18-k?

I think you should try a nonlinear least squares code before trying interval methods or direct search methods like Nomad because gradient-based methods work better (most of the time) if your problem is smooth.

---

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [May 2, 2022, 12:37pm UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/10 "2022-05-02T12:37:32Z")

</div>

I am in the overdetermined case (M = 18 + k \> N), I add equations that a solution must satisfy. I will try a least squares method then !

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [May 2, 2022, 1:44pm UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/11 "2022-05-02T13:44:23Z")

</div>

Just fyi, _overdetermined_ means more equations than variables.

_Underdetermined_ means more variables than equations.

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [May 2, 2022, 2:26pm UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/12 "2022-05-02T14:26:56Z")

</div>

You should try to find a solver that does not add the artificial k equations, unless the added equations are not artificial and mean something for the problem. The risk in adding equations is that you can turn a problem with a zero residual into a problem without one.

---

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [May 2, 2022, 3:10pm UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/13 "2022-05-02T15:10:41Z")

</div>

So I tried using this, but NLsolve doesn’t converge in this case for my problem. Currently trying it out on smaller test cases to see what’s going on.

---

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [May 2, 2022, 3:13pm UTC](https://discourse.julialang.org/t/nlsolve-jl-for-different-input-and-output-dimensions/80202/14 "2022-05-02T15:13:29Z")

</div>

Because of the problem’s symmetries, I am sure that if a solution exists, it will belong to a continuous family of solutions. I tried changing variables to drop some and get the unicity this way, but it dramatically slows down the process - no clue why.

(I should maybe add that the function I am minimizing uses TaylorIntegration’s taylorinteg, and thus has no analytical form)
