# NLsolve problem

**URL:** <https://discourse.julialang.org/t/nlsolve-problem/106222>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [November 14, 2023, 2:49pm UTC](https://discourse.julialang.org/t/nlsolve-problem/106222 "2023-11-14T14:49:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GianmarcoPreso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianmarcopreso/32/202340_2.png) [@GianmarcoPreso](https://discourse.julialang.org/u/GianmarcoPreso)\
**Post date:** [November 14, 2023, 2:49pm UTC](https://discourse.julialang.org/t/nlsolve-problem/106222/1 "2023-11-14T14:49:24Z")

</div>

Hi all!

I’m new in Julia. I have to solve a non-linear equation, and I have found the NLsolve package.

function coolebrok(fric\_fact)  
1 ./sqrt(fric\_fact[:]) + 2\*log10(epsilon./. (3.7.\*DD) + 2.51./(sqrt(fric\_fact[:]).\*max(Re, 1e-10)))  
end

sol = nlsolve(coolebrok, ff[:]./10)

this is what i have writtend in my code. Consider that fric\_fact has to be a vector of 703x1 elements. This code is not working. Where is the mistake? Consider that this equation is solvable with fsolve in Matlab.

Thanks!

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 14, 2023, 2:52pm UTC](https://discourse.julialang.org/t/nlsolve-problem/106222/2 "2023-11-14T14:52:59Z")

</div>

Hello and welcome to the community 👋 consider formatting your code as code using triple back ticks ````` and include a running example that produces the error you are seeing. This makes it easier for people to help you.

From just looking at the code, you’re trying to compute `sqrt(array)` which does not work in Julia. To compute the element-wise sqare root, use broadcasting: `sqrt.(array)`.

You might also want to let `fric_fact` be a `Vector` rather than a `703 x 1 Matrix` so that you don’t have to write `[:]` everywhere.

---

<div class="post-metadata">

**Author:** ![GianmarcoPreso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianmarcopreso/32/202340_2.png) [@GianmarcoPreso](https://discourse.julialang.org/u/GianmarcoPreso)\
**Post date:** [November 14, 2023, 4:59pm UTC](https://discourse.julialang.org/t/nlsolve-problem/106222/3 "2023-11-14T16:59:05Z")

</div>

Thanks baggepinnen.  
I tried to modify the code how you said, but it still doesn’t work. I think it’s not a problem of the math, but it’s a problem of the solver.

```julia
function coolebrok(fric_fact)
    1 ./sqrt.(fric_fact) + 2*log10(epsilon./(3.7.*DD) + 2.51./(sqrt.(fric_fact).*max(Re, 1e-10)))
end

initial_guess = ff[:]./10
sol = nlsolve(coolebrok, initial_guess)

```

If i write the code like that, the error is:

```julia
no method matching isless(::Float64, ::Matrix{Float64})

```

I also tried the solver LeastSquareOptim, which allows me to use the levenberg-marquardt algorithm which is the algorithm used in fsolve in Matlab.

```julia
fric_fact = zeros(703)
optimize(coolebrok, fric_fact, LevenbergMarquardt())

```

but it returns the same error as always.

Thanks.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 14, 2023, 5:01pm UTC](https://discourse.julialang.org/t/nlsolve-problem/106222/4 "2023-11-14T17:01:37Z")

</div>

> [@baggepinnen](#):
>
> and include a running example that produces the error you are seeing

this is still required to help you. What is `DD` and all the other variables that are undefined?

> [@GianmarcoPreso](#):
>
> `isless(::Float64, ::Matrix{Float64})`

somewhere you are computing `scalar < matrix`, but it’s hard to tell without the code

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [November 14, 2023, 5:14pm UTC](https://discourse.julialang.org/t/nlsolve-problem/106222/5 "2023-11-14T17:14:40Z")

</div>

> [@GianmarcoPreso](#):
>
> `max(Re, 1e-10)`

`Re` might be a matrix (use `max.(Re, 1e-10)` instead) !

---

<div class="post-metadata">

**Author:** ![GianmarcoPreso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianmarcopreso/32/202340_2.png) [@GianmarcoPreso](https://discourse.julialang.org/u/GianmarcoPreso)\
**Post date:** [November 15, 2023, 9:53am UTC](https://discourse.julialang.org/t/nlsolve-problem/106222/7 "2023-11-15T09:53:57Z")

</div>

I have solved the problem with the Optim package, which allows me to apply a specific algorithm.

Thanks for your help! 🙂
