% springConstant1.m
% Finds the spring constant k, such that F=-kx, given the data
%   F[N] | x[cm]
%   ------------
%   5    | 0.001
%   50   | 0.011
%   500  | 0.13
%   1000 | 0.30
%   2000 | 0.75
%   ------------
% J.M.Soler. Oct.2011

% Set the 'matrix' of coefficients
cm = 1.e-2;      % one cm in m
x = [0.001 ; 0.011 ; 0.13 ; 0.30 ; 0.75]*cm;

% Set the column vector of independent terms (absolute value)
F = [5 ; 50 ; 500 ; 1000 ; 2000];

% Find solution to x*k = F 
k = x\F


% Plot F(x) and its fit
xmesh = (0:1)*cm;
plot(x,F,'o', xmesh,k*xmesh)
xlabel('x (m)'), ylabel('F (N)')
