# I am seeing strange behavior in NLsolve package. Mutating function is not working

**URL:** <https://discourse.julialang.org/t/i-am-seeing-strange-behavior-in-nlsolve-package-mutating-function-is-not-working/104359>\
**Category:** General Usage\
**Tags:** nlsolve\
**Created:** [September 28, 2023, 4:33pm UTC](https://discourse.julialang.org/t/i-am-seeing-strange-behavior-in-nlsolve-package-mutating-function-is-not-working/104359 "2023-09-28T16:33:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shashank](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashank/32/12323_2.png) [@Shashank](https://discourse.julialang.org/u/Shashank)\
**Post date:** [September 28, 2023, 4:33pm UTC](https://discourse.julialang.org/t/i-am-seeing-strange-behavior-in-nlsolve-package-mutating-function-is-not-working/104359/1 "2023-09-28T16:33:16Z")

</div>

I am trying to optimize a function that has several contributions. So I need to add several terms to it, but when I pass that function to nlsolve I am getting strange results. I have reproduced a minimal example that reproduces the problem.

```julia
using SparseArrays
using NLsolve
bins = Int(4)

function f!(f0,x)
    f0=0*x
    for i in 1:bins
        f0[i] = f0[i] + x[i]*x[i] - 1
    end
end

function j!(j0,x)
    for i in 1:bins
        j0[i,i] = 2.0*x[i]
    end
end

x0 = 0.5*ones(bins)
f0 = zeros(Float64,bins)
f!(f0,x0)
j0 = spzeros(bins, bins)
j!(j0,x0)
df = OnceDifferentiable(f!, j!, x0, f0, j0)

sol = nlsolve(df, x0,method=:newton)
println(sol)

```

If I run this code I get the answer [0.5,0.5,0.5,0.5], which is obviously wrong. But if I change the function f! to

```julia
function f!(f0,x)
    for i in 1:bins
        f0[i] = x[i]*x[i] - 1
    end
end

```

then I get the correct result. If I want to optimize a function that has several components it is easier to initialize f0 to zero and add components to it. But that does not work with NLsolve. Is there a way to initialize f0 to zero and add various components to it? Also, this behavior is quite nonintuitive. Functions should not behave this way depending on the sequence of mutation.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [September 28, 2023, 4:40pm UTC](https://discourse.julialang.org/t/i-am-seeing-strange-behavior-in-nlsolve-package-mutating-function-is-not-working/104359/2 "2023-09-28T16:40:26Z")

</div>

> [@Shashank](#):
>
> `f0=0*x`

This assigns the name `f0` to a new value `0*x` so the value you are intended to mutate is no longer accessible. Try

```julia
f0[:] = 0*x

```

---

<div class="post-metadata">

**Author:** ![Shashank](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashank/32/12323_2.png) [@Shashank](https://discourse.julialang.org/u/Shashank)\
**Post date:** [September 28, 2023, 4:45pm UTC](https://discourse.julialang.org/t/i-am-seeing-strange-behavior-in-nlsolve-package-mutating-function-is-not-working/104359/3 "2023-09-28T16:45:57Z")

</div>

Thanks a lot! That works.
