Clear Filters
Clear Filters

How to find the maximum value

3 views (last 30 days)
sophp
sophp on 6 Feb 2018
Commented: Jan on 7 Feb 2018
Below is a 3D graph.
T=600:1:850;
t=0.2:0.1:20;
[tm,Tm] = meshgrid(t, T);
k1 = 10^7.*exp(-12700./Tm);
k2 = 5*10^4.*exp(-10800./Tm);
k3 = 7*10^7.*exp(-15000./Tm);
Y_B = (k1.*tm)./(((k2.*tm)+1).*(1+(tm.*(k1+k3))));
mesh(t,T,Y_B)
view(40, 45)
grid on
How do I find the maximum value of Y_B for t=0.2:0.1:20 over the range of T from 600 to 850 and plots on a double y-axis graph the value of T and Y_Bmax with t?

Accepted Answer

Jan
Jan on 6 Feb 2018
Edited: Jan on 6 Feb 2018
t = 0.2:0.1:20;
T = 600:50:850;
for i = 1:numel(T)
k1 = 1e7 .* exp(-12700 ./ T(i));
k2 = 5e4 .* exp(-10800 ./ T(i));
k3 = 7e7 .* exp(-15000 ./ T(i));
for j = 1:numel(t)
Y_B(i,j) = (k1.*t(j)) ./ (((k2.*t(j))+1).*(1+(t(j).*(k1+k3))));
% Prefer:
% Y_B(i,j) = k1 .* t(j) ./ ((k2 .* t(j) + 1) .* (1 + t(j) .* (k1 + k3)));
end
end
plot(t, max(Y_B, [], 1), '.-');
k1, k2, k3 are not changed inside the inner loop, so move them to the outer one. I would not overdo it with the parentheses.
By the way, you can even omit the loops:
t = 0.2:0.1:20;
T = (600:50:850).';
k1 = 1e7 .* exp(-12700 ./ T);
k2 = 5e4 .* exp(-10800 ./ T);
k3 = 7e7 .* exp(-15000 ./ T);
Y_B = k1 .* t ./ ((k2 .* t + 1) .* (1 + t .* (k1 + k3))); % >= R2016
plot(t, max(Y_B, [], 1), '.-');
This uses auto expanding, which was introduced in Matlab R2016b. Your meshgrid approach was fine also, while the loops suggested by Birdman have no advantage here.
  2 Comments
sophp
sophp on 7 Feb 2018
HI Jan thank you for this, it works perfectly! I am trying to plot a graph to show the relationship between residence time t and the operating temperature T(y_B max). However, one is row vector where as the other is a column vector. HOw do I do this?
Jan
Jan on 7 Feb 2018
@sophp: By transposing one of the two vectors?

Sign in to comment.

More Answers (1)

Rik
Rik on 6 Feb 2018
You can specify the dimension max should operate on, so you can insert your matrix. See the documentation for instructions.
For a double y-axis, see yyaxis, or search the File Exchange for one of many examples.
  1 Comment
sophp
sophp on 6 Feb 2018
Hi Rik,
I should have been more specific with my question. But, I am new to MATLAB and cannot find anything that tells me to find the maximum for each loop of i, form an array and plot a graph. This is the code I have
t = [0.2:0.1:20];
T = [600:50:850];
hold on;
for i=1:numel(T)
for j=1:numel(t)
k1(i) = 1e7 .* exp(-12700 ./ T(i));
k2(i) = 5e4 .* exp(-10800 ./ T(i));
k3(i) = 7e7 .* exp(-15000 ./ T(i));
Y_B(i,j) = (k1(i).*t(j))./(((k2(i).*t(j))+1).*(1+(t(j).*(k1(i)+k3(i)))));
end
[val,idt] = max(Y_B(i,j))
end
hold on
plot(t(idt),Y_B(idt))
However the correct graph does not appear.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!