How to plot a maximum value on an existing line graph

조회 수: 91 (최근 30일)
Grace Rembold
Grace Rembold 2019년 11월 25일
댓글: Wakeel Mohammed 2021년 1월 22일
I am currently using assigned data to plot two three matrices as line graphs in a sublot, they are both 25x1 matrices.
X=Cell mass concentration
t= time (hours)
Ethanol= ethanol concentration
I also need to plot max(X) as a single point on this plot, but every time i try, it appears as a separate line (with a slope of 0) on the graph instead of marking the point.
here is my code and graph:
A=figure
x=[t];
y1=[Ethanol];
y2=[X]
subplot(1,2,1); plot(x,y1);
hold on
plot(x, max(Ethanol), 'r*');
%Labeling
title('Time vs. Cell Mass Concentration');
xlabel('Time (hours)');
ylabel('Ethanol (g/L)');
subplot(1,2,2); plot(x,y2);
%Labeling
title('Time vs. Ethanol');
xlabel('Time (hours)');
ylabel('Cell Mass Concentration (g/L)');
in the image you can see what happens as a currently try and plot the max value (subplot plot 1) and what the graph looks like without even attempting to plot the max value (subplot plot 2)
Problem 2 help matlab online.png

답변 (2개)

Ruger28
Ruger28 2019년 11월 25일
편집: Ruger28 2019년 11월 25일
Please use the code formatting.
A=figure
x=[t];
y1=[Ethanol];
y2=[X]
% Plot frst graph
subplot(1,2,1);
plot(x,y1);
hold on
% find max of Ethanol
[maxVal,maxIDX] = max(Ethanol);
% for single point plotting, use scatter
scatter(x(maxIDX), maxVal, 'r*'); % plot max of Ethanol with its corresponding x value
%Labeling
title('Time vs. Cell Mass Concentration');
xlabel('Time (hours)');
ylabel('Ethanol (g/L)');
subplot(1,2,2); plot(x,y2);
%Labeling
title('Time vs. Ethanol');
xlabel('Time (hours)');
ylabel('Cell Mass Concentration (g/L)');
  댓글 수: 2
Grace Rembold
Grace Rembold 2019년 11월 25일
Thank you so much, this worked wonderfully!
Wakeel Mohammed
Wakeel Mohammed 2021년 1월 22일
Thank you sooo much

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


David Goodmanson
David Goodmanson 2019년 11월 25일
Hi Grace,
Here is an example of what I think you are looking for. The idea is to use the second output of max to find the index in the x vector where the max occurs, and plot just that one point.
x = 0:.001:5;
y = 1+2.3*x-x.^2+.11*x.^3;
[maxy ind] = max(y)
plot(x,y,x(ind),y(ind),'o')
% or
% plot(x,y,x(ind),maxy,'o')
grid on

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by