% function file for spring-mass-damper ode45 solution % function dydx = smdode45(time,variables) m = 1; % mass c = 1; % damping k = 6; % spring stiffness t1 = 2; % start time of square wave t2 = 14; % end time of square wave fsquare = 10; % square wave amplitude if (time >= t1) & ( time <= t2) % evaluate forcing function f = fsquare; % amplitude else f = 0; end x = variables(1); % the input variables v = variables(2); dydx(1) = v; % and their derivatives dydx(2) = (f - c*v - k*x)/m;