"roots" function not working

5 visualizzazioni (ultimi 30 giorni)
Kyle
Kyle il 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?

Risposta accettata

Matt Fig
Matt Fig il 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 Commenti
Kyle
Kyle il 2 Mar 2011
This was my problem. Oops. Thank you very much.
Anowarul Azim Evan
Anowarul Azim Evan il 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by