I would like to write a code to find the minimum cost of running a dishwasher. This is dependent on the power required, hourly tariff rate, and time used. I am using fmincon
for this however the code provided below shows the following error message:
User supplied objective function must return a scalar value
My objective function is to minimize (Total Cost * Time) s.t total cost is equal to the summation of (hourly power)*(hourly cost) from hour 1 to 24 is equal to 0.8 kwh, also, the total cost must be greater than Ca
and the total run time for the day is one hour.
% Array showing the hourly electricity rates (cents per kwh)
R=zeros(24,1);
R(1:7,1)=6;
R(20:24,1)=6;
R(8:11,1)=9;
R(18:19,1)=9;
R(12:17,1)=13;
p_7 = transpose([0.8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]); %This is the power pattern of appliance (operates at 0.8 kWh for 1 hour daily)
for k=1:23
P7(:, k+1) = circshift(p_7,k); % This shows all the possible hours of operation
end
Total = P7*R; % This is the total cost per hour at different hourly tariffs
fun = @(x)Total.*(x);
x0 = [1];
A = Total;
%Ca = 0.5;
Ca = ones(1,24);
b = Ca;
Aeq = Total;
Daily_tot_7 = 2*ones(1,24);
beq = Daily_tot_7;
ub = 24;
lb = 1;
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
I believe that my understanding on converting constraints to fmincon
is not correct and that I may be missing vital constraints for this issue.
Your output is currently a vector of outputs. You stated that your cost function is the summation of hourly elements. Therefore, your function definition should be
However, if I'm reading into this right, you wish to solve for each hour individually. In that case, you need to set your x0 variable to be defined as a 24x1 input
If that is the case you need to adjust your A,b,Aeq, and beq variables accordingly. However, do you actually need these, you can just not use them by replacing them with
[]
Finally, your p7 variable is likely better redefined as
My apologies if I misunderstood what you are trying to accomplish here.