MATLAB: Constraint function in optimization toolbox

optimization

One of the variables in my model is Measured Power which is being calculated by an external black box model connected with MATLAB. The way its calculated is below:

Best Answer

I think that you missed the step where you call the external function to calculate the nonlinear constraint.
function [c,ceq] = con_EbsOptimize4(para)
global asmInfo app oc model objects Pr Hu Pmeasm Hmeasm Power Powermeasm;
model.Simulate(errors);
Power = oc.CastToComp11(objects.Item('Generator_1'));
Powermeasm = Power.QREAL.Value;
c = Powermeasm-192000;
ceq = [];
end
There are two other things that you might want to keep in mind for this:
  1. Often, the function that calculates the objective function is the same as the function that calculates the nonlinear constraint. If this is true for your function, consider using the technique in Objective and Nonlinear Constraints in the Same Function.
  2. If you are using fmincon as your solver, keep in mind the problems in finite differences mentioned in Optimizing a Simulation or Ordinary Differential Equation.
Alan Weiss
MATLAB mathematical toolbox documentation