How do I find a root in a nonlinear equation?

2 Ansichten (letzte 30 Tage)
computing6
computing6 am 29 Apr. 2015
Kommentiert: Star Strider am 29 Apr. 2015
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?

Antworten (1)

Star Strider
Star Strider am 29 Apr. 2015
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 Kommentare
Joseph Cheng
Joseph Cheng am 29 Apr. 2015
you'd want to find(yz<=0) or you'll miss when x==0
Star Strider
Star Strider am 29 Apr. 2015
True. The consecutive indices though provide enough information for interp1 to find the actual zeros.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Systems of Nonlinear Equations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by