# Parametrize NLsolve function

**URL:** <https://discourse.julialang.org/t/parametrize-nlsolve-function/11886>\
**Category:** Optimization (Mathematical)\
**Created:** [June 22, 2018, 5:54pm UTC](https://discourse.julialang.org/t/parametrize-nlsolve-function/11886 "2018-06-22T17:54:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 22, 2018, 5:54pm UTC](https://discourse.julialang.org/t/parametrize-nlsolve-function/11886/1 "2018-06-22T17:54:12Z")

</div>

Hello,  
I want to solve a function using NLsolve, which has parameters. I do not want to pass them using global variables.

Simplified example:

```julia
using NLsolve
function f!(F, x)
    global a, b
    F[1] = a * sin(x[1]) + b
end
a = 1.0
b = 0.5
nlsolve(f!, [0.])

```

How can I solve this problem without using global variables?

---

<div class="post-metadata">

**Author:** ![crinders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crinders/32/8434_2.png) [@crinders](https://discourse.julialang.org/u/crinders)\
**Post date:** [June 22, 2018, 6:22pm UTC](https://discourse.julialang.org/t/parametrize-nlsolve-function/11886/2 "2018-06-22T18:22:30Z")

</div>

```julia
function f!(F,x,a,b)
   F[1] = a * sin(x[1]) + b
 end 
a = 1.0
b = 0.5 
nlsolve((F,x) ->f!(F,x,a,b), [0.])

```

Should do the trick.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 22, 2018, 6:58pm UTC](https://discourse.julialang.org/t/parametrize-nlsolve-function/11886/3 "2018-06-22T18:58:03Z")

</div>

Thanks! This works nicely. 🙂

---

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [July 16, 2019, 10:25pm UTC](https://discourse.julialang.org/t/parametrize-nlsolve-function/11886/4 "2019-07-16T22:25:27Z")

</div>

Hi, thanks for the reply on thi. Could you please comment on what does it mean the construction `(F,x) ->f!(F,x,a,b)` ?

And, more important, could you prove a link to any references to understand this concept in depth ? Thanks in advance !

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 16, 2019, 10:28pm UTC](https://discourse.julialang.org/t/parametrize-nlsolve-function/11886/5 "2019-07-16T22:28:04Z")

</div>

The `->` syntax creates an anonymous function: [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions-1)

```nohighlight
foo = x -> 2 * x

```

is equivalent to

```nohighlight
function foo(x)
    return 2 * x
end

```
