我在朱莉娅(Julia)上还很新,有一个问题可能看起来很简单.假设我有一个函数,我将其命名为test(x::Vector, arg1, arg2),其中x是变量的向量,并且该函数具有两个参数arg1& arg2.
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.
我想针对向量x优化(最小化)函数test.我无法弄清楚如何使用Optim包中的optimize函数来接受两个参数值.在R中,可以执行以下操作:
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)在Julia中是否有类似的方法来声明参数值?
Is there a similar way to declare argument value in Julia?
推荐答案您可以定义另一个函数来修复这些参数的值.
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.])更多推荐
将其他参数传递给内置函数
发布评论