Finding maximum of a function using optimization toolbox
6 次查看(过去 30 天)
显示 更早的评论
Satya Venkata Siddhardha Manchala
2020-10-16
评论: Satya Venkata Siddhardha Manchala
2020-10-20
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 个评论
采纳的回答
Matt J
2020-10-16
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 个评论
Matt J
2020-10-20
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...
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!