% springConstant2.m
% Finds the spring constants k1 and k2, such that |F|=k1*x+k2*x^2, 
% 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 for k1 and k2
cm = 1.e-2;      % one cm in m
x = [0.001 ; 0.011 ; 0.13 ; 0.30 ; 0.75]*cm;
X = [x,x.^2];

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

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

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