How to read the eigenvectors of a 4x4 matrix elements for a given range of variable elements B and N.
이전 댓글 표시
%Below is my code. I have a 4x4 matrix with variables B and N in some of the elements.
%My goal is to read the eigenvectors for each of the values in the range of the values
% for B and N
delta=45;
%create a 4x4 zeros matrix
mat=zeros(4,4);
%set values for corresponding entries of the matrix
mat(1,1)= delta;
mat(1,2)= 0 ;
mat(1,3)=0;
mat(2,1)=0 ;
mat(2,2)= delta;
mat(2,4)=0;
mat(3,1)=0;
mat(3,3)= -delta;
mat(3,4)= 0 ;
mat(4,2)=0;
mat(4,3)=0 ;
mat(4,4)= -delta;
for N = 0:1:5
for B = 0:0.001:10
mat(1,4)=sqrt(750*B*N);
mat(2,3)=sqrt(750*B*N);
mat(3,2)=sqrt(750*B*N);
mat(4,1)=sqrt(750*B*N);
eival=eig(mat);
[V,D]=eig(mat);
%read the eigenvalues of mat into the column vector of eigenvalues "eival" for the range of values of B and N
for m=1:4
if eival(m,1)>=0
eiv = eival(m,1);
break
else
eiv = eival(m,1);
end
end
end
end
답변 (1개)
Walter Roberson
2022년 11월 11일
편집: Walter Roberson
2022년 11월 11일
You should learn a common programming pattern.
Replace
for B = 0:0.001:10
with
Bvals = 0:0.001:10;
numB = length(Bvals); %you can use this size to preallocate arrays
then
for Bidx = 1 : numB
B = Bvals(Bidx);
%now compute stuff
eiv(m,Bidx,n+1) = appropriate value
end
The pattern here is to create the list of permitted values outside of the loop. Then loop over the indices of the elements. Each time pull out the one value that is relevant to you by using the list of values and the index. After your computation, store your result indexed by the loop index.
When you use this kind of pattern, it does not matter whether the values being looped over are positive integers, or whether they are regularly spaced or whether they are unique or sorted order. You separate the array indexing from the contents of the array, and that makes everything so much more flexible and robust.
댓글 수: 5
Walter Roberson
2022년 11월 11일
%Below is my code. I have a 4x4 matrix with variables B and N in some of the elements.
%My goal is to read the eigenvectors for each of the values in the range of the values
% for B and N
delta=45;
%create a 4x4 zeros matrix
mat=zeros(4,4);
%set values for corresponding entries of the matrix
mat(1,1)= delta;
mat(1,2)= 0 ;
mat(1,3)=0;
mat(2,1)=0 ;
mat(2,2)= delta;
mat(2,4)=0;
mat(3,1)=0;
mat(3,3)= -delta;
mat(3,4)= 0 ;
mat(4,2)=0;
mat(4,3)=0 ;
mat(4,4)= -delta;
Nvals = 0:1:5;
numN = length(Nvals);
Bvals = 0:0.001:10;
numB = length(B)
eiv = zeros(4, numN, numB);
for Nidx = 1:numN
N = Nvals(Nidx);
for Bidx = 1 : numB;
B = Bvals(Bidx);
mat(1,4)=sqrt(750*B*N);
mat(2,3)=sqrt(750*B*N);
mat(3,2)=sqrt(750*B*N);
mat(4,1)=sqrt(750*B*N);
eival=eig(mat);
[V,D]=eig(mat);
eiv(:, Nidx, Bidx) = nan;
%read the eigenvalues of mat into the column vector of eigenvalues "eival" for the range of values of B and N
for m=1:4
if eival(m,1)>=0
eiv(m, Nidx, Bidx) = eival(m,1);
break
else
eiv(m, Nidx, Bidx) = eival(m,1);
end
end
end
end
%eiv is now 4 x (numN) x (numB) and will contain NaN in entries that were
%skipped because of the break statement.
Question: are you certain that all of the eigenvalues are real-valued ? SInce you test them for >= 0 ?
Walter Roberson
2022년 11월 11일
If you know that the eigen values will have that structure, and you search for the first positive one, then why not just take abs() of the first one of them instead of doing the search ?
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!