
Train system
Free body diagram and Newton's law
Model Construction
Running the Model
Obtaining MATLAB Model
In Simulink, it is very straightforward to represent a physical system or a model. In general, a dynamic system can be constructed from just basic physical laws. We will demonstrate through an example.
In this example, we will consider a toy train consisting of an engine and a car. Assuming that the train only travels in one direction, we want to apply control to the train so that it has a smooth start-up and stop, along with a constant-speed ride.
The mass of the engine and the car will be represented by M1 and M2, respectively. The two are held together by a spring, which has the stiffness coefficient of k. F represents the force applied by the engine, and the Greek letter, mu (which will also be represented by the letter u), represents the coefficient of rolling friction.

The system can be represented by following Free Body Diagrams.

From Newton's law, you know that the sum of forces acting on a mass equals the mass times its acceleration. In this case, the forces acting on M1 are the spring, the friction and the force applied by the engine. The forces acting on M2 are the spring and the friction. In the vertical direction, the gravitational force is canceled by the normal force applied by the ground, so that there will be no acceleration in the vertical direction. We will begin to construct the model simply from the expressions:
Sum(forces_on_M1)=M1*x1'' Sum(forces_on_M1)=M1*x1''
This set of system equations can now be represented graphically,
without further manipulation. First, we will construct two copies
(one for each mass) of the expressions sum_F=Ma or a=1/M*sum_F. Open
a new model window, and drag two Sum blocks (from the Linear library),
one above the other. Label these Sum blocks "Sum_F1" and "Sum_F2".

The outputs of each of these Sum blocks represents the sum of the
forces acting on each mass. Multiplying by 1/M will give us the
acceleration. Drag two Gain blocks into your model and attach each
one with a line to the outputs of the Sum blocks.

These Gain blocks should contain 1/M for each of the masses. We will
be taking these variab as M1 and M2 from the MATLAB environment, so
we can just enter the variab in the Gain blocks. Double-click on
the upper Gain block and enter the following into the Gain field.
1/M1Similarly, change the second Gain block to the following.
1/M2Now, you will notice that the gains did not appear in the Gain blocks, and just "-K-" shows up. This is because the blocks are two small on the screen to show 1/M2 inside the triangle. The blocks can be resized so that the actual gain can be seen. To resize a block, select it by clicking on it once. Small squares will appear at the corners. Drag one of these squares to stretch the block.




+++There are only 2 forces acting on M2, so we can leave Sum_F1 alone for now.


F_friction_1=mu*g*M1*v1To generate this force, we can tap off the velocity signal and multiply by a gain, mu*g*M1. Drag a Gain block into your model window. Tap off the line coming from the v1 integrator and connect it to the input of the Gain block (draw this line in several steps if necessary). Connect the output of the Gain block to the second input of Sum_F1. Change the gain of this gain block to the following.
mu*g*M1Resize the Gain block to display the gain and label the gain block Friction_1.

+-+

k*(x1-x2)First, we need to generate (x1-x2) which we can then multiply by k to generate the force. Drag a Sum block below the rest of your model. Label it "(x1-x2)" and change its list of signs to
-+Since this summation comes from right to left, we need to flip the block around. Select the bloc by single-clicking on it and select Flip from the Format menu (or hit Ctrl-F). You should see the following.




Now the model is complete. We simply need to supply the proper input
and view the proper output. The input of the system will be the force,
F, provided by the engine. We already have placed the function
generator at the input. The output of the system will be the velocity
of the engine. Drag a Scope block from the Sinks block library into
your model. Tap a line off the output of the "v1" integrator block to
view the output. Label the scope "View_v1".
Now, the model is complete. Save your model in any file you like.
You can download the completed model here.
Create an new m-file and enter the following commands.
M1=1; M2=0.5; k=1; F=1; mu=0.002; g=9.8;Execute your m-file to define these values. Simulink will recognize MATLAB variab for use in the model.
Now, we need to give an appropriate input to the engine. Double-click
on the function generator (F block). Select a square wave with
frequency .001Hz and amplitude -1 (positive amplitude steps negative
before stepping positive).
The last step before running the simulation is to select an
appropriate simulation time. To view one cycle of the .001Hz square
wave, we should simulate for 1000 seconds. Select Parameters
from the Simulation menu and change the Stop Time field to
1000. Close the dialog box.
Now, run the simulation and open the View_v1 scope to examine the
velocity output (hit autoscale). The input was a square wave with two
steps, one positive and one negative. Physically, this means the
engine first went forward, then in reverse. The velocity output
reflects this.
Save this model as train2.mdl or download our version here. Now, we can extract the model into MATLAB.
Enter the following command at the MATLAB command window to extract a
state-space model.
[A,B,C,D]=linmod('train2')
You should see the following output which shows a state-space model of
your Simulink model.
A = -0.0196 0 1.0000 -1.0000 0 -0.0196 -2.0000 2.0000 0 1.0000 0 0 1.0000 0 0 0 B = 1 0 0 0 C = 1 0 0 0 D = 0To obtain a transfer function model, enter the following command at the MATLAB command prompt.
[num,den]=ss2tf(A,B,C,D)You will see the following output representing the transfer function of the train system.
num = 0 1.0000 0.0196 2.0000 0.0000 den = 1.0000 0.0392 3.0004 0.0588 0.0000These models are equivalent (although the states are in different order) to the model obtained by hand in the MATLAB tutorials.
