How do I mark all critical points on a graph of a function?

9 ビュー (過去 30 日間)
Lincoln
Lincoln 2024 年 2 月 18 日
コメント済み: Matt J 2024 年 2 月 18 日
I'm trying to plot the function f=(sin(x)+(1/3)*(cos(5*x))) across 0<=x<=2pi, find all roots of the derivative and then use them to mark all the critical points on the graph. The following code is as far as I've got:
syms x;
f=(sin(x)+(1/3)*(cos(5*x)));
v=diff(f)
fplot(f,[0 10])
hold on
fplot(v,[0 10])
roots(v)
I've tried the solve command too but cannot find a way to list all roots, plug the roots into the original function, then mark the resultant critical points on the graph.
Any advice appreciated, thanks.

採用された回答

Matt J
Matt J 2024 年 2 月 18 日
編集済み: Matt J 2024 年 2 月 18 日
syms x;
f=(sin(x)+(1/3)*(cos(5*x)));
v=matlabFunction( diff(f) );
f=matlabFunction(f);
x=linspace(0,2*pi,1e5);
loc=find(gradient(sign(v(x)))~=0);
xcrit=mean(reshape(x(loc),2,[])); %critical point x-coordinates
fplot(f,[0 2*pi])
hold on
plot(xcrit,f(xcrit),'rx')
hold off;
legend('f(x)', 'crit points','Location','NE'); axis padded
  2 件のコメント
Lincoln
Lincoln 2024 年 2 月 18 日
Wow thanks! Is there a way I can find the roots of a derivative function and store them as an array?
Matt J
Matt J 2024 年 2 月 18 日
xcrit is that array.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by