Finding maximum of a function using optimization toolbox

14 views (last 30 days)
function [p_uth,Eload] = threshold(ann_load)
for i = 1:size(ann_load,2)
p_uth(i) = 1.6; % upper threshold - temporarily assigned for now
pES_val = ann_load(:,i)-p_uth(i); % calculating load values above the upper thresold value
load_val = ann_load(:,i); % assigning load values of a day to load_val variable
x_p_uth = findX((1:size(ann_load,1))',load_val,p_uth(i)); % finding x-coordinates where upper threshold touches the load curve
y_temp = zeros(size(x_p_uth,1),1)*p_uth(i); % array of upper thresold values - to facilitate calculations
pES = pES_val(pES_val>0); % P_ES values
index_pES = find(pES_val>0); % index of P_ES values greater than 0
y_val = [y_temp;pES]; % uniting arrays of y values
x_val = [x_p_uth;index_pES]; % uniting arrays of x values
[x_sorted,sort_index] = sort(x_val); % sorting the x values array
y_sorted = y_val(sort_index); % sorting the y values array
Eload(i) = trapz(x_sorted,y_sorted)*5/12; % finding area under the curve of pES values
end
end
In the above I need to find the maximum of 'Eload', which is the objective function. As evident from the code, I am considering p_uth and pES as the variables. Hence Eload = f(p_uth,pES). I need to maximize Eload which can be done by varying p_uth, which needs to be varied between its upper and lower bounds. Eload and p_uth will be 1x366 array. The input to the function, ann_load, is a 288x366 array.
pES is calculated automatically based on the relation mentioned in the above code
pES_val = ann_load(:,i)-p_uth(i);
pES = pES_val(pES_val>0);
It can be noted that array size of pES varies in every iteration as the no. of positive values change in every iteration, based on the above equation.
Given the current conditions, I don't know how to use the optimization toolbox to find the maximum. I think I can solve it using nested loops in a scripted file instead of a function but I want the best possible solution using optimization toolbox. Does anybody have an insight as to how to solve it. Any help is appreciated.
  2 Comments
Matt J
Matt J on 16 Oct 2020
Edited: Matt J on 16 Oct 2020
In the above I need to find the maximum of 'Eload', which is the objective function....Eload and p_uth will be 1x366 array.
If Eload is vector-valued, would does it mean to "maximize" it? Are you solving a separate optimization problem for each i?
Satya Venkata Siddhardha Manchala
Yes. ann_load is a 288x366 array i.e. each of the 366 columns give a day's data. The data is provided in 5-minute intervals hence there are 288 readings. Eload should be a 1x366 array because Eload has the maximum value of each day for all 366 days.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 16 Oct 2020
Assuming you are solving a separate optimization for each i (see my comment above), it seems to me that the solution is very simply
p_uth=min(ann_load)
This will maximize the elements of pES_val and hence its integral.
  11 Comments
Matt J
Matt J on 20 Oct 2020
Any tips on how to get better at writing code?
Stay active in the Answers forum... maybe also view Mathworks webinars from time to time...
Satya Venkata Siddhardha Manchala
Thanks a lot for the info and for solving my problem. I'll take your advice and try to improve my coding skills.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!