In: Physics
At each position in a 3D grid lies an atom with a positive or negative spin, specified by values +1 and -1, respectively. The spin value at each position is assigned randomly based on whether a randomly generated number is greater than or less than 0.5. I need to plot a 3D visualization of this grid using MATLAB in which positive spin locations are represented by markers of one color and negative spin locations are another color.
I have written the corresponding matlab script below,
If you want to change the marker color, size or shape visit the fallowing link,
https://in.mathworks.com/help/matlab/ref/plot3.html
-----------------------------------------------------------------------------------------
xn = 3; % length of lattice in x-direction
yn = 3; % length of lattice in y direction
zn = 3; % length of lattice in y direction
%%
%Section 1
figure(1)
% plotting lattice points
ms = 500; % Marker size
for i = 1:xn;
for j = 1:yn;
for k = 1:zn;
rn = rand;
if rn < 0.5;
scatter3(i,j,k,ms,'o','r')
grid off
else
scatter3(i,j,k,ms,'o','b')
grid off
end
hold on
end
end
end
%%
% Section 2
% If you want to show the 3D grid lines, other wise just run the
1st
% section
for i = 1:xn;
for j = 1:yn;
xg = [];
for xm =1:xn
xg(xm)=i;
end
yg = [];
for ym =1:yn
yg(ym)=j;
end
plot3(xg,yg,1:zn,'k--','linewidth',1)
hold on
end
end
for i = 1:xn;
for j = 1:zn;
xg = [];
for xm =1:xn
xg(xm)=i;
end
zg = [];
for zm =1:zn
zg(zm)=j;
end
plot3(xg,1:yn,zg,'k--','linewidth',1)
hold on
end
end
for i = 1:yn;
for j = 1:zn;
zg = [];
for zm =1:zn
zg(zm)=j;
end
yg = [];
for ym =1:yn
yg(ym)=i;
end
plot3(1:xn,yg,zg,'k--','linewidth',1)
hold on
end
end