How do I find a root in a nonlinear equation?

2 visualizaciones (últimos 30 días)
computing6
computing6 el 29 de Abr. de 2015
Comentada: Star Strider el 29 de Abr. de 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?

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Systems of Nonlinear Equations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by