Example 1. Simulation of an Equation.

In this example we will use Simulink to model an equation. Let's consider

 (1)

where the displacement x is a function of time t, frequency w, phase angle phi, and amplitue A. In this example the values for these parameters are set as follows: frequency=5 rad/sec;phase=pi/2;A=2.

1. From Simulink's library drag the following blocks to the Model Window

 Blocks to be dragged to the model window Where located in Simulink library browser Ramp Sources Constant Sources Gain Math Operation Sum Math Operation Product Math Operation Trigonometry Function Math Operation Scope Sinks Mux Signal Routing

2. The next step is to connect these blocks as shown in Figure E1-1.

Figure E1-1

Double click on the blocks and enter the appropriate values as prompted by the pop-up dialog windows. Note that the cosine function can be selected from the pull-down menu in the pop-up window. In the arrangement shown above, the input signal (a ramp function) is to be displayed along with the output (displacement) via the use of the mux tool as demonstrated earlier in this tutorial. To view the plots, double click on the scope.

3. Make sure all blocks are connected correctly then run the simulation (CTRL+T). You may need to select the Autoscale button on the scope display window to obtain a better display of the plots.
You may find the sinusoidal plots to be a bit "jaggy". You may want to improve the resolution of the displayed plot by redefining the Max Step Side value ("auto" is set a default value) in Simulation Parameters window (with keystrokes CTRL+E in the model window). Just for fun, you may want to experiement with different choice of solver. ODE45 is a default choice. You are encouraged to learn more about the solver methods by checking out the help files in Matlab command window. For instance, help ODE45 for parameters in non-stiff differential equations.

This example has demonstrated the use Simulink with built-in mathematical functions and other supporting toolboxes to simulate an equation. The same output/result can also be obtained with the following set of instructions entered in Matlab command window:

>> t=(0:.01:10);A=2;phi=pi/2;omega=5;
>> xt=A*cos(omega*t+phi);
>> plot(t,xt);grid

______________________________________

Example 2. Mass-Spring-Dashpot System Simulation

Figure E2-1

Consider a mass-spring-dashpot system as shown in Figure E2-1. The mathematical model for this system is described by

 (2)

where m is the equivalent mass of the system, c is the damping ratio, k is the spring stiffness, and f(t) is the forcing function in the x-direction.

In this example I will illustrate how to use Simulink to simulate the response of this system to unit step input.

STEP 1

In Simulink, create a new model window (CTRL+N) and drag the following blocks from the Simulink library window:

 Blocks to be dragged to the model window Where located in Simulink library browser Step Sources Gain Math Operation Sum Math Operation Integrator Continuous Scope Sinks To Workspace Sinks

STEP 2

By re-arranging Eqn 2 to yield an expression for the acceleration term, Eqn (2) becomes

 (3)

Based on Eqn 3, we connect the blocks in the diagram as shown in Figure E2-2. Use CTRL+F and CTRL+R to flip and rotate the blocks as necessary (select the block first then execute the key sequence). Note that you can use CTRL+right mouse button to create branches of the connecting lines. Don't worry about the parameter values and the signs for these blocks at this point as we'll take care of this in STEP 3. Just get them connected first.

Figure E2-2

STEP 3

Enter the values of the parameters for each block. In this example, we will set m = 2.0; c=0.7; k=1. You are encouraged to try different values and observe the system's response to step input.

To show that you may obtain different form of output, I included another block (in addition to the scope block) called "simout". This block can be found in the Sinks group from the Simulink Library browser. The output from this block is used in Matlab workspace. To illustrate how this block works, I will select a name for the output called "simout" as the variable name in the block's parameter setting (double click on the "simput" block to bring up the parameter dialog window). In additon, I will need a time array from the simulation. This can be specifed as a parameter in the Simulation Parameter window (CTRL+E) under the Workspace I/O tab as shown in Figure E2-3.

Figure E2-3

STEP 4 Run the simulation by clicking on the button (alternately you may use keyboard command CTRL+T ). The screenshot of the output from the Scope block is show in Figure E2-3.

Figure E2-3

That's it! You have successfully modeled and simulated a second-order under-damped dynamic system. To exam different responses, feel free to change different values for m, c, and k in the gain blocks.

To see how you can use the output from the "simout" block (by the way, you may name the block whatever you wish), go to Matlab Command window and type

>> who

You should receive an echo from Matlab listing the following variables: "simout" and "time" (and perhaps others variables in the current workspace memory).

Now, you may create a plot of the system response identical to that shown in the Scope output. The command for creating this plot is:

>>plot(time,simout);grid

Note that the outout format used in the example above is matrix type. The output sent to workspace can be used for further analysis and storage in ascii format. Output to workspace allows more options in plot presentation and further data analysis as the arrays are in ascii format.

Example 3. Using the same system presented in Example 2, we will simulate the response using transfer function approach.

Example 4.

In this example, we'll consider the same system as described in Example 3. But instead of using step input, we'll excite the second-order system with impulse load. In addition, we will simulate the response using a state-space model.

NOTES ON IMPULSE FUNCTION

Impulse function is useful in the simulatoin of impact or sudden loads such as the striking of a the tip of the pole against a ball in a pool game. Unit impulse function at a desired instant a is defined by

 (4)

Equation 4 is also known as Dirac delta function.

To simulate unit impulse in Simulink, we'll use a two-block step function set as shown below.

SOME NOTES ON STATE-SPACE APPROACH

The concept of the state of the system is utilized extensively in the time-domain analysis and design of control systems. The state variables (along with the input functions) used in equations describing the dynamics of a system provide the future state of the system. Mathematically, the state of the system is described by a set of first-order differential equation in terms of state variables.

For our example, we will express the natural state variables,e.g., position and velocity, of the system in terms of the following variables:

 (5)

where x1 represents the position and x2 represents the velocity of the system. With the new state variables defined in Eq. 5, Eq. 3 becomes

 (6)

In a matrix form:

 (7)

Equation 7 may be expressed in a more compact form:

 (8)

where A is known as the system matrix and B as the input matrix.

The output equation is expressed by

 (9)

where

 (10)

and

 (11)

C is called the output matrix and D is called the direct transmittance matrix.

In this problem we will first ask Matlab to convert the transfer function as shown in Figure E3-1 using the following command:

>> [A,B,C,D]=tf2ss(1,[2,0.7,1])

NOTE: You are encouraged to read the help file on tf2ss function (>> help tf2ss).

Now, let's get back to SIMULINK window and construct the necessary blocks as shown below. Note that the additional blocks (Transfer Fcn and its "scope") shown in light blue are provided for comparison with the the output from state-space block.

As demonstrated in previous examples, you may change the parameters in the state-space block or any other block by double clicking on it to bring up a parameter editing window.

For state space model, enter the following parameters:

A: [-0.35 -0.5;1 0]
B: [1;0]
C: [0 0.5]
D: 0

Once the entries are completed, click OK button to close the panel and continue on making necessary entries for other blocks.

For the impulse simulation:

In this example, let's consider an unit impulse at 0.2 second with pulse duration of 0.01 second and a magnitude of 40. To simulate this impulse, we'll enter in the first block (Step start) the following parameters (double click on the block to bring up the parameter windows):

Step time: 0.2
Initial value: 0
Final value: 40

For the second block (Step end):

Step time: 0.21
Initial value: 0
Final value: 40

This will produce the following impulse:

WARNING: Cares must be taken in selecting the proper pulse duration. A duration that is too short may yield computational error. A duration that is too long could misrepresent the dynamics of the modeled system.

To run the simulation use keystrokes: CTRL+T or click on the button.

To change the simulation parameters and make adjustment to simulation duration, press CTRL+E or choose Simulation parameters... from Simulation menu.

Enter 30 for Stop time.

The screenshot below shows the impulse response of the system described by Eq. 2 with m = 2; k = 1; c=0.7

For the fun of it, you may want to make adjustments to damping ratio and/or spring stiffness paramaters and run the simulation again to see how these changes would affect the response to the impulse input.

_________________________________