Calling external time dependent variables inside ODE45 function

5 次查看(过去 30 天)
I want to use ODE45 to solve a system of ODEs. The ODEs depend on time dependent variables that I have from another script. These time dependent variables are saved in the work space as variable. I want to load them (or use them somehow) inside the ODE45 function.
To make it clear, the variables that I want to use are names as (POS, VEL, ACC) and all of them are peicewise matrices (like peicewise functions but each has 3 rows).
One of them as an example:
piecewise(t in Dom::Interval([1], [2]), matrix([[3.0416666666666666666666666666667*t^5 - 20.208333333333333333333333333333*t^4 + 49.166666666666666666666666666667*t^3 - 53.333333333333333333333333333333*t^2 + 26.666666666666666666666666666667*t - 5.3333333333333333333333333333333], [0], [- 1.1458333333333333333333333333333*t^5 + 8.2291666666666666666666666666667*t^4 - 22.083333333333333333333333333333*t^3 + 26.666666666666666666666666666667*t^2 - 13.333333333333333333333333333333*t + 2.6666666666666666666666666666667]]), t in Dom::Interval([0], [1]), matrix([[- 2.2916666666666666666666666666667*t^5 + 6.4583333333333333333333333333333*t^4 - 4.1666666666666666666666666666667*t^3 - 0.0000000000000062172489379008766263723373413086], [0], [1.5208333333333333333333333333333*t^5 - 5.1041666666666666666666666666667*t^4 + 4.5833333333333333333333333333333*t^3 + 0.00000000000000088817841970012523233890533447266]]))
and VEL and ACC are similar with different values (coeffiecents) only.
The ODE45 function is like that:
function output=PID5DOF4(t,D)
x=D(1);
y=D(2);
z=D(3);
.
.
.
.
qld=POS;
qldotd=VEL;
qlddot=ACC;
.
.
.
xddot=A*qld+B*qldotd;
yddot=C*qldotd+D*qldotd;
zddot=E*qlddotd+F*qldotd;
output=[xddot yddot zddot]';
NOTE THAT,
POS, VEL, ACC are function of time, so we need to substitute the time (t) at each time step in the solution to find qld, qldotd, qlddotd.
  4 个评论
Hassan Alkomy
Hassan Alkomy 2020-5-10
I found that I need to factorize my matrix into vectors and then use interp1.
Thank you.
Hassan Alkomy
Hassan Alkomy 2020-5-10
Follow up,
interp1 also accepts matrices as input argument, so I do not need to factorize the matrix.
Actually your answer is an essential part of the solution. I would say that this is also an accepted answer.
Thank you again.

请先登录,再进行评论。

采纳的回答

Ameer Hamza
Ameer Hamza 2020-5-10
See these two examples of ode45 to learn how to deal with time-dependent parameters and load external variable inside the ode function

更多回答(0 个)

类别

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

Translated by