Find roots of a first order differential equation
1 view (last 30 days)
Show older comments
Elena Boschet
on 21 Oct 2021
Commented: Star Strider
on 21 Oct 2021
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!
0 Comments
Accepted Answer
Star Strider
on 21 Oct 2021
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(:)
figure
plot(x, y, '-b', x0,zeros(size(x0)),'or')
grid
function [fx] = Function(x, r)
fx = r - 2*cos(x);
end
.
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!