ENGM 541 Laboratory #4

University of Alberta ENGM 541: Modeling and Simulation of Engineering Systems Laboratory #4

© M.G. Lipsett 2010

MATLAB is capable of solving sets of equations in a number of ways. This lab will explore a few of them for a small linear system.

Picard’s Iteration Method and Taylor for Short Duration Simulation

We’ve looked at two numerical methods for short duration integrations with respect to time. Each has a truncation error because of the simplification of the integration of the governing equations with respect to time by truncating the .

For a symbolic expression, M ATLAB can evaluate the first few terms of the Taylor Series using the command taylor in the Symbolic Math Toolbox (which is part of the network licence). Typing the following commands, >> syms x >> s = taylor(sin(x)) returns s = x-1/6*x^3+1/20*x^5 which we recognize to be the first three terms of the Taylor Series expansion of sin( x). Actually, the call to the taylor with no additional arguments (optional parameters) returns the first five terms by default. In the case of sin( x), only three of the first five terms are non-zero.

Picard’s Method is not a vendor-supplied routine; but there are third-parties who have developed m-file functions to calculate the numerically using Picard’s method.

Exercise 1

Use ezplot to plot the symbolic expression s. How accurate does it appear to be near x=0? How accurate does it look farther away from x=0? Find the expression for the first twenty terms of the Taylor Series of sin( x), by typing >> s = taylor(sin(x),20) Use ezplot to plot the symbolic expression s again. Does it look more accurate?

Symbolic Integration with M ATLAB

Recall that MATLAB Symbolic Math Toolbox is capable of manipulating equations symbolically, to solve single equations (using solve ) and systems of equations (using solve with multiple arguments).

MATLAB can also integrate symbolic functions. A first-order ordinary differential equation (ODE) is defined by

Page 1 of 4 ENGM 541 Laboratory #4

dy y'= = g(x, y) dx where x is an independent variable. (Typically in simulations, the independent variable is not x, but time t.) The solution is a function y = f(x) , such that f’’(x) = g(x,y) . In MATLAB, the symbolic function for solving ODEs is dsolve(’equation’,’condition’) where the first argument of the function is the symbolic equation of the ODE, and the second (optional) argument specifies the initial conditions. For example, y'= 3y + e2x ; y(0) = 3 is solved symbolically in MATLAB by >> s = dsolve(’Dy=3*y+exp(2*x)’,’y(0)=3’); with the result s = -1/3*exp(2*x)+exp(3*t)*(3+1/3*exp(2*x)) which agrees with the analytical result.

Exercise 2

Find a solution to the initial value problem dy = t + 3; y(0) = 7 dt and plot the result for 0 ≤ t ≤ 10. (A simple way to plot the symbolic expression s in the domain from a to b is to use ezplot(s,[a b]) . )

Introduction to S IMULINK

As we’ve seen so far in the course, MATLAB is a very effective analysis and visualization tool. Many of the numerical procedures that we have studied are summarized in commands that we can use with confidence and very little programming.

SIMULINK is a simulation package within Matlab that features a graphical user interface and a large number of pre-programmed elements, making construction of simulations simpler than conventional programming. Each element includes the constitutive relationship as part of its properties; and the program takes care of tracking the loop and node relationships as the network of elements is constructed. In this way, the set of governing differential equations is generated automatically. As well, MATLAB and SIMULINK incorporate solvers that give a good estimate of the solution of the differential equations over a time interval. Input and output blocks allow the results of the simulation to be accessed easily.

To start SIMULINK , enter simulink in the MATLAB Command Window: >> simulink

(It might take a few seconds to load.) When the Symbolic Library Browser window opens, click on the File menu, select New, and then click on Model to open a new

Page 2 of 4 ENGM 541 Laboratory #4 working window (or type CTRL+N ). This is the space on which the elements of the system block diagram will be placed and connected to create the model that will be simulated.

The block diagram represents the governing equations of the system. The governing equations for a system can be modeled in different ways. We have been using a state space representation. (Other formulations include transfer functions and frequency response functions.) As well as the state space formulation, the model also needs the initial conditions (the input), and somewhere for the output to be plotted or recorded.

The course text has an excellent explanation about how to build a simulation block diagram in S IMULINK .

There are two key points to remember. The first point is that a set of variables such as x, x& , and x&&& are not unrelated, but actually related by integration relationships within the governing equations. S IMULINK has integrator elements, in which the input to an integrator is the of the resulting output; and so one variable and its can be expressed in a chain of integrators.

The second point is that the summing block is where the admissibility laws are satisfied for the system - in this case, a sum of forces (including the inertial force) expressed in terms of loops variables through the constitutive relationships.

Exercise 3

Go to the S IMULINK Library Browser window. Clicking on one of the items opens up a set of icons. For example, select the continuous library and drag the integrator icon to the working window.

Open the Sources library and drag the Clock, Step, and Constant icons into the working window. Open the Math library and drag Sum and Gain icons to the working window. Open the Sinks library and drag the XY Graph and To Workspace icons to the working window.

To change a parameter in an element, double-click to open it and make the changes. Change the Gain to 2, and the Constant to 5. Change Step Time to 5 in the Step icon. Open the To Workspace icon and change the Save Format setting to Array. This will save an array called simout when you run the simulation. Open the XY Graph icon and choose xmin=0, xmax=10, ymin=0, and ymax=20.

The connecting points on each icon can be joined with connecting lines. You can select any object (or several objects at once) to move in the workspace. You can also add comments on any white space in the diagram by double clicking to create a text box.

In the working window, arrange the icons and connect the lines as shown in Figure 1.

Page 3 of 4 ENGM 541 Laboratory #4

Figure 1 Go to the Simulation Menu and click on Configuration Parameters. Set the stop time for the simulation to 10 (which is the same as the xmax for the XY Graph).

To run the simulation, go to the Simulation menu and click on Start. The simulation will run; and it will produce the output array simout and the XY graph of the simulation results.

In the Matlab command window, type >> plot(tout,simout) at the command line. This produces the same figure as the XY Graph.

Exercise 4

The equation of motion for a linear mass-spring-damper system is mx&& + xb& + kx = f (t ). Build a S IMULINK block diagram of this system using only sources, integrators, gains, and summing blocks, with an XY Graph to display the output. Use m = 1, b = 1, k = 1.

Once you’ve built the simulation block diagram, simulate the response of x(t) for three different inputs f(t) : • a unit step input, • f(t)= sin(0.2 t), and • f(t) = t . Use x(0) = 0 and x& (0) = 0 for the initial conditions, and run the simulation for a few seconds. Try other values for m, b, and k, to see their effect on system performance.

Exercise 5

Change the model for a nonlinear constitutive relationship for the spring so that 3 fspring = k1x + x , where k 1 = 10. How does the change affect the response of the system?

References: Kulakowski et al, Chapter 6 and Appendix 3 & 4. D. McMahon, Matlab Demystified . McGraw-Hill 2007. www.mathworks.com

Page 4 of 4