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

3 Ansichten (letzte 30 Tage)
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)

Antworten (1)

Cris LaPierre
Cris LaPierre am 6 Mai 2025
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 Kommentare
rezheen
rezheen am 9 Mai 2025

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

Walter Roberson
Walter Roberson am 9 Mai 2025
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.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by