0

I've been trying to make this code into a function and gather the wave number, but how do I get the frequency and then collect the data bt changing the wave number and then graphing. Below here is the code on matlab:

% 1D spin wave in a line: LLG + damping, periodic BC, arrows, live wavelength (FFT)
clear; clc; close all;

% ---- Parameters ----
N      = 100;       % number of spins along the line
a      = 0.5;      % lattice spacing (set physical units if you want)
J      = 2.0;      % exchange coupling
alpha  = 0.03;     % damping
dt     = 0.01;     % time step
T      = 50;        % total time
steps  = round(T/dt);

% ---- Initial spins: all up + localized transverse bump to launch a wave ----
S = zeros(N,3); S(:,3) = 1;
S(N,1) = 0.5;   % small tilt at center

% Positions (a straight line along x)
x = (0:N-1)*a; y = zeros(1,N); z = zeros(1,N);

% ---- Figure ----
figure('Color','w');
axis equal; axis([min(x)-a, max(x)+a, -1.2, 1.2, -1.2, 1.2]);
xlabel('X (sites)'); ylabel('Y'); zlabel('Z'); grid on; view([45 25]);

% ---- Time evolution ----
for t = 1:steps
    S_new = S;

    % Update all spins with periodic neighbors
    for i = 2:N-1
        H_eff = J * (S(2,:));
        precession = -cross(S(1,:), H_eff);
        damping_term = alpha * cross(S(1,:), precession);
        S_new(1,:) = S(1,:) + dt * (precession + damping_term);
        S_new(1,:) = S_new(1,:) / norm(S_new(1,:));
        
        % Update last spin (only affected by spin N-1)
        H_eff = J * (S(N-1,:));
        precession = -cross(S(N,:), H_eff);
        damping_term = alpha * cross(S(N,:), precession);
        S_new(N,:) = S(N,:) + dt * (precession + damping_term);
        S_new(N,:) = S_new(N,:) / norm(S_new(N,:));
        % Effective magnetic field (exchange with neighbors)
        H_eff = J * (S(i+1,:) + S(i-1,:));
        
        % LLG equation with damping
        precession = -cross(S(i,:), H_eff);
        damping_term = alpha * cross(S(i,:), precession);
        dS = precession + damping_term;

        % Euler update
        S_new(i,:) = S(i,:) + dt * dS;

        % Renormalize to unit vector
        S_new(i,:) = S_new(i,:) / norm(S_new(i,:));
    end
    S = S_new;

    % Draw every few steps
    if mod(t,5)==0
        clf;
        quiver3(x, y, z, S(:,1)', S(:,2)', S(:,3)', 0.8, 'LineWidth', 1.5);
        xlabel('X (sites)'); ylabel('Y'); zlabel('Z');
        axis equal; axis([min(x)-a, max(x)+a, -1.2, 1.2, -1.2, 1.2]); grid on; view([45 25]);

        % --- Measure wavelength from Sx along the line via spatial FFT ---
        [lambda, k_peak] = spinwave_wavelength_fft(S(:,1), a);

        title(sprintf('1D Spin Wave (periodic, \\alpha=%.2f)  t=%.2f,  k=%.3f rad/site,  \\lambda=%.2f sites', ...
              alpha, t*dt, k_peak*a, lambda/a));
        drawnow;
    end
end

% -------- Helper: wavelength from spatial FFT of Sx --------
function [lambda, k_peak] = spinwave_wavelength_fft(Sx, a)
    if nargin < 2, a = 1; end
    N  = numel(Sx);
    s  = Sx(:) - mean(Sx);             % de-mean to avoid DC dominating
    Y  = fftshift(fft(s));
    P  = abs(Y).^2;
    %plot(P);

    % k-grid (rad per physical length)
    kgrid = fftshift(2*pi/a * ((0:N-1) - floor(N/2)) / N);

    % use nonnegative k, ignore k=0
    mask = kgrid >= 0;
    kpos = kgrid(mask); Ppos = P(mask);
    if ~isempty(kpos) && kpos(1)==0
        kpos(1) = []; Ppos(1) = [];
    end

    if isempty(Ppos)
        k_peak = NaN; lambda = NaN; return;
    end

    [~, idx] = max(Ppos);
    k_peak = kpos(idx);          % rad per unit length
    lambda = 2*pi / k_peak;      % same length units as 'a'
end

I managed to visualize the spin wave and retreive the wave length and wave number, but I'm not sure about the frequency and graphing

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.