Generate an iteration to determine the characteristic polynomial of several matrices

3 vues (au cours des 30 derniers jours)
Hello, I need a code that through the for will iteratively give me the characteristic polynomial of the 7 matrices that I have, the problem is that I want them to be called one at a time for each iteration, that is, in iteration 1, in the function poly between matrix A1, on iteration 2 in the poly function between matrix A2, and so on. For this reason, it occurred to me to put poly (Ai) because the number of each iteration is the same as the matrix that I need it to enter, but obviously matlab thinks that I am asking for the polynomial of the variable Ai. What can I do to solve my problem? Thanks in advance.
A1=[1 2; 2 1], A2=[1 -2; 2 1], A3=[2 1; 0 2]
A4=[2 2; 2 2], A5=[1 1 0; 0 -2 1; 0 0 2]
A6=[2 1 0; 1 2 0; 0 0 -1], A7=[2 0 0; 1 2 0; 0 0 -1]
for i=1:7
poly(Ai)
end

Réponse acceptée

John D'Errico
John D'Errico le 12 Mar 2021
What can you do? Learn to use MATLAB properly.
Here, that means to learn to use matrices and arrays, especially cell arrays.
A={[1 2; 2 1], [1 -2; 2 1], [2 1; 0 2], [2 2; 2 2],...
[1 1 0; 0 -2 1; 0 0 2],[2 1 0; 1 2 0; 0 0 -1],[2 0 0; 1 2 0; 0 0 -1]};
A is a cell array. You index into A using curly braces. A has 7 elements.
for i=1:7
poly(A{i})
end
ans = 1×3
1 -2 -3
ans = 1×3
1.0000 -2.0000 5.0000
ans = 1×3
1 -4 4
ans = 1×3
1 -4 0
ans = 1×4
1 -1 -4 4
ans = 1×4
1 -3 -1 3
ans = 1×4
1 -3 0 4
  3 commentaires
John D'Errico
John D'Errico le 13 Mar 2021
I'm sorry. You want me to teach you to write some code that is far more poor, so that you can avoid having to learn MATLAB? In fact, no, you cannot simply create and index named arrays like that at all easily. And what you could do (code that I won't tell you how to write) would look far more nasty than simply creating a cell array.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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