Docsity

Total Energy Calculation in a Magnetic Lattice, Exercises of Computational Physics

Toggle sidebar
bg1
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

Partial preview of the text

Download Total Energy Calculation in a Magnetic Lattice and more Exercises Computational Physics in PDF only on Docsity!

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