% Unit 2 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%============
% CANNONBALL
%============
clear all, close all, clc


% constants
gEarth     = -9.79757; % m/s^2
gMoon      = -1.622;   % m/s^2
gy         = gEarth;
gx         = 0.0;

% input values
N     = 100;     % points
x0    = 3.;      % m
y0    = 2.;      % m
v0    = 10.;     % m/s (NOTE: total velocity!)
alpha = 23.;     % degrees

% inital velocity transformed to [vxy, vy0]
vx0   = v0*cos(alpha * 2*pi/360.);
vy0   = v0*sin(alpha * 2*pi/360.);

% time arrays (Note: Tend is the time until return to y=y0)
Tend  = -(2*vy0)/gy
t     = linspace(0,Tend,N);

% analytical cannonball solution
x  = x0  + vx0.*t + 0.5.*gx.*t.^2;
y  = y0  + vy0.*t + 0.5.*gy.*t.^2;

%vx = vx0 .* ones(1,N); % making explicit use of the knowledge that gx=0
vx = vx0 +  gx.*t;
vy = vy0 +  gy.*t;


% figure 1: colored trajectory
figure(1)
i = find(vy>=0);
j = find(vy<0);

plot(x(i), y(i),'bo'), xlabel('x'), ylabel('y')
hold on
plot(x(j), y(j),'rx')
hold off

