Modeling Tutorial

Train system
Free body diagram and Newton's law
Equations of motion
Entering equations into matlab
Transfer function

Matlab can be used to represent a physical system or its model. To begin with, let's start with a review of how to represent a physical system as a set of differential equations.

Train system

In this example, we will consider a toy train consisting of an engine and one car. Furthermore, assume the train only goes forward (this way, we only have to deal with the thrust of the engine 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 car will be represented by masses M1 and M2 respectively. The two are held together by a spring k, which is not very stiff. F is the force of the engine, and the Greek letter, mu (which will also be represented by the letter u), is the coefficient of rolling friction.

Free body diagram and Newton's law

The system can be represented by the following Free Body Diagram on which all of the forces acting on the body are drawn.

From Newton's law, you know that the sum of the forces acting on a mass equals its mass times its acceleration. In this case the forces acting on M1 are the spring, friction and force of the engine. The forces acting on M2 are the spring and friction. In the vertical direction, the gravity force is canceled by the normal force of the ground acting on the train, and therefore there is no acceleration in the vertical direction. The equations of motion in the horizontal direction are as follows:

Equations of motion

This set of equations can now be manipulated into state-space form. This is done by writing the equations as a set of first-order differential equations. We will use x1 and x2 and their derivatives as the four state variables. In this problem, both state variables have reference positions of zero. The desired output is the velocity of the engine.

Entering equations into Matlab

Now we will see how to use Matlab to analyze and manipulate the system given a set of equations in state-space form. Since Matlab cannot deal with symbolic variables, we must come up with some numbers for the variables. Lets assume that

Now we are ready to start using Matlab. Create an m-file to run and enter the following lines of code:
We now have the variables needed to represent the model. The best way to use Matlab to represent a physical system is to enter it in its state-space form, using the A, B, C, D matrices.
     A=[    0    1     0      0;  
         -k/M1  -u*g  k/M1    0;
            0    0     0      1;
          k/M2   0   -k/M2  -u*g];
     B=[  0;
     C=[0 1 0 0];
See the tutorial Matlab basics to learn more about entering in matrices. All the computations done inside Matlab that involve state-space models can use the A, B, C, D matrices directly (assuming you have entered them in correctly). You now have the toy train modeled in Matlab.

Transfer function

Often, you do not wish to work with the system in state-space form because it is inconvenient or impractical. In such cases, you usually convert the differential equation into a transfer function using Laplace transforms. Matlab can take the Laplace transform of the state-space equations for you and give you the transfer function quite easily. This is done using the ss2tf command.

     [num,den] = ss2tf(A,B,C,D);
By entering this command at the end of your m-file, you have created two vectors, num and den that contain the numerator and denominator of your transfer function. Type either num or den into the command window to see the transfer function. Your numerator and denominator should look similar to the following:
The leading zero in the numerator is not part of the transfer function. It is just a convenience Matlab has incorporated so that the two polynomials (num and den) are the same length. To see that the leading zero is not part of the numerator, check the number of roots in the numerator. There should only be three. This can be done if you enter the command roots(num) into the command window. You should get the following output:
As you can see, there are only three roots. The numerator and denominator shown above represent the transfer function:

Matlab returns the transfer function as two vectors that represent the coefficients of the numerator and denominator polynomials in descending order of the power of s.

If you were initially given the transfer function in a problem, you can still use Matlab to perform your calculations. In this case, you would just have to enter the transfer function instead of the state-space model. For example, suppose you were given the above transfer function. This is how you would enter the transfer function into Matlab.

     num = [0 1 0.0196 2 0];
     den = [1 0.0392 3.004 0.0588 0];
You are now ready to continue solving the problem. It should be noted that many operations that can be done on systems, can be done using either the transfer function or the state-space model. Furthermore, it is simple to transfer between the two if the other form of representation is required. Click here to read more about converting back and forth between representations.

These tutorials contain four examples you can follow to learn more about modeling. You can link to them from below.

User Survey
Your anonymous answers to the following questions will help us to improve future versions of these tutorials.
How useful was this tutorial?
Very useful Useful Somewhat useful Not very useful A waste of time
How was the level of the tutorial?
Too simple About right Too difficult
How much time did you spend on it?
Less than 30 mins. 30 - 60 mins. More than 60 mins.
How much of the Matlab code did you run?
None Some Most

We would like to hear about suggestions you have for improvement, difficulties you had with the tutorials, errors that you found, or any other comments that you have. This feedback is anonymous.

If you would like to receive a response, send your comments or questions to

Modeling Examples
Cruise Control | DC Motor | Bus Suspension | Inverted Pendulum

Basics | Modeling | PID | Root Locus | Frequency Response | State Space | Examples

8/20/96 JDP