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개)

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

Thanks
I can't seem to get this to work. Could you show fully how your lines of code incorporate in my code.
With my original code, I have been able to use a nested for loop to read the eigenvalues into a column vector and plotted them against B.
The challenge I'm faced with is how to read the eigenvectors for each of these eigenvalues
%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 ?
Yes they are all real-valued.
I test them for <=0 too in the continuation of the code. I do that to be able to separate the positive and negative eigenvalues. (i.e. the eigenvalues are symmetric. +/- of the same value)
If you use symbolic math to solve it, you will find the eigenvalues to be +/- sqrt(delta^2 +750BN). Since B and N are both >=0 the eigenvalues must be real-valued. I solved it manually and found same. I obtained the eigenvectors symbolically too, but I want them numerically for each BN value
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 ?
Thanks! That should work too. But right now I'm over the eigenvalues. I'm interested in the eigenvectors.
FYI: your code as it stands now returns this
eival =
-45
-45
45
45
which is a special case for only when N or B or both equals 0 and the 750*B*N term drops out.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2022년 11월 11일

편집:

2022년 11월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by