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

5 vues (au cours des 30 derniers jours)
Julian
Julian le 20 Nov 2022
Commenté : Julian le 22 Nov 2022
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 commentaires
Sam Chak
Sam Chak le 22 Nov 2022
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 le 22 Nov 2022
Yes, I calculated the values of Ki(t) using some values I obtained from another differential equation that is solved earlier in my code.

Connectez-vous pour commenter.

Réponse acceptée

Torsten
Torsten le 20 Nov 2022
Use "interp1" to interpolate the elements of K to the time t requested by the ODE integrator.
  5 commentaires
Torsten
Torsten le 22 Nov 2022
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 le 22 Nov 2022
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!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by