
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 = totalEnergy(lattice, J, Bz) %totalEnergy: Returns the averge 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 % J - (optional) coupling constant for spin/spin % interaction. Defaults to 1. % Bz - (optional) external magnetic field in +z % direction. Defaults to 0. % Output: % E - the total energy of the lattice % Use no external magnetic field for default if (nargin < 2) Bz = 0; end lsize = size(lattice); if (length(lsize) < 3) lsize(3) = 1; end E = 0; for x = 1:lsize(1) for y = 1:lsize(2) for z = 1:lsize(3) % Compute total energy E = E + 1/2*siteEnergy(lattice,[x y z],J,Bz); end end end return