%% Examples Friday, February 7

%% Moving around a circle with constant speed
% We move with speed V=2 around a circle of radius R=3 in clockwise direction, starting at point (3,0,0). Find the position function r(t), the velocity function v(t), the acceleration function a(t). Find the period T for one full circle. Plot the resulting curve together with the velocity and acceleration vectors for t=pi/3.

syms t real                   % declare t as real symbolic value
Pi = sym('pi');               % symbolic value pi
R = sym(3); V = sym(2);
b = -V/R;                     % need "-" for clockwise
r = R*[cos(b*t),sin(b*t),0]
v = diff(r,t)                 % differentiate with respect to t
a = diff(v,t)
T = 2*Pi/abs(b)               % period T for one full circle

ezplot3(r(1),r(2),r(3),[0,double(T)]); hold on
r0 = subs(r,t,Pi/3)           % substitute t=pi/3 in r
v0 = subs(v,t,Pi/3)
a0 = subs(a,t,Pi/3)
arrow3(r0,v0,'r'); texts(r0+v0,'v')
arrow3(r0,a0,'g'); texts(r0+a0,'a')
hold off; 
axis equal; view(0,90);       % look at x,y plane


%% Throwing a ball
% I throw a ball at time t=0 from the point (0,0) with initial velocity (1,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];                  % initial position
v0 = [1, 1];                  % initial velocity
a = [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(2),t)            % find t-values where y-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)

ezplot(r(1),r(2),[0,double(ts(2))]);
axis equal; grid on           % plot trajectory
