How can I pass additional parameters to the constraint and objective functions in the Optimization Toolbox functions in versions prior to MATLAB 7.0 (R14) ?
1 回表示 (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2009 年 6 月 27 日
編集済み: MathWorks Support Team
2022 年 11 月 16 日
I would like to pass additional parameters to the nonlinear constraint function as well as objective function in my Optimization problem. I am using a MATLAB version prior to MATLAB 7.0 (R14) that did not support anonymous functions.
採用された回答
MathWorks Support Team
2009 年 6 月 27 日
If you are using a version of MATLAB prior to MATLAB 7.0 (R14), which did not have the ability to create anonymous functions, you will need to pass the additional parameters into the objective and constraint functions as demonstrated in the example below. We are using FMINCON for demonstration purposes.
[x,fval] = fmincon(@fun,x0,A,b,Aeq,beq,lb,ub,@nonlcon,options,P1,P2,...)
FMINCON passes the problem-dependent parameters P1, P2, etc., directly to the functions "fun" , "nonlcon" and any function in the options structure.
In this case a nonlinear constraint function could be of this form:
function [c, ceq] = nonlcon(x,P1,P2)
c = x(1)*P1 - x(2)*P2
...
...
ceq = [];
Note that P1 and P2 are also available to "fun" whether you use it or not. You can try this simple example:
The objective function "fun" is :
function f = fun(x,p1,p2)
f = -x(1) * x(2) * x(3)*p1;
The nonlinear constraint function "nonlcon" is :
function [c, ceq] = nonlcon(x,p1,p2)
% Define two inequality constraints which use p1 and p2
c(1) = x(1)*p1 + 2*x(2)*x(1)*p2 + 2*x(3) - p2;
c(2) = x(1)*x(2)-100;
% Define the equality constraints
ceq(1) = x(2) -x(1)*x(2);
ceq(2) = x(2) - x(1)*x(3);
The call to FMINCON used to minimize this expression is:
x0 = [10; 10; 10];
p1 = 1;
p2 = 72;
lb = [0 0 0];
ub = [ 50 50 50];
options = optimset('Largescale','off','Display','iter');
[x, fval] = fmincon(@fun, x0, [], [], [], [], lb, ub, @nonlcon, options, p1, p2)
The ODE functions use the same convention for handling additional parameters as is described here. Additional information about the ODE solvers can be found in the related solution below.
4 件のコメント
Walter Roberson
2022 年 1 月 11 日
We have not seen your code for FCVEC06January2022V2 . The error message would be consistent with the possibility that your function does not define any outputs, such as if you had
function FCVEC06January2022V2(a bunch of parameters here)
Walter Roberson
2022 年 1 月 12 日
I do not see anything obvious, but I do not have the full code to test with.
Put in a breakpoint at the line
x = fmincon(fun, x0, A, b, Aeq, beq, lb, ub, ncon, options);
and when you get there, execute
fun(x0)
and show us what the result is.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!