MATLAB: Solve 2nd order ODE with discrete time terms

differential equationsode45

I have a second order diferential equation:
k1 * d2a + k2 * da + a = e
where a and e are functions of t. I have e(t) in a matrix DATA where:
t = DATA(:,1)
e = DATA(:,2)
If I define the function to be used in ode45 solver as:
function dx = myFUN(t,x)
dx = zeros(2,1);
dx(1) = x(2);
dx(2) = 1/k1*(-k2*x(2)-x(1)+ e(t));
end
How can I pass the value of e(t) on each time step?

Best Answer

Assuming some values for the constants, t1 is your t = DATA(:,1) and e1 is your e = DATA(:,2):
function dx = myFUN(t,x)
t1 = 0:1/1000:1 ;
e1 = 0:1000;
e_int=interp1(t1,e1,t);
dx = zeros(2,1);
k1=1;k2=2;
dx(1) = x(2);
dx(2) = 1/k1*(-k2*x(2)-x(1)+ e_int);
end
Because t during solving probably won't exactly be one of the t values in your data, you have to interpolate to estimate e at t (e_int).
Related Question