I am trying to solve a forced mass-spring-damper system in matlab by using the Runge-Kutta method. Currently the code uses constant values for system input but instead I would like to vectors as input. For examples, I would like to replace my force amplitude F0 with a vector value.
Should I be using for loops or what is the simplest way to do it?
function O = MSDSRK(m,b,k,F0,w,x0,v0)
% ----- Input argument -----
% m: mass for particle
% b: damping coefficient
% k: spring constant
% F0: amplitude of external force
% w: angular freuency of external force
% x0: initial condition for the position x(0)
% v0: initial condition for the velocity v(0)
dt=0.1;
options=odeset('InitialStep',dt,'MaxStep',dt);
td=[0:dt:50];
% Solve differential equation with Runge-Kutta solver
[t,x]=ode45(@(t,X)MSD(t,X,m,b,k,F0,w),td,[x0;v0],options);
% Extract only particle position trajectory
O=[t x(:,1)];
end
function dX=MSD(t,X,m,b,k,F0,w)
% With two 1st order diffeential equations,
% obtain the derivative of each variables
% (position and velocity of the particle)
dX(1,1)=X(2,1);
dX(2,1)=(1/m)*(F0*sin(w*t)-b*X(2,1)-k*X(1,1));
end