How to find the maximum value

조회 수: 3 (최근 30일)
sophp
sophp 2018년 2월 6일
댓글: Jan 2018년 2월 7일
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?

채택된 답변

Jan
Jan 2018년 2월 6일
편집: Jan 2018년 2월 6일
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
sophp
sophp 2018년 2월 7일
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 2018년 2월 7일
@sophp: By transposing one of the two vectors?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rik
Rik 2018년 2월 6일
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
sophp
sophp 2018년 2월 6일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by