# Basic solve with Optim/ NLsolve

**URL:** <https://discourse.julialang.org/t/basic-solve-with-optim-nlsolve/109125>\
**Category:** Optimization (Mathematical)\
**Created:** [January 23, 2024, 2:52am UTC](https://discourse.julialang.org/t/basic-solve-with-optim-nlsolve/109125 "2024-01-23T02:52:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)\
**Post date:** [January 23, 2024, 2:52am UTC](https://discourse.julialang.org/t/basic-solve-with-optim-nlsolve/109125/1 "2024-01-23T02:52:58Z")

</div>

I have a very simple problem, but a little stomped. Below is a MWE

```julia
using NLsolve
f! = function (dx, x, a, b) # we want to find roots for f
    dx[1] = x[1] - a[1]
    dx[2] = x[2] = a[2]
    b = 5
end
a = [1; 5]
b = 0
g!(dx, x) = f!(dx, x, a, b)
res = nlsolve(g!, [1.0; 1.0])

```

How do I return an updated ‘b’ from the above f! function.  
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:** [January 23, 2024, 3:00am UTC](https://discourse.julialang.org/t/basic-solve-with-optim-nlsolve/109125/2 "2024-01-23T03:00:05Z")

</div>

You need to use something other than an `Int`:

```Julia
julia> function foo(b::Int)
           b = 3
           return
       end
foo (generic function with 1 method)

julia> function bar(b::Ref{Int})
           b[] = 3
           return
       end
bar (generic function with 1 method)

julia> b = 2
2

julia> foo(b)

julia> c = Ref(2)
Base.RefValue{Int64}(2)

julia> bar(c)

julia> c[]
3

```

But I don’t know if your `f!` function makes sense. It overwrites `x[2]` to the constant `a[2]`.

Even if you update `b`, the final value will be whatever was last calculated. There’s no guarantee this will correspond to a root (even if it probably will).
