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

%======================
% particle trajectory
%======================
clear all
close all
clc

tmin = 1.8; tmax = 4.3; v0 = 1.5; A0 = -1; omega = 4.5; tau = 3.3; x0 = -7.5;

% time vector
N = 50;
t = linspace(tmin,tmax,N);


% anonymous function
%-------------------
vformula = @(t,v0,A0,omega,tau)(v0 + A0.*exp(-t./tau) .* (cos(omega.*t) -(omega.*t).*sin(omega.*t)));

% velocity plot
%-------------------
v = vformula(t, v0, A0, omega, tau);

figure(11)
plot(t,v)
xlabel('t [s]')
ylabel('v [m/s]')
legend('velocity')

% acceleration plot
%-----------------------
dv = diff(v);
dt = diff(t);
a  = dv./dt;

figure(12)
plot(t(1:N-1)+dt./2,a)
xlabel('t [s]')
ylabel('a [m/s^2]')
legend('acceleration')

% position plot
%-------------------
x = x0 + cumsum( (v(1:N-1)+v(2:N))./2 .* dt );

figure(13)
plot(t(2:N),x)
xlabel('t [s]')
ylabel('x [m]')
legend('x')

