How to write Fmincon when I have two "to be optimized variables"?

I have two variables which I'm going to use Fmincon to optimize. Both of the variables are changing with time. I write Fmincon as below:
% Simulation time
Time = 0:0.005:25;
% Initial Guess for the control input at every time interval
TopS_FL0 = 0.2 .* ones(length(Time),1);
TopS_RL0 = 0.2 .* ones(length(Time),1);
% Bounds for control input at every time interval
TopS_FL_L = 0 .* ones(length(Time),1);
TopS_FL_U = 1 .* ones(length(Time),1);
TopS_RL_L = 0 .* ones(length(Time),1);
TopS_RL_U = 1 .* ones(length(Time),1);
[ Topt1, Topt2 ] = fmincon( @( Top_FL, Top_RL )obj( other parameters_1, Top_FL, Top_RL), [TopS_FL0, TopS_RL0], [], [], [], [], [TopS_FL_L, TopS_RL_L],[TopS_FL_U, TopS_RL_U],...
@( Top_FL, Top_RL )ctr( other parameters_2, Top_FL, Top_RL ), optNLP);
When I run my code, it always shows that:
Not enough input arguments.
Error in fmincon (line 534)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
Can anyone give me some help? Thank you very much.

 채택된 답변

Alan Weiss
Alan Weiss 2015년 3월 30일
As the documentation clearly states, fmincon expects your objective function to accept a vector of input variables, and output a scalar. So you need to do something like
fun = @(x)obj(x,other_parameters)
where fun is something like
function y = obj(x,other_parameters)
Top_FL = x(1);
Top_RL = x(2)
...
Alan Weiss
MATLAB mathematical toolbox documentation

댓글 수: 5

Thank you very much!
Dear Alan,
I have an optimization problem where my function has two control variables as the following
function [NetPV,finalSupply] = OptimizedRate (Weights,NRates)
Weights and NRates are vectors as: Weights = [x1 x2 x3] and NRates = [y1 y2 y3]
I want to optimized Weights and NRates not just passing one of them. I have seen some examples like: https://uk.mathworks.com/help/optim/ug/passing-extra-parameters.html
and tried them. However this pass NRates variable instead of optimizing it.
[x,fval,exitflag,output] = patternsearch(@(Weights)OptimizedRate(Weights,NRates),x0,[],[],Aeq,beq,LB,UB,options);
I'm looking for away to optimize Weights and NRates variables jointly.
Thanks for your help!
initial_weights = rand(1,3); %use something more meaningful
initial_nrates = rand(1,3); %use something more meaningful
x0 = [initial_weights, initial_nrates];
[x,fval,exitflag,output] = patternsearch( @OptimizedRate, x0, [], [], Aeq, beq, LB, UB, options);
with
function f = OptimizedRate(x)
Weights = x(1:3);
NRates = x(4:6);
...
Thanks Walter. How would you write the linear constraint in this case?
Example:
A = [1 1 -2 0 0 0;
0 0 0 0 1 -1]
b = [0;
0]
to express Weights(1) + Weights(2) <= 2 * Weights(3) & Nrates(2) <= Nrates(3)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

질문:

Tao
2015년 3월 30일

댓글:

2018년 1월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by