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

 채택된 답변

KSSV
KSSV 2020년 10월 20일

1 개 추천

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')

추가 답변 (1개)

Alan Stevens
Alan Stevens 2020년 10월 20일

0 개 추천

If you meant you want to highight all the peaks and troughs then the following will do it (assuming you haven't got the findpeaks function available):
X = 0:0.01:10;
Y = (X.^(1.01))+4*cos((3/4).*X.*pi) - 2*sin((2/3)*pi.*X)-0.25;
% Peaks and troughs
% Gradient of curve
dYdX = 1.01*X.^0.01 - 3*pi*sin((3/4).*X.*pi) - (4/3)*pi*cos((2/3)*pi.*X);
signchange = diff(sign(dYdX)); % Where curve turns over
idxpos = signchange<0; % Peak positions
idxneg = signchange>0; % Trough positions
figure(); %creates figure window
plot(X,Y,'r',X(idxpos),Y(idxpos),'bo',X(idxneg),Y(idxneg),'bs'),grid
xlabel('Time (mins)') %x axis label
ylabel('Density Altitude (km)') %y axis label

댓글 수: 1

Steven Lord
Steven Lord 2020년 10월 20일
You could use the islocalmin and islocalmax functions included in MATLAB.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

질문:

2020년 10월 20일

댓글:

2020년 10월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by