Find roots of a first order differential equation

2 ビュー (過去 30 日間)
Elena Boschet
Elena Boschet 2021 年 10 月 21 日
コメント済み: Star Strider 2021 年 10 月 21 日
Hello
I have a created a function in matlab:
function [fx] = Function(x, r)
fx = r - 2*cos(x);
end
Now I would like to find the zero points of the function for r = 1 and x between - 2pi and 2pi.
Thank you!

採用された回答

Star Strider
Star Strider 2021 年 10 月 21 日
Try this —
r = 1;
x = linspace(-2*pi, 2*pi, 25);
y = Function(x,r);
zxidx = find(diff(sign(y))); % Indices Of Approximate Zero-Crossings
for k = 1:numel(zxidx)
x0(k) = fzero(@(x)Function(x,r),x(zxidx(k)));
end
x0(:)
ans = 4×1
-5.2360 -1.0472 1.0472 5.2360
figure
plot(x, y, '-b', x0,zeros(size(x0)),'or')
grid
function [fx] = Function(x, r)
fx = r - 2*cos(x);
end
.
  2 件のコメント
Elena Boschet
Elena Boschet 2021 年 10 月 21 日
Thank you so much, real MVP! :)
Star Strider
Star Strider 2021 年 10 月 21 日
As always, my pleasure!
Thank you!
.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by