"roots" function not working

5 vues (au cours des 30 derniers jours)
Kyle
Kyle le 1 Mar 2011
Hi,
I need to use the roots function to find the roots of a polynomial. I copied the text from the example @ the MathWorks.com entry for this function:
p = [1 -6 -72 -27];
r = roots(p);
However, r does not return, and the following error appears:
??? Subscript indices must either be real positive integers or logicals.
I cannot believe that Matlab cannot execute this simple code that is an official example. Does someone have any insight as to what the problem may be?

Réponse acceptée

Matt Fig
Matt Fig le 1 Mar 2011
You have a variable named roots. Do this :
clear roots
p = [1 -6 -72 -27];
r = roots(p);
Don't name variables after built-in MATLAB functions. (And especially don't blame MATLAB for your silly mistakes ;-))
  3 commentaires
Kyle
Kyle le 2 Mar 2011
This was my problem. Oops. Thank you very much.
Anowarul Azim Evan
Anowarul Azim Evan le 18 Mar 2021
rt=[1 1 1 1 1]; % all roots are 1 like (x-1)*(x-1)*(x-1)*(x-1)*(x-1)
p=poly(rt) % right-most is the constant value then x coefficient, then x^2 and so on...
roots(p)
%% roots
1.0008 + 0.0006i
1.0008 - 0.0006i
0.9997 + 0.0009i
0.9997 - 0.0009i
0.9990 + 0.0000i
%% Is it any bug? I was supposed to get 5 ones as roots.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by