The toy train has a locomotor and a cart and can be modeled as a two-mass-spring system, with the locomotor exerting the input force f to displace the train.
The Lagrangian of a two-mass-spring system:
In[1]:=
ℒ=
1
2
m
1
2
D[
x
1
[t],t]
+
1
2
m
2
2
D[
x
2
[t],t]
-
1
2
k
2
(
x
1
[t]-
x
2
[t])
;
Derive the equations of motion:
In[2]:=
eqns=Table[
∂
t
D[ℒ,q'[t]]-D[ℒ,q[t]]
q
,{q,{
x
1
,
x
2
}}]/.{
x
1
f[t]-μ
m
1
g
x
1
'[t],
x
2
-μ
m
2
g
x
2
'[t]}
Out[2]=
{k(
x
1
[t]-
x
2
[t])+
m
1
′′
x
1
[t]f[t]-gμ
m
1
′
x
1
[t],-k(
x
1
[t]-
x
2
[t])+
m
2
′′
x
2
[t]-gμ
m
2
′
x
2
[t]}
Define a set of numerical values for the model's parameters:
In[3]:=
pars={
m
1
0.5,
m
2
0.25,k1,μ0.02,g9.8};
A state-space model of the toy train:
In[4]:=
train=StateSpaceModeleqns/.pars,{
x
1
[t],
x
2
[t],
x
1
'[t],
x
2
'[t]},f[t],{
x
1
[t],
x
2
[t]},t,
labels
Out[4]=
f
x
1
0.
0.
1.
0.
0.
x
2
0.
0.
0.
1.
0.
′
x
1
-2.
2.
-0.196
0.
2.
′
x
2
4.
-4.
0.
-0.196
0.
x
1
1
0
0
0
0
x
2
0
1
0
0
0
A displacement of the train's locomotive's position