Getting all max, min values of a function as a set of coordinate points

조회 수: 1 (최근 30일)
rezheen
rezheen 2025년 5월 6일
댓글: Walter Roberson 2025년 5월 9일
My code only returns 1 set of points for max, min. There are more points based on the graph I provide. This is my code:
clear, clc
syms x y real
y=abs(x^3-9*x); fplot(y, [-5 5])
dy=diff(y); x_cps=solve(dy==0);
y_cps=subs(y,x_cps); y_max=(1); x_max=(1); y_min=(1); x_min=(1);
% % x_cps & y_cps are the critical points
for k=1:length(y_cps)
if y_cps(k)>y_max
y_max=y_cps(k);
x_max=x_cps(k);
elseif y_cps(k)<y_min
y_min=y_cps(k);
x_min=x_cps(k);
end
end
fprintf('(%s,%s)\n',x_max,y_max)
(3^(1/2),6*3^(1/2))
fprintf('(%s,%s)\n',x_min,y_min)
(-3,0)

답변 (1개)

Cris LaPierre
Cris LaPierre 2025년 5월 6일
I would use findpeaks
syms x y real
y=abs(x^3-9*x);
[X,Y] = fplot(y, [-5 5]);
Warning: Having two output arguments for fplot will be removed in a future release. Use the XData and YData properties instead.
[pk, loc] = findpeaks(Y,X);
[pkN, locN] = findpeaks(-Y,X);
plot(X,Y,loc,pk,'r*',locN,pkN,'m*')
  댓글 수: 9
rezheen
rezheen 2025년 5월 9일

@ Steven Lord: Can we find intervals of increasing & decreasing with the code you have? That'd be awesome.

Walter Roberson
Walter Roberson 2025년 5월 9일
No, you cannot find intervals of increasing and decreasing with the code posted by @Steven Lord -- not without stripping down to all but the first 5 lines and then adding notable additional code.

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by