How to save data from a loop as a vector?

Good Morning! I'm new to matlab and I have a question. When I do the loop described in the code below (where I am varying the magnetic field B), only the last data is saved. I would like to construct a vector with all the data of the eigenvalues ​​of the HT matrix, to construct a graph of the magnetic field as a function of the four eigenvalues ​​of Ht. I would like to build four vectors with the data of all the loops of the eigenvalues ​​to build a graph. Anyone who can help me I would be very grateful. Follow the program below.
clear
clc
d0=0.2; %meV delta 0
d1=0.18; %#delta 1
d2=0.05; %#delta 2
%#the electron and hole effective g factor in the z and x directions.
ghx=-0.35 ;
ghz=-2.2;
gex=-0.65;
gez=-0.8;
alpha=0.02; %#meV/T^2 diamagnetic shift coefficient,
ga=0.0 ;
gb=ga ; %# the interaction strength with the exciton state with same polarization.
theta=pi/3 ;
wx=0.01;
wd=wx-d0;
wa=wx+0.5;
wb=wa;
wl=wa;
G0a=0.05; %# is the maximum value of energy of the pulse
G0b=G0a;
tc=30 ; %# the time of the center of the pulse
tal=10 ; %#is the Gaussian rms width
mub=0.0579; %# Bohr magneton
for B=0:0.1:10
bp=mub*B.*sin(theta)*(gez+ghz)*0.5 ; %bp
bm=mub*B.*sin(theta)*(gez-ghz)*0.5; %bm
be=mub*B.*cos(theta)*gex*0.5; %be
bh=mub*B.*cos(theta)*ghx*0.5; %bh
H1=[wx,d1/2,0,0;d1/2,wx,0,0;0,0,wd,d2/2;0,0,d2/2,wd];
H2=[bp+alpha*B.^2,0,be,bh;0,-bp+alpha*B.^2,bh,be;be,bh,-bm+alpha*B.^2,0;bh,be,0,bm+alpha*B.^2];
HT=H1+H2;
[V,D] = eig(HT);
end

 Risposta accettata

There are different ways to do this.
This approach simply concatenates them as the third dimension of a (4x4xnumel(B)) array —
d0=0.2; %meV delta 0
d1=0.18; %#delta 1
d2=0.05; %#delta 2
%#the electron and hole effective g factor in the z and x directions.
ghx=-0.35 ;
ghz=-2.2;
gex=-0.65;
gez=-0.8;
alpha=0.02; %#meV/T^2 diamagnetic shift coefficient,
ga=0.0 ;
gb=ga ; %# the interaction strength with the exciton state with same polarization.
theta=pi/3 ;
wx=0.01;
wd=wx-d0;
wa=wx+0.5;
wb=wa;
wl=wa;
G0a=0.05; %# is the maximum value of energy of the pulse
G0b=G0a;
tc=30 ; %# the time of the center of the pulse
tal=10 ; %#is the Gaussian rms width
mub=0.0579; %# Bohr magneton
B=0:0.1:10;
V = NaN(4,4,numel(B)); % Preallocate
D = V; % Preallocate
for k = 1:numel(B)
bp=mub*B(k).*sin(theta)*(gez+ghz)*0.5 ; %bp
bm=mub*B(k).*sin(theta)*(gez-ghz)*0.5; %bm
be=mub*B(k).*cos(theta)*gex*0.5; %be
bh=mub*B(k).*cos(theta)*ghx*0.5; %bh
H1=[wx,d1/2,0,0;d1/2,wx,0,0;0,0,wd,d2/2;0,0,d2/2,wd];
H2=[bp+alpha*B(k).^2,0,be,bh;0,-bp+alpha*B(k).^2,bh,be;be,bh,-bm+alpha*B(k).^2,0;bh,be,0,bm+alpha*B(k).^2];
HT=H1+H2;
[V(:,:,k),D(:,:,k)] = eig(HT);
end
Another option would be to store them in cell arrays.
.

8 Commenti

Great! but now how do I get the vector to plot an eigenvalue as a function of the magnetic field? A vector of the type
E1 = [- 0.2150, -0.2156, -0.2134, -0.2043, -0.1868, -0.1608, -0.1264, -0.0843, -0.0350, 0.0210, 0.084, 0.084, 0.085, 0.084, 0.084, 0.084, 0.084, 0.084, 0.084, 0.084, 0.088 1.0597, 1.2167]
For example where B=0:0.5:10 and plot
plot(B,E1)
Thanks in advance for your help!
I am not certain what you want.
Try this —
d0=0.2; %meV delta 0
d1=0.18; %#delta 1
d2=0.05; %#delta 2
%#the electron and hole effective g factor in the z and x directions.
ghx=-0.35 ;
ghz=-2.2;
gex=-0.65;
gez=-0.8;
alpha=0.02; %#meV/T^2 diamagnetic shift coefficient,
ga=0.0 ;
gb=ga ; %# the interaction strength with the exciton state with same polarization.
theta=pi/3 ;
wx=0.01;
wd=wx-d0;
wa=wx+0.5;
wb=wa;
wl=wa;
G0a=0.05; %# is the maximum value of energy of the pulse
G0b=G0a;
tc=30 ; %# the time of the center of the pulse
tal=10 ; %#is the Gaussian rms width
mub=0.0579; %# Bohr magneton
B=0:0.1:10;
V = NaN(4,4,numel(B)); % Preallocate
D = V; % Preallocate
for k = 1:numel(B)
bp=mub*B(k).*sin(theta)*(gez+ghz)*0.5 ; %bp
bm=mub*B(k).*sin(theta)*(gez-ghz)*0.5; %bm
be=mub*B(k).*cos(theta)*gex*0.5; %be
bh=mub*B(k).*cos(theta)*ghx*0.5; %bh
H1=[wx,d1/2,0,0;d1/2,wx,0,0;0,0,wd,d2/2;0,0,d2/2,wd];
H2=[bp+alpha*B(k).^2,0,be,bh;0,-bp+alpha*B(k).^2,bh,be;be,bh,-bm+alpha*B(k).^2,0;bh,be,0,bm+alpha*B(k).^2];
HT=H1+H2;
[V(:,:,k),D(:,:,k)] = eig(HT);
end
for k = 1:size(D,3)
Dm(:,k) = diag(D(:,:,k));
end
figure
plot(B,Dm)
grid
xlabel('B')
ylabel('\lambda')
legend(compose('D(%d,%d)',repmat(1:4,2,1)'), 'Location','best')
This plots all the eigenvalues as funcitons of ‘B’.
.
It was exactly what I wanted. I appreciate your help!!!!
As always, my pleasure!
Celso Júnior
Celso Júnior il 16 Apr 2022
Modificato: Celso Júnior il 16 Apr 2022
Hi, could I ask one more favor, because I see that you are an expert, with I could make a vector now with the values of the eigenvectors, for example take each all the first columns of the eigenvectors of the l of the loop and build a graph in function of the magnetic field. So
plot(B,V(1,1)^2 , B , V(2,1)^2, B ,V(3,1)^2, B, V(4,1)^2))
and so on up to the fourth eigenvector.
Sure!
This plots the columns of each element of ‘V’
d0=0.2; %meV delta 0
d1=0.18; %#delta 1
d2=0.05; %#delta 2
%#the electron and hole effective g factor in the z and x directions.
ghx=-0.35 ;
ghz=-2.2;
gex=-0.65;
gez=-0.8;
alpha=0.02; %#meV/T^2 diamagnetic shift coefficient,
ga=0.0 ;
gb=ga ; %# the interaction strength with the exciton state with same polarization.
theta=pi/3 ;
wx=0.01;
wd=wx-d0;
wa=wx+0.5;
wb=wa;
wl=wa;
G0a=0.05; %# is the maximum value of energy of the pulse
G0b=G0a;
tc=30 ; %# the time of the center of the pulse
tal=10 ; %#is the Gaussian rms width
mub=0.0579; %# Bohr magneton
B=0:0.1:10;
V = NaN(4,4,numel(B)); % Preallocate
D = V; % Preallocate
for k = 1:numel(B)
bp=mub*B(k).*sin(theta)*(gez+ghz)*0.5 ; %bp
bm=mub*B(k).*sin(theta)*(gez-ghz)*0.5; %bm
be=mub*B(k).*cos(theta)*gex*0.5; %be
bh=mub*B(k).*cos(theta)*ghx*0.5; %bh
H1=[wx,d1/2,0,0;d1/2,wx,0,0;0,0,wd,d2/2;0,0,d2/2,wd];
H2=[bp+alpha*B(k).^2,0,be,bh;0,-bp+alpha*B(k).^2,bh,be;be,bh,-bm+alpha*B(k).^2,0;bh,be,0,bm+alpha*B(k).^2];
HT=H1+H2;
[V(:,:,k),D(:,:,k)] = eig(HT);
end
for k = 1:size(D,2)
Vm{k} = squeeze(V(:,k,:));
end
Vm
Vm = 1×4 cell array
{4×101 double} {4×101 double} {4×101 double} {4×101 double}
figure
for k = 1:size(D,2)
subplot(2,2,k)
plot(B,Vm{k})
grid
xlabel('B')
ylabel('Right Eigenvector')
title(sprintf('Column %d',k))
legend(compose('V(%d,%d)', [1:4;ones(1,4)*k]'), 'Location','best')
end
sgtitle('Eigenvectors')
This plots the each column of ‘V’ for every ‘V’. (It is easiest to use a cell array for this.)
.
Thanks a lot for the help! Only God to reward you!!!
As always, my pleasure!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by