How to solve a differential equation with a time-varying term?
17 views (last 30 days)
Show older comments
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 Comments
Sam Chak
on 22 Nov 2022
Hi @Julian
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.
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!