使用 Matlab 进行优化
Optimization with Matlab
matlab中objective函数的常用写法(包括梯度向量)如下:
[L,G] = objfun(x)
其中 L
是 objective 函数的值,G
是梯度向量,x
是我要优化的系数向量。
但是,当我包含另一个输入(即 [L,G]=objfun(x,M)
,其中 M
是一个矩阵)或当我在函数 objfun
中调用另一个函数时,代码不是 运行.
如何通过保持这种优化格式在 objfun
中包含任何输入并调用任何函数?
注意我调用优化如下:
[x ,fval] = fminunc(@objfun,x,options)
哪里
options = optimoptions(@fminunc,'Algorithm','quasinewton',...
'Display','iter','Gradobj','on','TolFun',10^-8)
有一篇关于 passing extra parameters 到 objective 函数的 mathworks 帮助文章:
您可以使用 @(...)
运算符为您的函数生成一个仅依赖于单个参数的匿名函数句柄。
a = 4; b = 2.1; c = 4;
f = @(x)objfun(x,a,b,c)
来自原始页面(您的 objfun
是 parameterfun
):
Note: The parameters passed in the anonymous function are those that exist at the time the anonymous function is created. Consider the
example
a = 4; b = 2.1; c = 4;
f = @(x)parameterfun(x,a,b,c)
Suppose you subsequently change, a to 3 and run
[x,fval] = fminunc(f,x0)
You get the same answer as before, since parameterfun uses a = 4, the
value when f was created.
To change the parameters that are passed to the function, renew the
anonymous function by reentering it:
a = 3;
f = @(x)parameterfun(x,a,b,c)
matlab中objective函数的常用写法(包括梯度向量)如下:
[L,G] = objfun(x)
其中 L
是 objective 函数的值,G
是梯度向量,x
是我要优化的系数向量。
但是,当我包含另一个输入(即 [L,G]=objfun(x,M)
,其中 M
是一个矩阵)或当我在函数 objfun
中调用另一个函数时,代码不是 运行.
如何通过保持这种优化格式在 objfun
中包含任何输入并调用任何函数?
注意我调用优化如下:
[x ,fval] = fminunc(@objfun,x,options)
哪里
options = optimoptions(@fminunc,'Algorithm','quasinewton',...
'Display','iter','Gradobj','on','TolFun',10^-8)
有一篇关于 passing extra parameters 到 objective 函数的 mathworks 帮助文章:
您可以使用 @(...)
运算符为您的函数生成一个仅依赖于单个参数的匿名函数句柄。
a = 4; b = 2.1; c = 4;
f = @(x)objfun(x,a,b,c)
来自原始页面(您的 objfun
是 parameterfun
):
Note: The parameters passed in the anonymous function are those that exist at the time the anonymous function is created. Consider the example
a = 4; b = 2.1; c = 4; f = @(x)parameterfun(x,a,b,c)
Suppose you subsequently change, a to 3 and run
[x,fval] = fminunc(f,x0)
You get the same answer as before, since parameterfun uses a = 4, the value when f was created.
To change the parameters that are passed to the function, renew the anonymous function by reentering it:
a = 3; f = @(x)parameterfun(x,a,b,c)