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

1 次查看(过去 30 天)
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