Solve 2nd order ODE with discrete time terms
조회 수: 4 (최근 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?
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!