% dX = getDerivs(t, X) % Function to get the state derivatives for the 16.07 spring-mass example % 16.07, Fall 2004 % Last updated September 7th, 2004 % % x' = F(x): "state derivatives are a function of the current state" % % t: current time given by the integrator % X: current state vector given by the integrator % dX: vector of state derivatives % % The equation that we are solving is: % % x_dot_dot + (k/m)x = (1/m)A sin(omega t) % % See springmass.m for a definition of the state vectors % % Parameters and inputs are hardcoded inside; while bad coding practice, it avoids % the whole "function calling function function with nested function" % nonsense that would otherwise be necessary % ( http://www.mathworks.com/access/helpdesk/help/techdoc/math/funfun18.html#942684 ) function dX = getDerivs(t, X) % system parameters k = 1; % spring constant [N/m] m = 1; % mass [kg] A = 1; % forcing amplitude [N] % unpack the state vector (follow the definition) x = X(1); x_dot = X(2); % set up some forcing parameters based on the current time if(t < 100) omega = .1; elseif(t < 150) omega = 1; else omega = 1 + .1*(t - 150); end; % Newton's 2nd: calculate the acceleration on the mass x_dot_dot = (1/m)*A*sin(omega*t) - (k/m)*x; % pack up the vector of state derivatives (follow the definition) dX = [ x_dot; % mass velocity [m/s] x_dot_dot ]; % mass accel [m/s^2]