Cant store values in a for loop becuase index isnt positive or iterative

1 ビュー (過去 30 日間)
Hello,
I am doing a simulation of quantum tunneling where I change the height of a barrier between 2 wells (V2). I want to change the height from 0 to -1 with a for loop. My output is the minmax function where I get a max of the graph and a local minimum with respect to the potential given.
My code works, but I cant figure out a way to store the min and max or each given V2. I know im overlooking something simple.
close all;clc;clear;
L=600; %length
num = 2048; %number of points; fortran code has 2049
dx = L/(num-1); %distance between points
%express physical constants in atomic units
hbar = 1;
m = 1;
a = -hbar*hbar/(2*m*dx*dx);
%define parameters of wells
distance=10; %distance between wells (only works in certain numbners divisible by 2)
width=10 ; %width of well; fortran code has as float type
V0= -1.0; %undoped well
V1= -1.0; %doped well
minmax2=[];
for V2=0.0:-0.01:-.03; %height of barrier btwn 2 wells
V = 0; %placeholder for now, will be used in matrix to set doping
minmax=[];
%create matrix to represent potential
U= zeros(1,num);
number_of_wells = 2;
offset=floor(num/2-number_of_wells*width/2-(number_of_wells-1)*distance/2);
doped_wells =zeros(1,number_of_wells);%matrix defines which wells are doped
%wells can be selectively doped by setting corresponding matrix element
%to any non-zero value
doped_wells(1) = 0;
for well = 1:number_of_wells
if doped_wells(well) == 0
V = V0;
else V = V1;
end
for i =0:width-1
U(offset + distance*(well-1/2) + (well-1)*width +i) = V;
%U(offset - distance*(well-1/2) - (well-1)*width -i) = V0;
%fortan code has negatively numbered wells
end
end
for i =0:width-1
U(offset + distance*(well-3/2) + (well-1)*width +i) = V2; %defining the height of barrier
end
%create matrix M to represent finite differences method
M = zeros(num,num);
M(1,1) = -2*a + U(1);
M(1,2) = a;
for i=2:num-1
M(i,i-1) = a;
M(i,i) = -2*a + U(i);
M(i,i+1) = a;
end
M(num,num-1)= a;
M(num,num) = -2*a + U(num);
%diagonalize M and normalize results
[evectors,evalues] = eig(M);
N=zeros(1,num); %store normalized square of eigenvectors of matrix M
start = 1; %this and next line allow choice of range of eigenvectors to
%include in matrix of normalized eigenvectors
max0 = 1;
for j = start:max0
for k=1:num
N(k,j) = evectors(k,j)*conj(evectors(k,j))/dx;
%normalized square of wavevector for l-th state
end
end
%plot(evalues) %used to examine eigenvalues, not pertinent to paper
Uscaled = zeros(1,num);
scale = 5;
for i = 1:num
Uscaled(i) = U(i)/scale;
end
plot(Uscaled);
N=N(:,1);
hold on
plot(N)
title("2 well")
axis ([1000 1060 -.25 .25])
N=N;
minimum=findpeaks(-N);
min2=min(minimum);
maximum=findpeaks(N);
min2=-min2;
max2=max(maximum);
% minmax=[min2,max2]
minmax=[min2,max2,V2]
end
minmax = 1×3
0.0062 0.2141 0
minmax = 1×3
0.0064 0.2139 -0.0100
minmax = 1×3
0.0066 0.2137 -0.0200
minmax = 1×3
0.0068 0.2134 -0.0300

採用された回答

Image Analyst
Image Analyst 2022 年 11 月 28 日
Use an array to store all the V2 in advance then set V2 inside the loop. Use the iterator to go over all values of V2 and put the things you want to save into the k'th location of the corresponding array
allV2 = 0.0 : -0.01 : -.03; % Height of barrier between 2 wells
numV2 = numel(allV2)
minValues = zeros(1, numV2); % Preallocate
maxValues = zeros(1, numV2);
for k = 1 : numV2 % For every height (for every V2):
V2 = allV2(k);
% code...
% Store min and max in the appropriate vector arrays.
minValues(k) = whatever
maxvalues(k) = whatever
end

その他の回答 (0 件)

カテゴリ

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!

Translated by