Clear Filters
Clear Filters

finding the minimum of a function input with a parameter

46 views (last 30 days)
Olivia
Olivia on 10 Aug 2024 at 15:17
Answered: Sam Chak on 11 Aug 2024 at 8:38
%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)

Answers (3)

Sam Chak
Sam Chak on 11 Aug 2024 at 8:38
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
best_cv =
0.233346000000000
out = rootflow(best_cv);
fr1 = out(1) % 1st value of recycle flowrate vector
fr1 =
6.999977518403889e+03
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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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

Image Analyst
Image Analyst on 10 Aug 2024 at 15:48
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)
  1 Comment
Olivia
Olivia on 10 Aug 2024 at 16:24
Edited: Olivia on 10 Aug 2024 at 16:25
i'm trying to minimize conversion without the recylce rate going over 7000:(

Sign in to comment.


Star Strider
Star Strider on 10 Aug 2024 at 16:39
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
B0 = 0.6605
[B,fv] = fmincon(@(b)norm(rootflow(b)), B0, [],[],[],[],[],7E+3) % Set Upper Bound 'ub' at 7000
Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
B = 1.0382
fv = 5.1573e-06
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 Comment
Image Analyst
Image Analyst on 11 Aug 2024 at 3:41
Were the values of total_recycle_flowrate_5 always less than 5, or at least less than 5 for the final answer?

Sign in to comment.

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!