%% ANIMATED VERSION: RUN this in Matlab (rather than publish)

%% Throwing a ball
% I throw a ball at time t=0 from the point (0,0,0) with initial velocity (1,0,1). Find the position function r(t). Find the time the ball hits the ground, and the distance from the starting point.

syms t real                   % declare t as real symbolic value
g = 9.8                       % acceleration from gravity
r0 = [0, 0, 0];               % initial position
v0 = [1, 0, 1];               % initial velocity
a = [0, 0, -g]                % acceleration vector using Newton's law
v = v0 + int(a,t,0,t)         % integrate for variable t from lower bound 0 to upper bound t
r = r0 + int(v,t,0,t)            
ts = solve(r(3),t)            % find t-values where z-component is zero: ts(1)=0, ts(2)=2/g
distance = subs(r(1),t,ts(2)) % substitute t=ts(2) in x-component r(1)

ezplot3(r(1),r(2),r(3),[0,double(ts(2))],'animate');
view(0,0); axis equal         % look at x,z plane

