% plotDampedOscillator.m
% Plots the solutions returned by a damped oscillator function
% J.M.Soler. Jan.2011

% Set simulation parameters
clear, clf
w0 = 2*pi;                   % angular frequency (rad/s)
m = 1;                       % particle mass (kg)
b = 1;                       % friction coefficient (N*s/m)
x0 = 1;                      % initial position (m)
v0 = 0;                      % initial velocity (m/s)
tmax = 10;                   % maximum integration time (s)
dt = 0.1;                    % time interval (s)
c = b/(2*m);                 % dumping coefficient (s^-1)
w = sqrt(w0^2-c^2);          % damped angular frequency (rad/s)
xmax = sqrt(x0^2+(v0/w)^2);  % oscillator amplitude (m)

% Find trajectories
xExact  =  exactDampedOscillator( w, c, x0, v0, dt, tmax);

% Solve the oscillator equation by Runge-Kutta method
xRungeKutta = velocityRungeKutta1( @dampedSpring, m, x0, v0, dt, tmax );

% Plot trajectories
t = (0:dt:tmax);
plot( t,xExact, t,xRungeKutta,'o' )
legend('Exact','Runge-Kutta')
axis([0 tmax -1.2*xmax,1.2*xmax])
xlabel('time (s)'), ylabel('x(t) (m)')
