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.
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.
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:
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.
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
M1=1; M2=0.5; k=1; F=1; u=0.002; g=9.8;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; 1/M1; 0; 0]; C=[0 1 0 0]; D=[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.
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:
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.