# ToDiscreteTimeModel fails following video

Following the systems-modeling video here

at about 15 min, 20 seconds in, the presenter (Bob Sanhedrick, I think) shows a model for an "Asteroids-game"-like 2D spacecraft.

The model is trivial:

dynamics = {m x''[t] == u1[t], m y''[t] == u2[t]}


or, in state-space form:

spacecraft = StateSpaceModel[
dynamics,
{x[t], y[t]},
{u1[t], u2[t]},
{x[t], y[t]},
t]

{{{0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 0}},
{{0, 0}, {1/m, 0}, {0, 0}, {0, 1/m}},
{{1, 0, 0, 0}, {0, 0, 1, 0}},
{{0, 0}, {0, 0}}}


The next step is converting it to a discrete-time model (15:52 in the video):

spacecraftD = ToDiscreteTimeModel[spacecraft, 1, Method -> "ZeroOrderHold"]


This fails (even in the downloaded copy of the video's notebook -- it's not my typo):

During evaluation of In[3]:= General::ivar: 0 is not a valid variable. >>
During evaluation of In[3]:= General::ivar: 0 is not a valid variable. >>
StateSpaceModel[{
{{1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 1}, {0, 0, 0, 1}},
{{1/(2 m), 0}, {1/m, 0}, {0, 1/(2 m)}, {0, 1/m}},
{{1, 0, 0, 0}, {0, 0, 1, 0}}, {{0, 0}, {0, 0}},
Automatic},
$Failed, {\[FormalK]}, SamplingPeriod -> 1]  Fortunately, I can copy out the failed model explicitly, leaving out the Automatic, and reproduce the rest of the demonstration verbatim: The question is "why did this fail, and what can I do to fix it." I read the documentation for TransferFunctionModel and tried a number of hacks, to no avail. I'd like to know this so that as I go to more advanced models I don't get stuck with something that doesn't have so obvious a workaround. • works with Mma9 – Phab Commented Sep 2, 2014 at 8:08 ## 2 Answers This looks like a bug. May be you can send report to [email protected]. I do not why it fails. Hard to debug using Trace. But it should work as is. I verified it using Maple symbolic control system functions and here is the result. I took Mathematica's A,B,C,D that came up from the state space, used them in Maple to create a state space, then converted it to discrete using same sample rate and zoh method, and it worked. Then also obtained the transfer functions (2, since 2 inputs/outputs) in z domain. All works with no problem:  dynamics = {m x''[t] == u1[t], m y''[t] == u2[t]} sys=StateSpaceModel[dynamics, {x[t], y[t]}, {u1[t], u2[t]}, {x[t], y[t]}, t]   ToDiscreteTimeModel[sys, 1, Method -> "ZeroOrderHold"] (* General::ivar: 0 is not a valid variable. >> *)  Now copied A,B,C,D to Maple and tried there:  restart: alias(DS=DynamicSystems): alias(size=LinearAlgebra:-Dimensions); a:=Matrix([[0,1,0,0],[0,0,0,0],[0,0,0,1],[0,0,0,0]]): b:=Matrix([[0,0],[1/m,0],[0,0],[0,1/m]]): c:=Matrix([[1,0,0,0],[0,0,1,0]]): d:=Matrix([[0,0],[0,0]]): sys:=DS:-StateSpace(a,b,c,d); sysd:=DS:-ToDiscrete(sys,1,method=zoh); {sysd:-a,sysd:-b,sysd:-c,sysd:-d}; #print A,B,C,D in z spce  sysdAstf:=DS:-TransferFunction(sysd): sysdAstf:-tf; # extract transfer functions in z space  May be M got some problem because there is no stiffness terms in the differential equations, hence the second and fourth state derivatives have zero rows in the A matrix. I do not know. Mathematica version 10, windows 7, Maple 18.01, windows 7 • the matrices of the discrete-time model are correct. The problem is in handling of the variables. Commented Sep 3, 2014 at 3:52 • Wolfram customer support confirmed it's a bug. Commented Sep 5, 2014 at 1:55 The input form of$spacecraft\$ is the following:

StateSpaceModel[{{{0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0,
0}},
{{0, 0}, {1/m, 0}, {0, 0}, {0, 1/m}}, {{1, 0, 0, 0}, {0, 0, 1,
0}},
{{0, 0}, {0, 0}}}, {{x[t], 0},
Subscript[\[FormalX], 1], {y[t], 0}, Subscript[\[FormalX], 2]},
{{u1[t], 0}, {u2[t], 0}}, Automatic, t, SamplingPeriod -> None,
SystemsModelLabels -> None]


Delete all but the first argument, to get this:

StateSpaceModel[{{{0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0,
0}},
{{0, 0}, {1/m, 0}, {0, 0}, {0, 1/m}}, {{1, 0, 0, 0}, {0, 0, 1,
0}},
{{0, 0}, {0, 0}}}]


ToDiscreteTimeModel will definitely work with the above input.

To programmatically achieve this, do:

spacecraft = StateSpaceModel[Normal@spacecraft]


The errors you are getting seems to be because ToDiscreteTimeModel is having trouble handling the variables in StateSpaceModel. These variables were introduced in v10 and are primarily for the nonlinear systems. Nevertheless, it should still handle the variables correctly and produce the following:

StateSpaceModel[{{{1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 1}, {0, 0, 0,
1}},
{{1/(2*m), 0}, {1/m, 0}, {0, 1/(2*m)}, {0, 1/m}}, {{1, 0, 0,
0}, {0, 0, 1, 0}},
{{0, 0}, {0, 0}}}, Automatic, {{u1[\[FormalK]],
0}, {u2[\[FormalK]], 0}},
{Subscript[\[FormalY], 1][\[FormalK]],
Subscript[\[FormalY], 2][\[FormalK]]}, \[FormalK],
SamplingPeriod -> 1, SystemsModelLabels -> None]


I thought this handled correctly in v10, but I can verify that it will be in the next release.