% This is the solution to M219 2017's HW-1 #3.
%
% 2017.01.30 Patrick Magrath & DBE@UCLA

%% HW01 Problem #2A
% Define some constants
dt=1e-10;                % Time steps in seconds
gamma=42.57e6;            % Gyromagnetic ratio, [Hz/T]
B0=1.5;                    % External magnetic field strength [T]

N_Steps=1000;                % Number of time steps
M0=[1 0 0]';                % Initial condition [A.U.]
t_max=dt*N_Steps;            % Maximum time of simulation [s]
t=linspace(0,t_max,N_Steps); % Vector of time points [s]

M_H20=zeros(4,N_Steps);                % Initialize the magnetization vectors
M_H20(:,1)=[M0; 0];                    % Define the initial condition
dB0_H20=PAM_B0_op(gamma,B0,dt);        % Define the incremental precession

% Simulate precession
for n=2:N_Steps
  M_H20(:,n) = dB0_H20*M_H20(:,n-1);
end

% Plot the results - Note F/W differences are hard to see on short time scales...
figure; hold;
  plot(t,M_H20(1,:),'linewidth',2);
  plot(t,M_H20(2,:),'linewidth',2);
  plot(t,M_H20(3,:),'linewidth',2);
  %plot(t,M_H20(3,:),'linewidth',2);

  xlabel('Time [s]');
  ylabel('Magnetization [a.u.]');
  legend('Mx','My','Mz');
  title('Free Precession in the B0 Field')

  set(gcf,'Color','w');
  set(gca,'Color','w','XColor','k','YColor','k');
  set(gca,'Color','w','XColor','k','YColor','k','LineWidth',1.25,'Fontsize',11,'Fontweight','bold');
  set(get(gca,'Title'),'Color','k','FontSize',16);
  set(get(gca,'Xlabel'),'FontSize',14,'fontweight','bold');
  set(get(gca,'Ylabel'),'FontSize',14,'fontweight','bold');
  grid('on')
  print2desktop('/Users/pmagrath/desktop/','HW1_P3_D')