finding the minimum of a function input with a parameter
1 次查看(过去 30 天)
显示 更早的评论
%function to find recylce flowrate for range of conversions
%then trying to find minimum conversion where total_recylce_flowrate_5 <= 7000
function newmatrix = rootflow(conversion)
a = [-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ; 1 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0; 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0; 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 .6 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 .253 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 1 0; 0 0 0 .147 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 .16 0 0 -1 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 1 0 0 -1 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 1 0 0 0 0 0 -1 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 .84 0 0 0 0 0 -1 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 ; 0 0 0 0 0 0 0 0 0 0 0 0 .09 0 0 -1 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 .94 0 0 -1 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 .91 0 0 0 0 0 -1 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 .06 0 0 0 0 0 -1 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1];
b = zeros(21,1);
b(1,1) = -1360.777;
a5 = a; %new variable for a so old one isn't altered
newmatrix = zeros(1001,1); %creates matrix that I will add the values from the for loop
rowcounter = 1; %counts rows
for i = (conversion) %will go through all the conversion values
a5(7,4) = 1-i; %changes the values that are affected by the conversion
a5(8,4) = i*0.631;
a5(9,4) = i*0.368;
x5 = a5\b; %new solution matrix for each conversion value
total_recycle_flowrate_5 = 2.20462*(x5(19) + x5(20));%new recycle flowrate for each conversion value
newmatrix(rowcounter) = total_recycle_flowrate_5;%the new recycle flowrate is added to newmatrix
rowcounter = rowcounter + 1; %rowcounter moves to next row so recylce flowrate will be added to next row
end
end
newmatrix = rootflow(0:.001:1)
%trying to minimize conversions, not rootflow, but it won't let me? Also how to correctly use parameter (<=7000)?
fminsearch(rootflow(0:.001:1),.5,options)
options = optimset(total_recycle_flowrate_5,<=7000)
0 个评论
回答(3 个)
Sam Chak
2024-8-11
Hi @Olivia
I'm afraid I'm not entirely certain. Based on your description, it seems you aim to determine the value of the 'Conversion' variable between 0 and 1 such that the flow rate is less than 7000. However, your 'newmatrix' vector consistently outputs 1001 values. Therefore, I assume the first value in the vector represents the true flow rate.
format long
cv = linspace(0, 1, 1000001); % Conversion values
sol = zeros(numel(cv), 1);
for i = 1:numel(cv)
out = rootflow(cv(i));
sol(i) = out(1);
end
plot(cv, sol), grid on, hold on
idx = find(sol <= 7000); % find the positions in the solution that meet the condition
idx1 = idx(1); % 1st index value
best_cv = cv(idx1) % best cv up to 6 decimals of accuracy
out = rootflow(best_cv);
fr1 = out(1) % 1st value of recycle flowrate vector
plot(best_cv, fr1, 'p', 'markersize', 12)
yline(fr1, '--', 'Flowrate < 7000');
x = [best_cv, cv(idx1:end), 1];
y = [0, sol(idx1:end)', 0];
patch(x, y, 'yellow')
xlabel('Conversion values')
ylabel('Total Recycle Flowrate')
hold off
function newmatrix = rootflow(conversion)
a = [-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ; 1 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0; 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0; 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 .6 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 .253 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 1 0; 0 0 0 .147 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 .16 0 0 -1 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 1 0 0 -1 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 1 0 0 0 0 0 -1 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 .84 0 0 0 0 0 -1 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 ; 0 0 0 0 0 0 0 0 0 0 0 0 .09 0 0 -1 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 .94 0 0 -1 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 .91 0 0 0 0 0 -1 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 .06 0 0 0 0 0 -1 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1];
b = zeros(21,1);
b(1,1) = -1360.777;
a5 = a; %new variable for a so old one isn't altered
newmatrix = zeros(1001,1); %creates matrix that I will add the values from the for loop
rowcounter = 1; %counts rows
for i = (conversion) %will go through all the conversion values
a5(7,4) = 1-i; %changes the values that are affected by the conversion
a5(8,4) = i*0.631;
a5(9,4) = i*0.368;
x5 = a5\b; %new solution matrix for each conversion value
total_recycle_flowrate_5 = 2.20462*(x5(19) + x5(20));%new recycle flowrate for each conversion value
newmatrix(rowcounter) = total_recycle_flowrate_5;%the new recycle flowrate is added to newmatrix
rowcounter = rowcounter + 1; %rowcounter moves to next row so recylce flowrate will be added to next row
end
end
0 个评论
Image Analyst
2024-8-10
Are you simply trying to find the minimum value of newmatrix for that particular value of conversion? If so, why not just use min?
% Find min value and location of the newmatrix vector.
[minValue, indexOfMin] = min(newmatrix)
% Find value of conversion at that index
conversionValueAtMin = conversion(indexOfMin)
Star Strider
2024-8-10
If you want to constrain the optimisation, one approach sould be to use the Optimization Toolbox fmincon function.
Example —
%function to find recylce flowrate for range of conversions
%then trying to find minimum conversion where total_recylce_flowrate_5 <= 7000
B0 = rand % Initial Paraaemter Estimate
[B,fv] = fmincon(@(b)norm(rootflow(b)), B0, [],[],[],[],[],7E+3) % Set Upper Bound 'ub' at 7000
function newmatrix = rootflow(conversion)
a = [-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ; 1 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0; 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0; 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 .6 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 .253 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 1 0; 0 0 0 .147 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 .16 0 0 -1 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 1 0 0 -1 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 1 0 0 0 0 0 -1 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 .84 0 0 0 0 0 -1 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 ; 0 0 0 0 0 0 0 0 0 0 0 0 .09 0 0 -1 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 .94 0 0 -1 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 .91 0 0 0 0 0 -1 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 .06 0 0 0 0 0 -1 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1];
b = zeros(21,1);
b(1,1) = -1360.777;
a5 = a; %new variable for a so old one isn't altered
newmatrix = zeros(1001,1); %creates matrix that I will add the values from the for loop
rowcounter = 1; %counts rows
for i = (conversion) %will go through all the conversion values
a5(7,4) = 1-i; %changes the values that are affected by the conversion
a5(8,4) = i*0.631;
a5(9,4) = i*0.368;
x5 = a5\b; %new solution matrix for each conversion value
total_recycle_flowrate_5 = 2.20462*(x5(19) + x5(20));%new recycle flowrate for each conversion value
newmatrix(rowcounter) = total_recycle_flowrate_5;%the new recycle flowrate is added to newmatrix
rowcounter = rowcounter + 1; %rowcounter moves to next row so recylce flowrate will be added to next row
end
end
% newmatrix = rootflow(0:.001:1)
% %trying to minimize conversions, not rootflow, but it won't let me? Also how to correctly use parameter (<=7000)?
% fminsearch(rootflow(0:.001:1),.5,options)
% options = optimset(total_recycle_flowrate_5,<=7000)
.
1 个评论
Image Analyst
2024-8-11
Were the values of total_recycle_flowrate_5 always less than 5, or at least less than 5 for the final answer?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!