

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


function E = siteEnergy(lattice, site, J, Bz, Sz) %siteEnergy: Returns the energy of a single site in a % lattice based on spin-spin coupling with % nearest neighbors % Input: % lattice - a matrix representing the lattice of spins % containing the z component of those spins % site - lattice site [x,y,z] to compute energy % J - (optional) coupling constant for spin/spin % interaction. Defaults to 1. % Bz - (optional) external magnetic field in +z % direction. Defaults to 0. % Sz - (optional) Compute the energy if site has % specified spin with z component Sz. Defaults % to spin given in lattice for the site. % Output: % E - the energy of the site % Unpack the coordinates of site x = site(1); if (length(site) >= 2) y = site(2); else y = 1; end if (length(site) >= 3) z = site(3); else z = 1; end % Use J = 1 for default if (nargin < 3) J = 1; end % Use no external magnetic field for default if (nargin < 4) Bz = 0; end % Use lattice spin for default if (nargin < 5) Sz = lattice(x,y,z); end % Get lattice size nx = size(lattice, 1); ny = size(lattice, 2); nz = size(lattice, 3); % Interaction of field with spin E = -Bz.*Sz;
% Sum over nearest neighbors % If there are neighbors in the x direction if (nx > 1) neighbors = [x-1, x+1]; % Periodic boundary conditions neighbors = mod(neighbors-1,nx) + 1; neighborspins = lattice(neighbors,y,z); E = E - JSzsum(neighborspins); end % If there are neighbors in the y direction if (ny > 1) neighbors = [y-1, y+1]; % Periodic boundary conditions neighbors = mod(neighbors-1,ny) + 1; neighborspins = lattice(x,neighbors,z); E = E - JSzsum(neighborspins); end % If there are neighbors in the z direction if (nz > 1) neighbors = [z-1, z+1]; % Periodic boundary conditions neighbors = mod(neighbors-1,nz) + 1; neighborspins = lattice(x,y,neighbors); E = E - JSzsum(neighborspins); end return