% msd_finite.m
%
% Used to plot the solution of a mass-spring-damper system using
% a finite difference approach.
%
% Gary P. Scavone
% CCRMA, Stanford University, 2001.

%%% Simulation Parameters %%%

fs = 44100;      % sample rate
T = 1/fs;        % sample period
N = 1000;        % iterations

%%% System Parameters %%%

m = 0.5;          % mass in kg
k = 5000000;      % spring constant (N/m)
w0 = sqrt(k/m);   % natural angular frequency of system
f0 = w0/(2*pi);   % natural frequency of system (no damping)
R = 200;          % damping constant

%%% Equation Constants %%%

c1 = 1 + R*T/m + k*(T^2)/m;
c2 = 2 + R*T/m;

%%% Displacement Initialization %%%
x = zeros(1,N);
xm1 = 0.01;      % x[n-1]
xm2 = xm1;       % x[n-2]

for a = 1:N,
  
  x(a) = c2/c1*xm1 - xm2/c1;
  xm2 = xm1;
  xm1 = x(a);
  
end
  
plot(x)
grid
xlabel('Time Steps (samples)');
ylabel('Mass Displacement (m)');



