2
votes

I'm pretty new on Julia and have a question that may appear simple. Say I have a function, which I will name test(x::Vector, arg1, arg2) where x is a vector of variables and the function has two arguments arg1 & arg2.

I would like to optimize (minimize) the function test with respect to the vector x. I can't figure out how to use the optimize function from the Optim package that accepts two arguments values. In R, one may do as following:

optim(initial guest, test, arg1=value1,arg2=value2)

Is there a similar way to declare argument value in Julia?

1

1 Answers

4
votes

You can define another function that fixes the value of those arguments.

# Function to minimize
f(x::Vector, a, b) = (x[1] - a)^2 + (x[2] - b)^2

using Optim
g(x::Vector) = f(x, 3, 4)
optimize(g, [0.,0.])

You could also use an anonymous function (but it may be less efficient).

optimize(x -> f(x,3,4), [0.,0.])