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:
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.
Your objective function shouldn't be myconstr. Your objective should really be norm(confun(a)).^2
Also, you don't really have any nonlinear inequalities, or at least all of your inequalities looks quite linear to me. So you should use matrix data to express them and your call to fmincon should really look like,
x = fmincon(@(a) norm(confun(a)).^2,a0, A,B,[],[],[],[],[],opts)
Best Answer