How do I find a root in a nonlinear equation?

2 vues (au cours des 30 derniers jours)
computing6
computing6 le 29 Avr 2015
Commenté : Star Strider le 29 Avr 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?

Réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Systems of Nonlinear Equations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by