Solve 2nd order ODE with discrete time terms

5 次查看(过去 30 天)
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?

采纳的回答

Paul
Paul 2014-2-24
编辑:Paul 2014-2-24
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).
  2 个评论
Fran
Fran 2014-2-25
Thanks Paul, I realized on this, but I didnt know how to do that. This solution works fine and that is what I needed, but just for curiosity, could it be possible to pass e(t) at each time step instead of to compute t1 and e1 on each iteration?
Thanks again for your answer
Paul
Paul 2014-2-25
编辑:Paul 2014-2-25
Yes, there are several solutions for this. You could parse t1 and e1 as extra arguments of the function file:
function dx = myFUN(t,x,t1,e1)
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
t1 = 0:1/1000:1 ;
e1 = 0:1000;
[t,y]=ode45(@myFUN,[0;1],[0 0],[],t1,e1)
The [] are needed because that's normally where the options of ode45 (see odeset) would normally be. If you use the default options you put [] there to indicate this.
You could also for example use global variables, however this is frowned upon:
function dx = myFUN(t,x)
global t1 e1
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
global t1 e1
t1 = 0:1/1000:1 ;
e1 = 0:1000;
[t,y]=ode45(@myFUN,[0;1],[0 0])

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by