How to save data from a loop as a vector?

12 ビュー (過去 30 日間)
Celso Júnior
Celso Júnior 2022 年 4 月 16 日
コメント済み: Star Strider 2022 年 4 月 16 日
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

採用された回答

Star Strider
Star Strider 2022 年 4 月 16 日
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 件のコメント
Celso Júnior
Celso Júnior 2022 年 4 月 16 日
Thanks a lot for the help! Only God to reward you!!!
Star Strider
Star Strider 2022 年 4 月 16 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by