How to solve a differential equation with a time-varying term?

조회 수: 17 (최근 30일)
Julian
Julian 2022년 11월 20일
댓글: Julian 2022년 11월 22일
Hi all,
I'm trying to solve the following differential equation: x' = (A-B*K)*x, where A, B, and K are matrices. A and B are constant, but K is composed of different values (each corresponding to a different value in time).
This is the ode45 call I have:
[T,X] = ode45(@(t,x) sysfun(T,x,A,B,K,T),T,[1;0]);
Where T is my time vector I previously defined.
This is the sysfun function I defined:
function dx = sysfun(t,x,A,B,K,T)
dx = (A-B*K)*x;
end
My question is, how can I vary K? I'd like to be able to index it, but I don't understand how to do that. I saw some similar questions/answers but they didn't really help. I did find the following example from here
k = find(t >= timeserie,1);
k = max(1,k-1);
a = A(k);
However when I tried applying this to my code by doing the following, I just kept getting that k was always = 1 (instead of getting k =1, then k = 2, then k =3, etc).
function dx = sysfun(t,x,A,B,K,T)
k = find(t>=T)
k = max(1,k-1);
Kk = K(k,:);
dx = (A-B*Kk)*x;
end
Any help would be greatly appreciated. Thanks!
  댓글 수: 2
Sam Chak
Sam Chak 2022년 11월 22일
Do you have the time-varying functions of the elements in the matrix K?
It looks like the matrix K depends on some kind of a schedule.
Julian
Julian 2022년 11월 22일
Yes, I calculated the values of Ki(t) using some values I obtained from another differential equation that is solved earlier in my code.

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2022년 11월 20일
Use "interp1" to interpolate the elements of K to the time t requested by the ODE integrator.
  댓글 수: 5
Torsten
Torsten 2022년 11월 22일
K can be an array with its first dimension equal to the length of the T vector.
So the elements of K don't need to be interpolated separately.
Julian
Julian 2022년 11월 22일
Ohh, so
[T,X] = ode45(@(t,x) sysfun(T,x,A,B,K,T),T,[1;0]);
becomes
[T,X] = ode45(@(t,x) sysfun(T,x,A,B,interp1(T,K,t),T),T,[1;0]);
and everything works. Thank you so much for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by