Essentials of Mechatronics  ISBN: 0-471-72341-X- ©John Billingsley 2006 - published John Wiley & Sons, Inc

Elements of simulation

1. Introduction to State Space

State space concepts grew from the principles of simulation.  Understand one and the other becomes clear.  It is essential that you should work through these examples and make sure that their principles are clear to you.

Instead of 'black boxes' containing transfer functions, we consider the set of variables that define what the system is doing 'right now'.

For our first example we look at a water tank that has a leak at the bottom.  The one and only state variable is the level of water in the tank, which we will call x.  The rate at which the water leaks is proportional to the water level in the tank.  So how do we make a mathematical model?

State equations tell us the rate-of-change of the state variable(s).  So for the tank, the rate of change dx/dt of the level will be proportional to -x.  We can write



where the constant k describes the leak, or


where the 'dot' denotes 'rate of change'.

If we now add an 'input' to the system, a faucet that pours water into the tank at a rate that will raise the level (ignoring the leak) at a rate u, we have

 

By 'rate of change' we mean the change in x over a very short interval, divided by the length of that interval.  So to a first approximation we can say that after an interval dt the new value of x will be


so in this case, the new value is


In computer terms, we just execute the line of code

x = x + (- k x + u) * dt

and the simulation is advanced by time dt.

Of course we need another line

t = t + dt

to keep track of the time, so that we can plot x against time.

Open any of the simulations 01tank.bas, 01tank.frm or 01tank.htm and you will see these two lines, which make up the whole of the actual simulation.  The rest of the code is concerned with setting the initial value of x and the value of u and with plotting the results.

Try changing the value assigned to k, in addition to trying various values of u and initial x.  Give an initial value and zero input, and you will see a curve that represents the water draining away.  Give zero initial value and a large input and you will see the level rise until the leakage is equal to the input.



In the case of the .htm file, you can use 'view source' to see the entire program.

Routines for rescaling the plot window and changing colour have been added 'behind the scenes', so that the text in the edit box will look as simple as possible.  As well as seeing the entire works with 'view page', you can save a local copy and set to work with any text editor (Notepad is useful) to hack it into your personal preference.

Do remember that these simulations use Javascript and a Java applet - so you must permit scripts to run and you must permit Java applets to make them work.


The later .htm files give a more complete simulation, some with two text windows instead of one.  Be sure to inspect the code - you should try out a few modifications.

If you browse a page and edit the code in one of the text windows and then save the page, you will NOT have saved your changes! 

You have to select and copy your code and paste it into a text editor, then save that as a named file.

To make permanent changes, you have to open the page in a text editor such as Notepad and edit the source text.  Do NOT use a Web editor - it will mangle up the code!

To exit from a program page, use the Browser 'Back' button.


 

2.  Euler integration

If you have endured lectures on the principles of calculus, you will know that this form of integration is an approximation.  It is only truly accurate if dt tends to zero.  For a more accurate solution, there are methods such as Runge-Kutta or Newton-Raphson - but maybe this simple Euler integration is good enough for our purposes.  Maybe the integration error is much less than the error in our measurement of the system parameters.

The next example makes it easy to try out various values of dt.  Below .001 you might not be able to see any difference, though the solution will get slower as you add leading zeros.

But a value of 1 is clearly too big, while at 4 the solution goes unstable.

Click on 02euler.bas or 02euler.htm to run the simulation - if you want a VB version you must modify the code yourself.

3.  Exact solution

Using calculus, the differential equation can be solved to give an exact solution for any steplength.

Run 03exact.bas or 03exact.htm.

4. Action input

Simulations can be tested more thoroughly if we can interact with them, putting in changes of input while the simulation is running.  In this first-order example, you can vary the 'faucet' by clicking on buttons in the .htm example, or by tapping on number keys in the Basic version.

Run 04action.bas or 04action.htm.