Solve equation by fzeros
Show older comments
I'm solving the equation
by using fzero.
My code is:
fzero(@(x) x-4*sin(x), -10)
Result just has ONE root. How to show ALL roots of equation by using fzero ?!
Answers (2)
The fzero requires initial guess. Pick the initial value that is closest to the root:
fzero(@(x) x - 4*sin(x), pi)
and it will return another solution.
Can also try this Taylor series expansion method to "guess" the initial values:
syms x
fun = x - 4*sin(x);
T9 = taylor(fun, x, 'Order', 9)
fplot([fun T9])
grid on
xlabel('x')
ylabel('f(x)')
legend('x - 4*sin(x)', 'Taylor9', 'location', 'northwest')
p = sym2poly(T9);
g = roots(p);
g(imag(g) ~= 0) = [] % initial guesses of the approximated roots
r1 = fzero(@(x) x - 4*sin(x), g(1))
r2 = fzero(@(x) x - 4*sin(x), g(2))
r3 = fzero(@(x) x - 4*sin(x), g(3))
T9 = x^7/1260 - x^5/30 + (2*x^3)/3 - 3*x
g =
0
-2.4661
2.4661
r1 = 0
r2 = -2.4746
r3 = 2.4746

It is straightforward to find all the roots.
One approach —
N = 6;
x = linspace(-N, N);
y = x-4*sin(x);
zxi = find(diff(sign(y))); % Approximate Zero-Crossing Indices
for k = 1:numel(zxi)
x0(k) = fzero(@(x) x-4*sin(x), x(zxi(k))); % Calculate Exact Zero-Crossings
end
figure
plot(x, y)
hold on
plot(x0, zeros(size(x0)), 'rs')
hold off
grid
legend('$y(x) = x-4sin(x)$','$Roots$', 'Location','best', 'Interpreter','latex')
.
Categories
Find more on Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!