![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/964120/image.png)
Solve equation by fzeros
1 次查看(过去 30 天)
显示 更早的评论
I'm solving the equation
by using fzero.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/964020/image.png)
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 ?!
0 个评论
回答(2 个)
Sam Chak
2022-4-14
编辑:Sam Chak
2022-4-14
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/964120/image.png)
0 个评论
Star Strider
2022-4-14
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')
.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!