% This shows the output of the velocity and trajectory of coupled motion
% due to a fixed magnetic field.
%
% Motion in z-direction assumed zero.  Initial velocity in x and y only.
% Applied external field in z-direction.

%%
set(0,'DefaultAxesFontSize',24);
set(0,'DefaultLineLineWidth',3);


%% Velocities
omega=5; %This is the cyclotron frequency, qB/m
t=[0:.015:9]; % Time.
vox=10;
voy=1;
vx=vox*cos(omega.*t)+voy*sin(omega.*t);
vy=voy*cos(omega.*t)-vox*sin(omega.*t);

figure
subplot(2,1,1)
plot(t,vx)
xlabel('Time')
ylabel('X Velocity')
subplot(2,1,2)
plot(t,vy)
xlabel('Time')
ylabel('Y Velocity')


%% Velocity Movie
figure
plot(vx(1),vy(1),'o','MarkerSize',6,'Color',[1 1 1])
axis([-1.3*sqrt(vox.^2+voy.^2) 1.3*sqrt(vox.^2+voy.^2) -1.3*sqrt(vox.^2+voy.^2) 1.3*sqrt(vox.^2+voy.^2)])
grid on
hold on
garbage(1)=getframe;
xlabel('vx');
ylabel('vy');
for i=2:max(size(vx))
    plot(vx(i),vy(i),'o','MarkerSize',10,'Color',[1-(i/max(size(vx))).^2 1-i/max(size(vx)) 1-i/max(size(vx))])
    garbage(i)=getframe;
    hold on
end
clear garbage

%% Positions
x=cumsum(vx);
y=cumsum(vy);

figure
subplot(2,1,1)
plot(t,x)
xlabel('Time')
ylabel('X Position')
subplot(2,1,2)
plot(t,y)
xlabel('Time')
ylabel('Y Position')

%% Position Movie
figure
plot(x(1),y(1),'o','MarkerSize',6,'Color',[1 1 1])
grid on
hold on
garbage(1)=getframe;
xlabel('x');
ylabel('y');
for i=2:max(size(vx))
    plot(x(i),y(i),'o','MarkerSize',10,'Color',[1-(i/max(size(vx))).^2 1-i/max(size(vx)) 1-i/max(size(vx))])
    garbage(i)=getframe;
    hold on
end
clear garbage