clear
clc
close all

% demand to meet
P_d = 100; % demand to meet [MW] baseload role x_d

% size your system
size_ESS = 20000;
size_PV = 100:100:2000; % size of PV plant [MW] 
size_wind = 100:100:2000; % size of wind plant [MW]
duration = 10;
Emax = size_ESS / duration;

cost_StorageP = 1000;
cost_StorageE = 150;

% Objective function for fmincon
objective = @(x) computeCost(x, size_ESS, size_PV, size_wind, P_d, duration, cost_StorageP, cost_StorageE);

% Initial guess for fmincon

% Bounds for fmincon
lb = [min(size_PV), min(size_wind)];  % Lower bounds for PV and wind sizes
ub = [max(size_PV), max(size_wind)];  % Upper bounds for PV and wind sizes

x0 = [1000, 1000];  % Initial guess for PV and wind sizes

% Print initial guess
fprintf('Initial guess: PV=%.2f MW, Wind=%.2f MWn', x0(1), x0(2));

% Perform the optimization
options = optimoptions(@fmincon, 'Display', 'iter');
[x_min, cost_min] = fmincon(objective, x0, [], [], [], [], lb, ub, [], options);


fprintf('Optimal PV size: %.2f MWn', x_min(1));
fprintf('Optimal wind size: %.2f MWn', x_min(2));
fprintf('Minimum cost: %.2fn', cost_min);

% Function to compute the cost
function cost = computeCost(x, size_ESS, size_PV, size_wind, P_d, duration, cost_StorageP, cost_StorageE)
    size_PV_selected = x(1);
    size_wind_selected = x(2);
    % Print the selected sizes for debugging
    fprintf('Selected sizes: PV=%.2f MW, Wind=%.2f MWn', size_PV_selected, size_wind_selected);

    P_out = func_Pout(size_PV_selected, size_wind_selected, duration, size_ESS);
    i_nd = P_out < P_d;

    if any(i_nd)
        % If demand is not met, set a large penalty
        cost = Inf;
    else
        f_s = size_PV_selected / (size_PV_selected + size_wind_selected);
        cost = size_PV_selected * size_wind_selected * (f_s * 1000 + (1 - f_s) * 1500) + size_ESS * cost_StorageP + size_ESS * cost_StorageE;
    end
end

This code has error:
Error using barrier
Objective function is undefined at initial point. Fmincon cannot continue.

Error in fmincon (line 891)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, …

Error in baseloadFmincon (line 35)
[x_min, cost_min] = fmincon(objective, x0, [], [], [], [], lb, ub, [], options);

What is wrong with x0?

Khám phá các thẻ bài đăng