NLsolve problem

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!

Hello and welcome to the community :wave: 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.

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.

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:

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.

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

but it returns the same error as always.

Thanks.

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

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

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

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

Thanks for your help! :slight_smile: