How to plot a maximum value on an existing line graph
조회 수: 76 (최근 30일)
이전 댓글 표시
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)
댓글 수: 0
답변 (2개)
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
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
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!