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

%======================================
% cannonball: finding initial velocity
%======================================
clear all
close all
clc

% gravitational constant (on earth)
g = -9.79757; % m/s^2

% time of flight and present position
T = 3;  % seconds
x = 18; % meter
y = 2;  % meter

% this is the actual system:
%    x = v0x*T+0.5*gx*T^2
%    y = v0y*T+0.5*gy*T^2
% generate matrix A and vector b from it:
a11 = T;
a12 = 0;
a21 = 0;
a22 = T;
A   = [a11, a12; a21, a22];


% x(t) = x0 + vx0*t + 0.5*gx*t^2
% y(t) = x0 + vy0*t + 0.5*gy*t^2
% (t=3; x=18; y=2; gx=0; gy=g)
b1  = x;
b2  = y-0.5*g*T.^2;
b   = [b1; b2];

% solve for initial velocities
v   = inv(A)*b  % m/s

% find shooting angle
angle = atan(v(2)./v(1))*180/pi