How to plot a maximum value on an existing line graph

89 visualizaciones (últimos 30 días)
Grace Rembold
Grace Rembold el 25 de Nov. de 2019
Comentada: Wakeel Mohammed el 22 de En. de 2021
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

Respuestas (2)

Ruger28
Ruger28 el 25 de Nov. de 2019
Editada: Ruger28 el 25 de Nov. de 2019
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 comentarios
Grace Rembold
Grace Rembold el 25 de Nov. de 2019
Thank you so much, this worked wonderfully!
Wakeel Mohammed
Wakeel Mohammed el 22 de En. de 2021
Thank you sooo much

Iniciar sesión para comentar.


David Goodmanson
David Goodmanson el 25 de Nov. de 2019
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

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by