Docsity

Plotting Spins in a Lattice: A MATLAB Function, Exercises of Computational Physics

Toggle sidebar
bg1
function plotLattice( lattice, spin )
%plotLattice Plot a lattice of spins
% Input:
% lattice - the lattice of spins
% spin - (optional) the spin of all particles in
% the lattice. Defaults to 1/2
% Use spin 1/2 for default
if (nargin < 1)
spin = 1/2;
end
% Get size of lattice
lsize = size(lattice);
if (length(lsize) > 3)
error('Lattice size limited to one, two, or three dimensions!');
end
if (length(lsize) < 3)
lsize(3) = 1;
end
% Number of Dimensions
dim = length(find(lsize > 1));
% Always plotting largest dimension along x, etc.
Szs = unique(lattice);
for sstep = 1:length(Szs);
Sz = Szs(sstep);
% Normalized spin
Sz_n = (Sz+spin)/(2*spin);
%points = find(lattice == Sz);
[xcoord, ycoord, zcoord] = ind2sub(lsize,find(lattice == Sz));
if (dim > 2)
plot3(xcoord-1,ycoord-1,zcoord-1,'.','Color',[Sz_n 0 1-Sz_n]);
hold on
else
plot(xcoord-1,ycoord-1,'.','Color',[Sz_n 0 1-Sz_n]);
hold on
end
end
if (dim <= 1)
axis([-1 lsize(1) -1 1])
elseif (dim == 2)
axis([-1 lsize(1) -1 lsize(2)])
else
axis([-1 lsize(1) -1 lsize(2) -1 lsize(2)])
end
hold off

Partial preview of the text

Download Plotting Spins in a Lattice: A MATLAB Function and more Exercises Computational Physics in PDF only on Docsity!

function plotLattice( lattice, spin ) %plotLattice Plot a lattice of spins % Input: % lattice - the lattice of spins % spin - (optional) the spin of all particles in % the lattice. Defaults to 1/ % Use spin 1/2 for default if (nargin < 1) spin = 1/2; end % Get size of lattice lsize = size(lattice); if (length(lsize) > 3) error('Lattice size limited to one, two, or three dimensions!'); end if (length(lsize) < 3) lsize(3) = 1; end % Number of Dimensions dim = length(find(lsize > 1)); % Always plotting largest dimension along x, etc. Szs = unique(lattice); for sstep = 1:length(Szs); Sz = Szs(sstep); % Normalized spin Sz_n = (Sz+spin)/(2*spin); %points = find(lattice == Sz); [xcoord, ycoord, zcoord] = ind2sub(lsize,find(lattice == Sz)); if (dim > 2) plot3(xcoord-1,ycoord-1,zcoord-1,'.','Color',[Sz_n 0 1-Sz_n]); hold on else plot(xcoord-1,ycoord-1,'.','Color',[Sz_n 0 1-Sz_n]); hold on end end if (dim <= 1) axis([-1 lsize(1) -1 1]) elseif (dim == 2) axis([-1 lsize(1) -1 lsize(2)]) else axis([-1 lsize(1) -1 lsize(2) -1 lsize(2)]) end hold off