Generate an iteration to determine the characteristic polynomial of several matrices

조회 수: 3 (최근 30일)
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

채택된 답변

John D'Errico
John D'Errico 2021년 3월 12일
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
John D'Errico
John D'Errico 2021년 3월 13일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by