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

3 ビュー (過去 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 件のコメント
Stephen23
Stephen23 2025 年 5 月 6 日
What toolboxes do you have available to use ?

サインインしてコメントする。

回答 (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 Center および File ExchangeAnnotations についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by