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일)
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
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
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
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개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R13SP1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by