MATLAB: How to plot min and max values on graph

min and max plotting

I've created some formulas and a function that is composed of if and elseif statements and plots a graph of it. However I am needing for the graph to show the max and min of the curve and I am unsure of how to do so.
X = 0:0.1:10;
Y = (X.^(1.01))+4*cos((3/4).*X.*pi) - 2*sin((2/3)*pi.*X)-0.25;
figure(); %creates figure window
plot (Y,'-r+') %plots figure
xlabel('Time (mins)') %x axis label
ylabel('Density Altitude (km)') %y axis label
function minmax_i = min_max(Y) %function code
N = ones(1,7);
for i = 2:(length(Y)-1)
if (Y(i-1) > Y(i)) && (Y(i+1) > Y(i)) %if true, local minimum
minmax_i(N) = i;
N = N+1;
elseif (Y(i-1) < Y(i)) && (Y(i+1) < Y(i)) %if true, local maximum
minmax_i(N) = i;
N = N+1;
end
end
end

Best Answer

X = 0:0.1:10;
Y = (X.^(1.01))+4*cos((3/4).*X.*pi) - 2*sin((2/3)*pi.*X)-0.25;
% min
[val0,idx0] = min(Y) ;
% max
[val1,idx1] = max(Y) ;
plot(X,Y)
hold on
plot(X(idx0),Y(idx0),'*r')
plot(X(idx1),Y(idx1),'*b')
Related Question