Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rkube opened this issue Oct 13, 2020 · 3 comments
Closed

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

rkube opened this issue Oct 13, 2020 · 3 comments

Comments

@rkube
Copy link

rkube commented Oct 13, 2020

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.

@fredrikekre
Copy link

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])

@rkube
Copy link
Author

rkube commented Oct 13, 2020

Thanks, got it.

@pkofod
Copy link
Member

pkofod commented Dec 16, 2020

Yes, that is the suggested way.

@pkofod pkofod closed this as completed Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants