% nlspringmass.m - spring mass system with nonlinear damping
% RMM, 25 Aug 05
%
% This function gives the dynamics for a dampled oscillator with nonlinear
% damping.  The states of the system are
%
%   x(1)	position
%   x(2)	velocity
%
% The nonlinear damping is implemented as a change in the linear damping
% coefficient at a small velocity.  This is intended to roughly correspond 
% to some sort of stiction (and give an interesting phase portrait).  The 
% default parameters for the system are given by
%
%   m = 1	mass, kg
%   k = 1	spring constant, N/m
%   b1 = 1      damping constant near origin, N-sec/m
%   b2 = 0.01	damping constant away from origin, N-sec/m
%   dth = 0.5	threshold for switching between damping
%
% This corresponds to a fairly lightly damped oscillator away from the origin.

function dx=nlspringmass(t, x, flags)
m = 1; k = 1;
b1 = 2; b2 = 0.01; dth = 0.2;

% Compute the friction force
if (abs(x(2)) < dth)
  Fb = b1 * x(2);
elseif (x(2) < 0)
  Fb = -b1*dth + b2*(x(2)+dth);
else
  Fb = b1*dth + b2*(x(2)-dth);
end;

dx = [x(2); -k/m*x(1) - Fb/m];
