How do I find a root in a nonlinear equation?

2 ビュー (過去 30 日間)
computing6
computing6 2015 年 4 月 29 日
コメント済み: Star Strider 2015 年 4 月 29 日
my equation is
function y=f(x)
x = (-20:0.05:20); %-20<x<20
y=5*(x^2)*(sin(x))
I already have the graph of this function, but what would I write in the script (m file) to solve this equation for the root and have it plot on the same graph? I tried fzero, but I keep getting an error message back, so maybe I'm using it incorrectly?

回答 (1 件)

Star Strider
Star Strider 2015 年 4 月 29 日
One option:
x = (-20:0.05:20); %-20<x<20
y=5*(x.^2).*(sin(x));
yz = y.*circshift(y, [0 -1]);
yzi = find(yz < 0);
for k1 = 1:size(yzi,2)-1
xzeros(k1) = interp1(y(yzi(k1):yzi(k1)+1),x(yzi(k1):yzi(k1)+1),0);
end
figure(1)
plot(x, y, '-m')
hold on
plot(xzeros, zeros(size(xzeros)), 'pb')
hold off
grid
The routine finds the indices near the zero-crossings by multiplying the y-values with a one-index circularly-shifted version of itself and then finds the values where these products are negative. It then uses interp1 and the intervals defined by these indices to find the actual values of the zeros, producing the vector ‘xzeros’ that are the x-coordinates of the zeros. It then plots them.
  2 件のコメント
Joseph Cheng
Joseph Cheng 2015 年 4 月 29 日
you'd want to find(yz<=0) or you'll miss when x==0
Star Strider
Star Strider 2015 年 4 月 29 日
True. The consecutive indices though provide enough information for interp1 to find the actual zeros.

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

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by