# Adaptation of NLsolve for function with vector output

**URL:** https://discourse.julialang.org/t/adaptation-of-nlsolve-for-function-with-vector-output/103139
**Category:** General Usage
**Tags:** nlsolve
**Created:** [August 24, 2023, 1:00am UTC](https://discourse.julialang.org/t/adaptation-of-nlsolve-for-function-with-vector-output/103139 "2023-08-24T01:00:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Outeirom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/outeirom/32/43141_2.png) [@Outeirom](https://discourse.julialang.org/u/Outeirom)
#### Post date: [August 24, 2023, 1:00am UTC](https://discourse.julialang.org/t/adaptation-of-nlsolve-for-function-with-vector-output/103139/1 "2023-08-24T01:00:49Z")

</div>

I’m facing an issue with NLSolve. I have two interacting functions that are working well, and I need to adapt them. Let me briefly summarize how the functions are currently set up and what I’m aiming to achieve.

Firstly, I have a function that calculates the government deficit:

```julia
function deficit(t)
    tax_h, tax_l = t[1], t[2]

    # Lots of calculations
    
    dif_tax = (ep_h * wp_h * tax_h - (eg_h * wg_h)) + (ep_l * wp_l * tax_l - (eg_l * wg_l))
    F = [dif_tax]
    return F
end

```

Then, I’m using another function that incorporates the `deficit` function within it, like this:

```julia
function solvemodeltax(chute_param, chute_th, chute_tl)
    chute_t = [chute_th, chute_tl]
    taxopt = nlsolve(x -> deficit(x), chute_t).zero
    tax_h = taxopt[1]
    tax_l = taxopt[2]

    # Lots of calculations
return output
end

```

And this is working fine. However, I’m aiming to perform a counterfactual exercise where the government needs to balance the budget for each specific sector (noting that H represents high skill and L represents low skill). Consequently, I’ve modified the code as follows

```julia
function deficit_separatesector(t)
    tax_h, tax_l = t[1], t[2]

    # Lots of calculations

    dif_taxh = ep_h * wp_h * tax_h - (eg_h * wg_h)
    dif_taxl = ep_l * wp_l * tax_l - (eg_l * wg_l)
    F = [dif_taxh, dif_taxl]
    return F
end 

```

In this case, the return is no longer a one-element array, but a two-element collection representing the deficit for each type of worker. Consequently, I adapted the solving function as follows (with a focus on the relevant parts):

```julia
function solvemodeltax_v2(chute_param, chute_th, chute_tl)
    chute_t = [chute_th, chute_tl]
    taxopt = nlsolve(x -> deficit_separatesector(x, kappa_h=kappa_h, kappa_l=kappa_l, b_h=b_h, b_l=b_l, y_h=y_h), chute_t).zero
    tax_h = taxopt[1]
    tax_l = taxopt[2]

    # Lots of calculations

  return output
end

```

In this last part, this final NLSolve is where the code encounters issues. The dimensions seem correct, and everything else appears fine, but I’m uncertain why it’s unable to find a solution. Is this an error within the code or a theoretical problem? Is there anything I can do to improve it?

From my perspective, it should work since the deficits are interlinked, suggesting that it’s possible (due to function continuity) to find a rate that optimally combines both sectors.

Note: I haven’t provided the entire function details, only the main aspects, as they are quite extensive. I believe this should give you an understanding of my point and the challenges.

---

<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: [August 24, 2023, 3:26am UTC](https://discourse.julialang.org/t/adaptation-of-nlsolve-for-function-with-vector-output/103139/2 "2023-08-24T03:26:45Z")

</div>

The problem is that nlsolve doesn’t converge? If so, then it is very hard to suggest anything if you don’t share what happens inside those functions.

---

<div class="post-metadata">

### Author: ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)
#### Post date: [August 24, 2023, 5:17am UTC](https://discourse.julialang.org/t/adaptation-of-nlsolve-for-function-with-vector-output/103139/3 "2023-08-24T05:17:09Z")

</div>

It may not be a programming problem: are you sure that the two equations you are trying to find a zero for actually can be zero at the same time? In other words: does the problem actually have a solution?

---

<div class="post-metadata">

### Author: ![Outeirom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/outeirom/32/43141_2.png) [@Outeirom](https://discourse.julialang.org/u/Outeirom)
#### Post date: [August 24, 2023, 7:58pm UTC](https://discourse.julialang.org/t/adaptation-of-nlsolve-for-function-with-vector-output/103139/4 "2023-08-24T19:58:59Z")

</div>

The problem is:

DomainError with NaN:  
integrand produced NaN in the interval (NaN, 38.328194321555955)

Thank you for the response. Initially, I thought it might be a programming problem, especially concerning `nlsolve` . However, I now believe it’s not a problem but rather a limitation of the model. It’s not possible to achieve what I was attempting to do: setting the tax level for high-skilled workers too high, which implies the absence of a private sector for such workers.

---

<div class="post-metadata">

### Author: ![Outeirom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/outeirom/32/43141_2.png) [@Outeirom](https://discourse.julialang.org/u/Outeirom)
#### Post date: [August 24, 2023, 8:07pm UTC](https://discourse.julialang.org/t/adaptation-of-nlsolve-for-function-with-vector-output/103139/5 "2023-08-24T20:07:56Z")

</div>

Yeah, I believe the code does not converge. However, my question is to know if the code I provided would work in NLsolve. I mean would nlsolve optim problem understand that I need taxes such that F (as below) is zero for each cell of the vector F, namely: dif\_taxh and dif\_taxl

```julia
F = [dif_taxh, dif_taxl]

```

[/quote]
