"fmincon" optimization with nonlinear constraints: colon error
Mostrar comentarios más antiguos
Hello!
This is my second post for the same topic (solar production with an hourly resolution over 15 years), but I have a different question this time. Thanks in advance for the help! Here's the issue:
I have an objective function that I can solve with fmincon (I think), only one nonlinear equality constraint and variable bounds. My variable is Cpv and it will be a single number, not an array.
Function = @ObjFun3; %Saved in the same directory with filename=function name
nonlinconst = @PVnonlincon; %Saved in the same directory with filename=function name
[Cpv,fval] = fmincon(Function,10000,[],[],[],[],0,10000,nonlinconst);
If I eliminate the nonlincon arguement, fmincon finds a solution (which is obviously not sufficient, I just wanted to check where the problem is). The error I am getting is:
Input arguments to function include colon operator. To input the colon
character, use ':' instead.
Error in fmincon (line 651)
initVals.nceq = ceqtmp(:);
Error in Opt3 (line 29)
[Cpv,fval] = fmincon(Function,10000,[],[],[],[],0,10000,nonlinconst);
Do I have a conceptual error somewhere, or a command that I cannot use when declaring a function, or an argument that fmincon doesn't accept?
ObjFun3 is defined as:
function [OF] = ObjFun3(Cpv)
DataW = struct2array(load('DataNinjaW+PV.mat','DataW')); %8760x15x6 array
DataPV = struct2array(load('DataNinjaW+PV.mat','DataPV')); %8760x15x6 array
Cn = 10000;
r = size(DataW,1);
Pw = DataW(r,1,1);
N = Cn .* ones(size(Pw));
NinjaPV = DataPV(r,1,Loc);
function F = Fed_pv(Cpv)
F = min((NinjaPV.*Cpv/1000), (N-Pw));
end
OF = sum(N - Pw - Fed_pv(Cpv), 'all');
end
And PVnonlincon is defined as:
function [c_ineq,c_eq] = PVnonlincon(Cpv)
DataW = struct2array(load('DataNinjaW+PV.mat','DataW')); %8760x15x6 array
DataPV = struct2array(load('DataNinjaW+PV.mat','DataPV')); %8760x15x6 array
Cn = 10000;
r = size(DataW,1);
Pw = DataW(r,1,1);
N = Cn .* ones(size(Pw));
NinjaPV = DataPV(r,1,Loc);
c_ineq = [ ];
Anonymous = @(Cpv) Fed_Pv(Cpv) + NotFed(Cpv) - NinjaPV*(Cpv);
c_eq = Anonymous;
function NF = NotFed(Cpv)
NF = max(0,(NinjaPV*Cpv/1000)+Pw-N);
end
function F = Fed_Pv(Cpv)
F = min((NinjaPV*Cpv/1000), (N-Pw));
end
end
1 comentario
My variable is Cpv and it will be a single number, not an array.
If Cpv is a scalar, then fmincon is overkill. You should really just use fminbnd.
Also, I think there must be a mistake in your nonlinear constraints. Your equality constraint function "Anonymous" is piecewise linear in Cpv, which means it can only have a finite number of roots (at most 3 in this case) unless the slope of some piece is zero over some interval. But that doesn't look possible here unless you really meant,
Anonymous = @(Cpv) Fed_Pv(Cpv) + NotFed(Cpv) - NinjaPV*(Cpv)/1000;
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Get Started with Optimization Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!