-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagnetizationVSTempretureForExternalField.m
More file actions
61 lines (49 loc) · 2.06 KB
/
Copy pathMagnetizationVSTempretureForExternalField.m
File metadata and controls
61 lines (49 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
% Parameters
numSpins = 100; % Number of spins in the lattice
J = 1; % Interaction strength
numIterations = 1000; % Number of iterations for spin updates
numTemperatureSteps = 100; % Number of temperature steps
fields = [0, 0.5, 1]; % External magnetic field values
% Temperature range
T_min = 0.1;
T_max = 5;
temperatures = linspace(T_min, T_max, numTemperatureSteps);
% Initialize matrix to store magnetization values for different fields
magnetization = zeros(numTemperatureSteps, length(fields));
% Loop over each magnetic field value
for f = 1:length(fields)
h = fields(f); % Current magnetic field
% Monte Carlo simulation for each temperature
for t = 1:numTemperatureSteps
T = temperatures(t);
beta = 1 / T;
% Initialize spins randomly
spins = 2 * randi([0, 1], numSpins, 1) - 1;
% Simulate over a number of iterations
for iter = 1:numIterations
% Randomly select a spin
i = randi(numSpins);
% Calculate the energy change if this spin is flipped
deltaE = 2 * J * spins(i) * (spins(mod(i-2, numSpins) + 1) + spins(mod(i, numSpins) + 1)) + 2 * h * spins(i);
% Metropolis criterion
if deltaE < 0 || rand() < exp(-deltaE * beta)
spins(i) = -spins(i); % Flip the spin
end
end
% Calculate average magnetization at this temperature
magnetization(t, f) = mean(spins);
end
end
% Plotting the magnetization vs. temperature for different magnetic fields
figure;
hold on;
colors = {'b', 'r', 'g'}; % Colors for different magnetic field plots
for f = 1:length(fields)
plot(temperatures, magnetization(:, f), 'Color', colors{f}, 'LineWidth', 1.5, 'DisplayName', ['h = ' num2str(fields(f))]);
end
xlabel('Temperature (T)');
ylabel('Magnetization (M)');
title('Magnetization vs. Temperature for Different Magnetic Fields');
legend('show');
grid on;
hold off;