# Solving non-linear equations with anonymous function

**URL:** <https://discourse.julialang.org/t/solving-non-linear-equations-with-anonymous-function/64934>\
**Category:** New to Julia\
**Tags:** question, diffeq, matlab\
**Created:** [July 19, 2021, 4:43pm UTC](https://discourse.julialang.org/t/solving-non-linear-equations-with-anonymous-function/64934 "2021-07-19T16:43:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bozgulec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bozgulec/32/27316_2.png) [@bozgulec](https://discourse.julialang.org/u/bozgulec)\
**Post date:** [July 19, 2021, 4:43pm UTC](https://discourse.julialang.org/t/solving-non-linear-equations-with-anonymous-function/64934/1 "2021-07-19T16:43:18Z")

</div>

Hello Dear Julians,

I’m currently translating a code from MATLAB to Julia and I got stuck at reformulating a non-linear differential equation problem. The code in MATLAB is as follows,

> for i = 1:numel(rrangeopt)
> 
> ```
> r = rrangeopt(i);
> welfarecurrent(i) = welfare(c,l,s,mass,r,My,e); % welfare under current tax
> 
> % compute lump sum payment equivalent to welfare gain from optimal tax using the function welfare
> % absolute change in lump sum payment:
> cwelfaregainlumpabs(i) = fsolve(@(add) 100000*(welfare(c+add,l,s+add,mass,r,My,e)-welfareopt(i)),1);
> % relative to current average income:
> cwelfaregainlumpgdp(i) = cwelfaregainlumpabs(i)/ymean;
> % relative to current lump sum payment:
> cwelfaregainlump (i) = cwelfaregainlumpabs(i)/s;
> 
> ```
> 
> end

I want to reformulate _fsolve(@(add) 100000_(welfare(c+add,l,s+add,mass,r,My,e)-welfareopt(i)),1)\* in Julia and solve it with NlSolve package but I couldn’t find ant documents online to help me. _welfare_ is a function while _welfareopt_ is a vector.

I would appreciate any input to help me understand what the MATLAB code does and how can I reformulate it in Julia.

Best,  
Bahar

---

<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:** [July 19, 2021, 9:17pm UTC](https://discourse.julialang.org/t/solving-non-linear-equations-with-anonymous-function/64934/2 "2021-07-19T21:17:58Z")

</div>

> [@bozgulec](#):
>
> `fsolve(@(add) 100000*(welfare(c+add,l,s+add,mass,r,My,e)-welfareopt(i)),1);`

I guess you want something like:

```julia
result = nlsolve(add -> 100000*(welfare(c+add,l,s+add,mass,r,My,e)-welfareopt[i]),1)

```

However, `nlsolve` currently doesn’t accept scalar-valued functions ([NLsolve.jl#272](https://github.com/JuliaNLSolvers/NLsolve.jl/issues/272)), so you have to wrap it in a vector, smething like:

```julia
result = nlsolve(add -> 100000*(welfare(c+add[1],l,s+add[1],mass,r,My,e)-welfareopt[i]),[1.0])

```
