Getting Started Doing Simulations
Why Simulate?
What Is A Simulation?

Why Simulate?

       Another lesson has discussed why you need to do simulations, and for the need to have a model of your system that is in a form you can use for computation.  Of course, your level of trust in your prediction will depend upon how well you modelled your system.  That's another story.  We'll start by discussing how to get started doing a simulation.  First, the goals for the lesson.

    Given a first order system,
    Be able to simulate the system using the Euler integration algorithm.
    Be able to choose a good time interval for calculation using that algorithm.

What Is A Simulation?

        A simulation is a mathematical calculation of a system's response.  The results can be presented in a number of ways, including numerical results, graphical plots, etc.  The intent is to inform the designer about what is going to happen.

What Do I Need To Start Simulating?

        Well, the first thing you need is a good mathematical description of your system.  You need a description that you can use to make predictions of how the system will behave as time goes on when it is operating.  In other words, you need a dynamic description like a transfer function, a differential equation or a set of state equations.

What Kind Of Description Works Best?

        You don't have much choice here.  If you want a good general way to describe a system you pretty much have to use a set of state equations.

So, What Do You Do?

        There's an old adage - "If the only tool you own is a hammer, then after a while everything looks like a nail."  Analytical tools for system design focus on linear systems.

If you have a system that is nonlinear, you linearize it and use your linear analytical tools.

When you're done with your design, you then try to account for nonlinearities.

So, the question comes down to this.  How can you predict how a system will behave when you account for the nonlinearities in the system?"

        How can you predict how a system will behave when you account for the nonlinearities in the system?
There are standard numerical techniques for solving nonlinear differential equations.

That process of using numerical techniques to determine numerical solutions is called simulating the system or simulation.

State equations are the commonest way of describing nonlinear systems and produce a model form that can be used in most simulation software.

The process is straight forward.
Determine a set of state equations for your system, including any nonlinear effects present in your system.

Code the resulting set of nonlinear differential equations for use in a standard set of simulation software.  You can use Mathcad, Matlab, or Simulink, for example.

Compute the system's response.  Do that for a variety of conditions that are representative of actual operating conditions - including extremes.

        Remember, you may dread working with state equations, but if you've ridden an airplane recently you put your life in the hands of people who used state equations to predict the aircraft's behavior - and it's always nonlinear - so you have to deal with them.

        In order to understand simulations, we need to start with a simple example.

tdx(t)/dt + x(t) = Gdcu(t)

dx(t)/dt = - x(t)/t + Gdcu(t)/t

        Next, if we want to solve this differential equation numerically, there are a number of numerical algorithms we can choose.
You should realize that a choice means a difference.  They don't all give the same numerical results.  Some are more accurate than others.

We're going to start by looking at the simplest algorithm possible.


The Euler Integration Algorithm

       To understand this algorithm, consider the situation below.  Assume that
 we have done the computation to some time, t.

Now, if we have the computation to some time, t, call the value we have x(t).

Our goal is to compute the next point, x(t + Dt).

The simplest way to estimate x(t + Dt) is to extrapolate the slope, as shown in the figure below.  That figure also shows the danger of that simple extrapolation.  A linear extrapolation will not follow the curve of the true function, x(t).

We can develop an expression for x(t+Dt) in terms of the value know at an arbitrary time, t.

        From the diagram above, we can get an approximate value for x(t + Dt).

x(t + Dt) ~= x(t) + Dt * (dx/dt)|t.

This simply extrapolates the slope from a starting value of x(t).

The slope at time t, is denoted by (dx/dt)|t.

        Now, here's the algorithm.  Actually, it's called the Euler integration algorithm.
You need a starting value for x(t).  Normally, that is the value at some arbitrary time assumed to be t = 0.

Compute x(t + Dt) ~= x(t) + Dt * (dx/dt)|t.

Repeat the second step above until the computation covers the time needed.

A side note:  The Euler integration algorithm is one algorithm used when integral control is implemented digitally.  Site of a future link to that lesson.
         Now, let us look at how the Euler integration algorithm works for the simple first order linear system we mentioned earlier.  We will do this calculation assuming that the system has a constant input.  Here's the transfer function of the system we will be working with.

G(s) = X(s)/U(s) = Gdc/(ts + 1)

Let's be clear about the situation. Next, we compute x(t + Dt) ~= x(t) + Dt * (dx/dt)|t, and the value we use for the derivative is:

dx(t)/dt = - x(t)/t + Gdcu(t)/t

        Now, there are numerous different ways that you can implement this algorithm, including the following. Besides doing the calculation, you will need to know what to expect when you see the calculation results. Here's a calculation done using the Euler integration algorithm.

This simulation is done for a first order, linear system with a transfer function, G(s) = Gdc/(ts + 1).

The system has a DC gain, Gdc = 1, and  a time constant, t = 1.
For this simulation, Dt = 0.1sec, which is one-tenth of the time constant, t.
        While the simulation results look like what we might expect, it is not clear how exact these results actually are.  The next plot shows the calculated results - in blue - along with the theoretical solution - in red.  Actually, by choosing a linear, first order system, where we know the theoretical results, allows us to see how well the algorithm performs.

        While the solution looks reasonably good, close inspection shows that the calculated results seem always to lie above the theoretical result.  It is interesting to see how things change as the time interval for the calculations, Dt, is changed.  Here are the results when Dt is increased from 0.1 sec to 0.2 sec.

        Generally, the calculation is good, but the tendency to be above the theoretical solution is worse than it was for the smaller calculation interval.

        It's reasonable to expect accuracy to deteriorate as the calculation interval is increased.  If we revisit the expression for the Euler algorithm calculation it is really just a short Taylor Series expression.

x(t + Dt) ~= x(t) + Dt * (dx/dt)|t

From what we know of Taylor Series we know that the approximation is better when the calculation interval is smaller.

        Anyhow, take this a little further and increase the calculation interval to 0.5sec.  Here is the result.

        You can see that, as the calculation interval is changed, that the general shape of the computed solution looks like the theoretical solution but that it just lies above the true solution.  It would be dangerous to generalize that from what we have.  If we change the calculation interval even more some strange things begin to happen.  Here is the result when Dt = 1.0.  We've increased to size of the dots for the calculated response to emphasize what happens for this calculation interval.

One needs to ask "What is going on here?".  Note the following about this calculation

Upon further reflection, the first point calculated - at one second - is exactly one - the steady state value.

Once the steady state value is reached in the computation, the calculated derivative is zero, so the calculation doesn't change after that.

We can probably conclude that we should always use a small Dt when we do a simulation.  The general rule is to use a Dt that is an order of magnitude smaller than the time constant, t.  We've done this calculation with a time constant of one second.  It's the ratio of the calculation interval to the time constant that is important.

        Now examine a calculation interval for Dt = 1.2 sec.  That pushes the calculation interval just beyond the value that produces the instant jump to steady state.  Here's the result.

There's a new phenomonon in this calculation - calculation overshoot and oscillation.  You probably never saw that before.  The lesson here is that the calculated response doesn't even have to have the same form as the theoretical response.

It's getting worse all the time.....

The worst is yet to come.  Let's try an even larger time interval, Dt = 2.0 sec - twice the time constant .
 

Now, the oscillations persist.  Note we had to change the scale to show you the top points in the oscillations.

The solution doesn't converge to the steady-state value.  It just oscilates around the steady state value.

It gets worse for larger intervals.

        This is another new phenomenon- sustained oscillations in the calculations.  Have you seen that before?  You had better think about that.  The calculated solution doesn't look like the theoretical solution, it doesn't converge like it should.  You should be getting to the point where you stop believing in calculations just because they are done by computer.  Remember this fundamental law.
Any calculation implements an algorithm, and the solution is only as good as the algorithm, even if the calculations are carried out to 359 decimal places.

If you believe any calculations just because they are calculations, then you better remember E. J.'s variation on Barnum's Law - Barnum underestimated the birthrate.

        Now, let's crank it up another small notch.  Do a calculation for Dt = 2.5 sec.  Here's what you will get.  Notice that we changed the scale another time, and we still get this one dot way out here!  It's actually part of the plot and we decided to include it FYI.

        It gets worse for larger intervals.  If the interval is larger, the computed solution grows even faster, but you should get the point by now.



Summing It Up

        The simulation of the first order system that we have been examining shows us several things that can happen when you use a simulation algorithm to compute a system response.

If you choose your computation interval to be small enough, then you can get an accurate calculation of the system response.
The update algorithm depends upon the ratio Dt/t.  Accuracy and stability in the calculation depend upon that ratio.

Too large a calculation interval can lead to disastrous failure.

What Else Is There To Be Concerned About?

        The example we have been using has highlighted features of simulation calculations, but there are other things to be concerned about.

We have only looked at a first order system.  We need to examine how to simulate higher order systems.  We'll find that we need to worry about how to get a state description of a system.

Our single example was a linear system.  We need to look at how to simulate nonlinear systems.  Click here to go directly to that lesson.  LINK TO BE IMPLEMENTED LATER.

We only looked at one integration algorithm, the Euler integration algorithm.  However, while there are more complex/more accurate algorithms, most of them share the characteristics we found in the Euler integration algorithm.