| Euler method

Euler method

Here we are dealing with the autonomous homogeneous system of linear differential equations: x' = y, y' = −x with the initial conditions x(0) = 1, y(0) = 0. We are interested in values x(2π), y(2π) (with the exact answer being 1 and 0, respectively). We will solve the system numerically using Euler method using N steps on the interval [0, 2π] (so Δt = 2π/N).

The program on a graphing calculator (TI-84 in this case) that solves the system is shown on the picture on the left. (I wish the screen would be that big.) In it we first specify the number of steps N. Then we calculate Δt (denoted as H) and set x and y to 1 and 0 — their initial values. Our next task is to proceed with the Euler step N times. For this we initialize the counter “how many steps is already done” C at 0. Then in a cycle we go with an Euler step, update the counter C and check if we have to do more steps (i.e. C is less than N yet). Finally we output the final values of x and y.

In each step we determine how we should change the values of x and y using the calculator functions Y1 and Y2 (see the picture on the right). This allowes us easily change (by redefining Y1 and Y2) which system of differential equations we are solving without changing the program. We also can rewrite the program so we input at the beginning the initial values of x and y, as the starting and final value of time t. The functions Y1 and Y2 could depend also on t (if the system is not autonomous) and on various parameters of the system (like frequency of oscillations or decay rate) which we again can input at the beginning. We also can change the step by Δt part of the program to Heun's or Runge–Kutta method to increase the accuracy of the numerical solution.

The output x(2π) and y(2π) for different values of N is given by the picture on the left. The calculation with N=10000 took about 6 minutes. Notice that the error in x(2π) value is roughly equal to 20/N, so Euler method is 1st order of accuracy. (In this particular case (because of certain reasons) the error in y(2π) value is about 84/N2. This doesn't change the order of accuracy of the numerical scheme, as the value of x(2π) is still not that good.)