# Basic usage of NLsolve for scalar problems

**URL:** <https://discourse.julialang.org/t/basic-usage-of-nlsolve-for-scalar-problems/28489>\
**Category:** Optimization (Mathematical)\
**Created:** [September 6, 2019, 11:27pm UTC](https://discourse.julialang.org/t/basic-usage-of-nlsolve-for-scalar-problems/28489 "2019-09-06T23:27:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [September 6, 2019, 11:27pm UTC](https://discourse.julialang.org/t/basic-usage-of-nlsolve-for-scalar-problems/28489/1 "2019-09-06T23:27:33Z")

</div>

I have troubles to use `NLsolve` package for solving the following simple scalar example:

```nohighlight
0 = cos(x)

```

for `x` initialized to, say, 1.5 (the anticipated solution is thus pi/2, that is, something like 1.57). The code is

```julia
julia> using NLsolve

julia> function f!(r,x)
             r = cos.(x) # x (and r) are expected to be vectors, hence the broadcasting
       end
f! (generic function with 1 method)

julia> function j!(J,x)
             J = -sin.(x)
       end
j! (generic function with 1 method)

julia> sol = nlsolve(f!,j!,[1.5])
Results of Nonlinear Solver Algorithm
 * Algorithm: Trust-region with dogleg and autoscaling
 * Starting Point: [1.5]
 * Zero: [1.5]
 * Inf-norm of residuals: 0.000000
 * Iterations: 0
 * Convergence: true
   * |x - x'| < 0.0e+00: false
   * |f(x)| < 1.0e-08: true
 * Function Calls (f): 1
 * Jacobian Calls (df/dx): 1

julia> sol.zero
1-element Array{Float64,1}:
 1.5

```

Not even a single iteration is run. What am I doing wrong? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 7, 2019, 1:26am UTC](https://discourse.julialang.org/t/basic-usage-of-nlsolve-for-scalar-problems/28489/2 "2019-09-07T01:26:03Z")

</div>

maybe this?

```julia
function f!(r,x)
    r .= cos.(x) # x (and r) are expected to be vectors, hence the broadcasting
end

function j!(J,x)
    (s1,s2) = size(J) #the jacobian is a square matrix, not a vector. the analytical derivative is Diagonal(-sin(x))
    J .= zeros(s1,s1)
    for i in 1:s1
        J[i,i] = -sin(x[i])
    end
end

```

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [September 7, 2019, 8:31am UTC](https://discourse.julialang.org/t/basic-usage-of-nlsolve-for-scalar-problems/28489/3 "2019-09-07T08:31:24Z")

</div>

Yup. The `r = cos.(x)` doesn’t actually change the value of r, it just reassigns it. For scalar problems, use Roots, though.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [September 7, 2019, 11:15am UTC](https://discourse.julialang.org/t/basic-usage-of-nlsolve-for-scalar-problems/28489/4 "2019-09-07T11:15:43Z")

</div>

Thanks to both of you. Indeed, the problem was in the difference between `=` and `.=`. The modified code

```julia
using NLsolve

function f!(r,x)
      r .= cos.(x) # x (and r) are expected to be vectors, hence the broadcasting
end

function j!(J,x)
      J .= -sin.(x)
end

sol = nlsolve(f!,j!,[1.5])

sol.zero

```

is functional.

I think I need to read a bit more about the `.=` stuff. I guess that this paragraph

> Finally, the maximum efficiency is typically achieved when the output array of a vectorized operation is _pre-allocated_ , so that repeated calls do not allocate new arrays over and over again for the results (see [Pre-allocating outputs](https://docs.julialang.org/en/v1/manual/performance-tips/#Pre-allocating-outputs-1)). A convenient syntax for this is `X .= ...` , which is equivalent to `broadcast!(identity, X, ...)` except that, as above, the `broadcast!` loop is fused with any nested “dot” calls. For example, `X .= sin.(Y)` is equivalent to `broadcast!(sin, X, Y)` , overwriting `X` with `sin.(Y)` in-place. If the left-hand side is an array-indexing expression, e.g. `X[2:end] .= sin.(Y)` , then it translates to `broadcast!` on a `view` , e.g. `broadcast!(sin, view(X, 2:lastindex(X)), Y)` , so that the left-hand side is updated in-place.

from the section " [Dot Syntax for Vectorizing Functions](https://docs.julialang.org/en/v1/manual/functions/#man-vectorized-1)" in the manual contains the explanation. I might need some quiet time to digest it.

And, of course, in this particular case, using `Roots.jl` is certainly the way to solve the problem but I wanted to understand this issue to learn Julia itself better. Thanks.
