JuliaNLSolvers / NLsolve.jl

Julia solvers for systems of nonlinear equations and mixed complementarity problems

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I pass parameters to the function I want to minimize?

rkube opened this issue · comments

Hi,
l'd like to pass parameters to the function to minimize. Something like

using NLsolve

function f!(F, x, params)
    a, b = params
    F[1] = (x[1] + a)*(x[2]^3 - b) + 18
    F[2] = sin(x[2] * exp(x[1]) - 1)
end

I also don't have a Jacobian for this function. What is the syntax to pass a vector into nlsolve so that this is passed into my function? I've tried

p = [1.0 1.0]
nlsolve(f!, [0.1, 1.0]; params=p)

but never got this working.

What is the syntax to pass a vector into nlsolve so that this is passed into my function?

Typically you create a new function that only takes F and x as arguments and encloses p, e.g.

julia> g!(F, x) = f!(F, x, p);

julia> nlsolve(g!, [0.1, 1.0])

or with an anonymous function

nlsolve((F, x) -> f!(F, x, p), [0.1, 1.0])

Thanks, got it.

Yes, that is the suggested way.