ODE with input a symbolic matrix

8 views (last 30 days)
Gabriele Galli
Gabriele Galli on 8 Aug 2020
Commented: Walter Roberson on 11 Aug 2020
Hello All,
I wrote the following function using syms variables and its output (i.e. Sxd) is a system of 12 equations contained inside a symbolic matrix.
function Sxd=S1(t, X)
syms x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x1d x2d x3d x4d x5d x6d x7d x8d x9d x10d x11d x12d x1d x2d k1 k2 k3 k4 V0 u dfdx dfdth Sx Sxd
x1=X(1);
x2=X(2);
x3=X(3);
x4=X(4);
x5=X(5);
x6=X(6);
x7=X(7);
x8=X(8);
x9=X(9);
x10=X(10);
x11=X(11);
x12=X(12);
x1d=k1*x2;
x2d=(u-x2)/(k2+k3*x1);
dfdx=[diff(x1d, x1) diff(x1d, x2); diff(x2d, x1) diff(x2d, x2)];
dfdth=[diff(x1d, k1) diff(x1d, k2) diff(x1d, k3) diff(x1d, k4) diff(x1d, V0) 0;
diff(x2d, k1) diff(x2d, k2) diff(x2d, k3) diff(x2d, k4) diff(x2d, V0) 0];
Sx = [x3 x5 x7 x9 x11 x1; x4 x6 x8 x10 x12 x2];
Sxd=dfdx*Sx+dfdth
end
Now I would like to pass this matrix to an ODE solver to solve it over time.
Actually, I am passing to the solver (ODE45):
[t1, sol1] = ode45('S1', tspan1, IC1);
where S1 is the above fn, tspan1 is the time span and IC1 is a vector containing the 12 IC (which are all 0s).
By the way, it doesn't work. I think this is due to the output format of the function (i.e. a symbolic matrix)
How can I solve this issue?
Thank you so much in advance,
Gabri
  1 Comment
Star Strider
Star Strider on 8 Aug 2020
It is not going to work anyway in the form you have it.
To begin with, the first assignment after the syms call must be:
X = sym('X',[1 12]);
Then, it must evaluate that returns a column vector, as a column vector of first-degree differential equations.
When you have that, you can use matlabFunction to create an anonymous function that you can use in the MATLAB numeric differential equation integration functions.
I have no idea what you want to do, so I cannot even begin to put it in the correct form. You must do that.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 8 Aug 2020
If you
syms t
syms X [1 12]
and then apply the body of your function, then Sxd will come out as a 2 x 6 array. However, the output from the ode function must be a column vector.
Symbolic calculations are inefficient. Let's see if we can create an appropriate numeric function:
>> matlabFunction(Sxd(:), 'vars', {t, X})
Error using sym/matlabFunction>checkVarsSubset (line 236)
Free variables 'k1,k2,k3,u' must be included in 'Vars' value.
The answer is NO. Your calculation includes the symbolic variables k1, k2, k3, and u, none of which are defined and none of which are parameters to the function. Your output is inherently symbolic, and that is not permitted for the ode*() series of function.
  3 Comments
madhan ravi
madhan ravi on 11 Aug 2020
k1 = 1; % an example
k2 = 2;
k3 = 3;
Walter Roberson
Walter Roberson on 11 Aug 2020
If your k1, k2, k3, and u were all numeric, then you could use the commands shown in the first example in the documentation for odeFunction in order to create an anonymous function that could be passed to ode45 or similar.
However, as things stand, those are symbolic variables, not numeric, and none of the ode*() routines can handle that.
You could potentially use dsolve() to get a symbolic solution.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!