How to plot a maximum value on an existing line graph

92 Ansichten (letzte 30 Tage)
Grace Rembold
Grace Rembold am 25 Nov. 2019
Kommentiert: Wakeel Mohammed am 22 Jan. 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

Antworten (2)

Ruger28
Ruger28 am 25 Nov. 2019
Bearbeitet: Ruger28 am 25 Nov. 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 Kommentare
Grace Rembold
Grace Rembold am 25 Nov. 2019
Thank you so much, this worked wonderfully!
Wakeel Mohammed
Wakeel Mohammed am 22 Jan. 2021
Thank you sooo much

Melden Sie sich an, um zu kommentieren.


David Goodmanson
David Goodmanson am 25 Nov. 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

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by