How to plot a maximum value on an existing line graph
58 次查看(过去 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)
![Problem 2 help matlab online.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/249953/Problem%202%20help%20matlab%20online.png)
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)');
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!