Solve equation by fzeros

조회 수: 4 (최근 30일)
Hoang Vu Huy
Hoang Vu Huy 2022년 4월 14일
답변: Star Strider 2022년 4월 14일
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 ?!

답변 (2개)

Sam Chak
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

Star Strider
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')
.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by