# A simple look at error analysis

You've heard that the first-order Euler's method is simple and easy to understand ... but maybe not so accurate. Well, is there a way to quantify that statement? Just how inaccurate is it?

There are several ways to test the accuracy of a computer simulation. One of the easiest ones is to pick a situation in which you KNOW the correct answer, and compare the simulation's value to that known value. Let's do that. We'll choose the situation described in the simple ball trajectory assignment.

The problem involves a ball which is tossed upward from ground level with some given velocity V0. We ignore air resistance and pretend that the gravitational acceleration is always g = -9.8 meters/sec^2. In this ideal situation, the height of the vall and the velocity of the ball at any time can be calculated analytically.

```

Q:  What is an equation for velocity of the ball
as a function of time?

Q:  What is an equation for height of the ball
as a function of time?

```

Right.

Okay. Suppose that we send the ball up into the air with an initial velocity V0 = 100 m/s. Can you answer these questions?

```

Q:  How long should the ball be in the air
before it strikes the ground?

Q:  What should the final velocity of the
ball be just as it strikes the ground?

Q:  Are there any other simple observable
quantities which you can figure out exactly?

```

Okay, we know what the results of our simulation SHOULD be. Let's find out what the program actually reports. I've written a MATLAB program which uses Euler's method to first order to compute the position and velocity of a ball thrown upward. I can't show you the program -- after all, it is your job to write one this week! -- but I'll be happy to show you some of the results.

If I use a timestep dt = 1 sec, I find the total time in the air to be 21.396 sec.

```

Q:   What is the ABSOLUTE error in this time?

Q:   What is the FRACTIONAL error in this time?

```

You'll find that the fractional errors are usually more useful than absolute values, especially when trying to compare the quality of a program in different situations.

Now, if I decrease the step size, I should decrease these errors, right? Right. Let me see.

```

timestep       time in air      error      fractional error
(sec)           (sec)          (sec)
------------------------------------------------------------------------
1.0            21.396         0.988         0.048

0.5            20.906         0.498         0.024

0.25           20.657         0.249         0.012

0.125          20.533         0.125         0.006
------------------------------------------------------------------------

```

```

Q:  When I cut the timestep by a factor of two,
by what factor did the fractional error change?

Q:  When I cut the timestep by a factor of two AGAIN,
by what factor did the fractional error change?

```

In many situations, you may find that the errors in your simulations are related to the size of the timestep by a power law, something like this:

The question is -- what is the exponent p?

• If p = 1, then cutting timestep in half makes the fractional error smaller by a factor of 2. That's okay.
• If p = 2, then cutting timestep in half makes the fractional error smaller by a factor of 4. That's better.
• If p = 3, then cutting timestep in half makes the fractional error smaller by a factor of 8. That's great!

So the value of the exponent p is an important feature of some algorithm: it tells us how sensitive the results are to the size of the timestep. In general, large values are better than small ones, since they allow us to decrease the error a lot with only a small change in the timestep.

How can we find the value of this exponent? One simple but effective way is to run a series of tests, recording the values of the timestep and the fractional error in some quantity. Then note what happens if we take the logarithm of the equation above:

Take your values for the timestep and fractional error and plot them on a graph ... and the slope will give you the power p.