How to index matrices inside a loop?

2 views (last 30 days)
Hey all,
I'm trying to index a matrix on witch it's elements vary inside a loop. The code aims to evaluate the time evolution operator in a 2-level quantum system, therefore I need to be able to identify the matrix in a specific time interation, i.e., I need to know the matrix values at U(1), U(20000) and so on. I apologize if it's a simple matter, I'm a self-taught MATLAB user and haven't found any awnsers using the search engine. Thanks in advance!
Here is the code
%% Quantum Control
format long
clear all
clc
%% Variables
for ft = -20000:.1:20000 % Time
alpha = 0.001; % Transition parameter
beta = 0.001; % Transition parameter
ai = 0.2; % Initial population |1>
ai2 = 0.8; % Initial population |2>
af = 1; % Final population |1>
w0 = 0.02; % w0 = (E2 - E1)/h
mi = 6; % dipole operator
phii = pi/24; % initial relative phase
phif = pi/4; % final relative phase
E1 = 1.98; % eigen-energy 1
E2 = 2; % eigen-energy 2
H = [0 1;1 0]; % Interation hamiltonian
H0 = [E1 0;0 E2]; % hamiltonian
%% Control function
g = 1./(1+exp(-alpha.*ft));
f = ai*(1-g)+af*g;
p = 1./(1+exp(-beta.*ft));
h = phii*(1-p)+phif*p;
%% Electric field
E = alpha.*(af-ai).*exp(alpha.*ft).*sin(w0.*ft+h)./(mi.*(1+exp(alpha.*ft)).*sqrt((1-ai+(1-af).*exp(alpha.*ft)).*(ai+af.*exp(alpha.*ft))))+2.*beta.*(phif-phii).*exp(beta.*ft).*sqrt(f.*(1-f)).*cos(w0.*ft+h)/(mi.*(1-2.*f).*(1+exp(beta.*ft).^2));
%% Time evolution operator
[V, D]=eig(H);
Z = expm(H0);
Z1 = expm(D);
U = exp(-1i*ft/2)*Z*exp(1i*mi*E*ft)*V*Z1*inv(V)*exp(-1i*ft/2)*Z
end
%% End

Accepted Answer

infinity
infinity on 9 Jul 2019
Edited: infinity on 9 Jul 2019
Hello,
You can change your code a bit like this
%% Quantum Control
format long
clear all
clc
%% Variables
time = -20000:.1:20000; % Time
n = length(time);
U = zeros(1,n);
for i = 1:n % Time
ft = time(i);
alpha = 0.001; % Transition parameter
beta = 0.001; % Transition parameter
ai = 0.2; % Initial population |1>
ai2 = 0.8; % Initial population |2>
af = 1; % Final population |1>
w0 = 0.02; % w0 = (E2 - E1)/h
mi = 6; % dipole operator
phii = pi/24; % initial relative phase
phif = pi/4; % final relative phase
E1 = 1.98; % eigen-energy 1
E2 = 2; % eigen-energy 2
H = [0 1;1 0]; % Interation hamiltonian
H0 = [E1 0;0 E2]; % hamiltonian
%% Control function
g = 1./(1+exp(-alpha.*ft));
f = ai*(1-g)+af*g;
p = 1./(1+exp(-beta.*ft));
h = phii*(1-p)+phif*p;
%% Electric field
E = alpha.*(af-ai).*exp(alpha.*ft).*sin(w0.*ft+h)./(mi.*(1+exp(alpha.*ft)).*sqrt((1-ai+(1-af).*exp(alpha.*ft)).*(ai+af.*exp(alpha.*ft))))+2.*beta.*(phif-phii).*exp(beta.*ft).*sqrt(f.*(1-f)).*cos(w0.*ft+h)/(mi.*(1-2.*f).*(1+exp(beta.*ft).^2));
%% Time evolution operator
[V, D]=eig(H);
Z = expm(H0);
Z1 = expm(D);
U(i) = exp(-1i*ft/2)*Z*exp(1i*mi*E*ft)*V*Z1*inv(V)*exp(-1i*ft/2)*Z
end
%% End
Now, U will be a vector.
  3 Comments
infinity
infinity on 9 Jul 2019
Hello,
The problem is that "exp(-1i*ft/2)*Z*exp(1i*mi*E*ft)*V*Z1*inv(V)*exp(-1i*ft/2)*Z"
is a matrix of 2x2. Therefore, we need to change the type of U to cell and store the result to this.
What you can do is to change
U = zeros(1,n);
become
U = cell(1,n);
and
U(i) = exp(-1i*ft/2)*Z*exp(1i*mi*E*ft)*V*Z1*inv(V)*exp(-1i*ft/2)*Z
become
U{i} = exp(-1i*ft/2)*Z*exp(1i*mi*E*ft)*V*Z1*inv(V)*exp(-1i*ft/2)*Z;
guilherme stahlberg
guilherme stahlberg on 9 Jul 2019
It solved! Thanks a lot, good sir!

Sign in to comment.

More Answers (0)

Categories

Find more on Quantum Mechanics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!