## 20.2 First Order Differential Equation

We will first deal with a first order differential equation by which we mean, specifically, an equation of the form y'(x) = f(x, y), for some function f. Suppose, further, that we know the solution at some point z.

This tells us, that in the interval in x starting at z and ending at z + d for very small d, we have, approximately

y(z + d) - y(z) = f(z, y) * d

We can use this "linear approximation" to compute y(z + d), and then continue to compute y(z + 2d) from it, and so on.

This approach is like the left hand rule for doing integrals; the only difference is that y itself appears in f.

It is a little more difficult to produce the analogue of the right hand rule, or trapezoid or Simpson's rule, since they require evaluating f and hence y beyond z, and we only start with y(z). If we put y(z + d) in our formula for computing y(z + d) the computer will accuse us, rightly, of using a circular reference.

There are ways to get around this and a whole sequence of formulae are known for evaluating y(x + d) - y(d) given our equation to any order in d. These are called Runge-Kutta rules, and are very effective. You can see how they do in the accompanying applet.

We will do only the simplest correction, namely approximate y(z + d) according to

This is still pretty easy to do, and is more or less like the trapezoid rule.

What do you do if you don't know y(z) at all?

Well, you can make a grid of (x, y) pairs with separation say d, and at each draw an arrow in the direction with slope f(x, y). Then you can start anywhere and play "connect the dots" to fill in families of solutions. You can get a good idea of what solutions look like from such pictures.

You tell me I can implement the integration you described on a spreadsheet?

Yes. Put your data, which consists of the starting values of x and y, and your choice for d in a convenient place at the top, say in b2, b3, and b4.

Then start columns at a10, b10, C10 etc, which will contain x and y respectively.

So, put a10=b2, b10= b3, set a11=a10+\$b\$4 and copy it down, set b11= b10+\$b\$4/2*(f(a10,b10)+f(a10, b10+f(a10,b10))*d) and copy that down. That is it.

You can compare the result with the left hand rule computation by setting up column c and starting c10=b10, but putting c11=c10+\$b\$4*f(a10,b10) and copying it down. Then you can make an x,y scatter chart of all three columns, and see what happens. The difference between the two computations gives you an impression of how bad the simpler one is.

You can see that it takes a bit more work to change functions, but is quite easy to change initial conditions.

Exercises 20.1 Set this up for f(x, y) = x*y, and for f(x, y) = x*sin(x, y), starting at x = 0, y = 1.

Does this always work?

No. For lots of interesting equations it is fine. However, sometimes your variable y can go to infinity, and then the calculation becomes quite inaccurate.

This can happen because, we are allowing any equation for y', and hence any equation for . Which means can and will wander around. If should happen to go through 0, then y will go to infinity without any particular reason for it.

Most of the time you can avoid this difficulty by solving the differential equation for (remember that , so that if we call , u, then u obeys
The point is that usually when y goes haywire, then is close to 0.

Anyway, integrating differential equations this way is sufficiently easy that it is worth a try.