% fitOneFall.m
% Jose M Soler. Nov.2010

% Set data (all in MKS system units)
clear all
m = 0.325;
x = [0.0  3.2  6.4  9.6  12.8 16.0]';  % notice: column vectors
t = [0.00 0.83 1.16 1.45 1.65 1.88]';

% Fit data using x = (g/2)t^2 - (cg/6m)t^3 = 
%                  = c2*t^2+c3*t^3 = [t^2,t^3]*[c2;c3] = T*C => C=T\x
T = [t.^2 t.^3];
C = T\x;
c2 = C(1);
c3 = C(2);
g = 2*c2;
c = -6*m*c3/g;
fprintf('g = %4.2f m/s^2 \n',g)
fprintf('c = %5.3f N*s/m \n',c)

% Find fit error
dx = sqrt( sum( (x - (c2*t.^2+c3*t.^3)).^2) / numel(x) );
fprintf('dx = %5.2f m \n',dx)

% Plot data and fit
time = [0:0.1:2];
plot(time,c2*time.^2+c3*time.^3,'-', t,x,'+')
xlabel('t (s)'), ylabel('x (m)')

% Get c assuming we know g
c2 = 9.8/2;
T = t.^3;
c3 = T\(x-c2*t.^2);
c = -6*m*c3/g;
fprintf('c = %5.3f N*s/m \n',c)
