clear all; clc; t1 = 1; % hopping parameter along x, y, z directions t2 = t1/sqrt(2); % hopping parameter along body diagonals a = 1; % lattice constant k = linspace(-pi/a, pi/a, 100); % wave vector N = length(k); % number of points in k-space % define the tight binding Hamiltonian H = zeros(N,N); for i=1:N-1 H(i,i+1) = -t1; H(i+1,i) = -t1; end H(1,N) = -t1; H(N,1) = -t1; for i=1:4:N-3 H(i,i+2) = -t2; H(i+2,i) = -t2; H(i+1,i+3) = -t2; H(i+3,i+1) = -t2; end for i=1:2:N-1 H(i,i+3) = -t2; H(i+3,i) = -t2; end % calculate the eigenvalues of the Hamiltonian E = eig(H); % plot the band structure plot(k, E, 'b'); hold on; plot(k, zeros(size(k)), 'k--'); xlabel('k'); ylabel('E'); title('Band structure');怎么检查k长度是否正确
时间: 2024-02-20 17:56:57 浏览: 61
ga_main_gamain_tic函数_遗传算法源码_AllClear_
可以使用MATLAB内置函数`length`检查向量的长度是否正确。在这个例子中,应该检查生成的$k$向量的长度是否为100,即:
```MATLAB
if length(k) == 100
disp('Length of k is correct.');
else
disp('Length of k is incorrect.');
end
```
上述代码会检查$k$向量的长度是否为100,如果长度正确,则输出"Length of k is correct.",否则输出"Length of k is incorrect."。可以将上述代码添加到原始代码中以检查$k$向量的长度是否正确。
阅读全文