Tc3, Tc7, Tc8 - Euler methods in a spreadsheet

Tc3, Tc7, Tc8 - Euler methods in a spreadsheet

Construct and adapt spreadsheets for Euler's method and the modified Euler method. You will use the correct starting and predicted gradients, retain precision, compare step lengths at the same endpoint and support an appropriate numerical answer.

A tangent step becomes a spreadsheet row

Starting at (0,1)(0,1), a curve has gradient x+yx+y. A tangent step of length 0.20.2 predicts height 1.21.2. Would two steps of length 0.10.1 land at the same height?

They do not: the second short step uses a new gradient. The calculation is a chain of local predictions, rather than one tangent stretched across the whole interval.

You already know

The differential equation supplies the gradient at each point. A tangent field shows those directions, and an initial condition selects the intended curve. Numerical values approximate that curve; a displayed decimal is not an exact analytical solution.

Over a short horizontal step, Euler's method uses the current gradient throughout the step.

Euler's method

For y′=f(x,y)y'=f(x,y), starting at (x0,y0)(x_0,y_0), use

xn+1=xn+h,yn+1=yn+hf(xn,yn)x_{n+1}=x_n+h,\qquad y_{n+1}=y_n+h f(x_n,y_n)

Here hh is the step length. Evaluate the gradient at the old point, and check that every evaluation remains in the equation's domain.

In the formula booklet.

For the family f(x,y)=x+ayf(x,y)=x+ay, put hh in H1\text{H1} and aa in H2\text{H2}. Enter the following literal spreadsheet formulae. In an entry, ∗* means multiplication; the dollar signs keep the two parameter cells fixed.

CellEntryMeaning
H1\text{H1}0.20.2Step length
H2\text{H2}11Equation parameter
A2\text{A2}00Initial xx
B2\text{B2}11Initial yy
C2\text{C2}=\text{A2}+\text{\H$2}*\text{B2}$Current gradient
A3\text{A3}=\text{A2}+\text{\H$1}$Next xx
B3\text{B3}=\text{B2}+\text{\H$1}*\text{C2}$Next yy

Copy C2\text{C2} down the required rows; copy A3:B3\text{A3:B3} down as well. Row 2+n2+n stores (xn,yn)(x_n,y_n). Do not copy the initial-value row into the later yy cells. These spreadsheet design conventions are not supplied in the booklet.

Worked example 1: two Euler steps

Question. With a=1a=1, h=0.2h=0.2, x0=0x_0=0, y0=1y_0=1, use Euler's method to estimate y(0.4)y(0.4).

Plan. Use two rows of updates, each based on the preceding point.

f(0,1)=1starting gradienty1=1+0.2(1)=1.2,x1=0.2first tangent stepf(0.2,1.2)=1.4update the gradienty2=1.2+0.2(1.4)=1.48,x2=0.4second step\begin{aligned} f(0,1)&=1 && \text{starting gradient}\\ y_1&=1+0.2(1)=1.2,\quad x_1=0.2 && \text{first tangent step}\\ f(0.2,1.2)&=1.4 && \text{update the gradient}\\ y_2&=1.2+0.2(1.4)=1.48,\quad x_2=0.4 && \text{second step} \end{aligned}

Why this step? The gradient 1.41.4, not the original gradient 11, belongs to the second row.

Answer. y(0.4)≈1.48y(0.4)\approx1.48 by Euler's method with h=0.2h=0.2.

Check. There are 0.4/0.2=20.4/0.2=2 updates. Both positive gradients predict an increasing curve, as the numerical output shows.

Change only hh to 0.10.1; the equation, initial point and target stay fixed.

Worked example 2: a smaller step changes the answer

Question. Estimate y(0.4)y(0.4) with h=0.1h=0.1, using the same spreadsheet.

Plan. Change H1\text{H1}, recalculate four updates and read the row whose xx-coordinate is 0.40.4.

y1=1+0.1(0+1)=1.1new first stepy2=1.1+0.1(0.1+1.1)=1.22new second step\begin{aligned} y_1&=1+0.1(0+1)=1.1 && \text{new first step}\\ y_2&=1.1+0.1(0.1+1.1)=1.22 && \text{new second step} \end{aligned}
nnxnx_nDisplayed yny_n, 88 d.p.
00001.000000001.00000000
110.10.11.100000001.10000000
220.20.21.220000001.22000000
330.30.31.362000001.36200000
440.40.41.528200001.52820000

Why this step? Halving hh doubles the number of steps to the same target; reading the same row number would compare different xx-coordinates.

Answer. y(0.4)≈1.5282y(0.4)\approx1.5282 for this step length.

Check. At x=0.2x=0.2, two short steps give 1.221.22, whereas one long step gives 1.201.20. This answers the opening question: the intermediate gradient changes the prediction.

Euler's polygon follows straight pieces of successive tangents. It need not stay on the exact solution curve, even when the initial tangent is correct.

[DIAGRAM: asset_name: Euler steps and the exact solution; asset_slug: euler_step_comparison; caption: A new gradient after each step changes the polygonal path; description: The exact solution y=2 exp(x)-x-1 from (0,1) is plotted on x from 0 to 0.4 beside Euler polygons with h=0.2 and h=0.1. Nodes at x=0.2 have heights 1.20 and 1.22 and are labelled separately with clear leader lines. The common final target x=0.4 is marked.]
A new gradient after each step changes the polygonal path

Watch out

Wrong: use f(xn+1,yn)f(x_{n+1},y_n) in an Euler row, or lock the previous value with an absolute reference. Right: use both coordinates from the old row and let those references move down.

Halve the step once more for a shorter target.

Predict the endpoint, then average two gradients

Euler uses only the starting gradient. The modified Euler method used here also estimates the gradient at the end of the step, then averages the two. It is the explicit trapezium, or Heun, version of a second-order Runge–Kutta method.

Modified Euler method

For the current point (xn,yn)(x_n,y_n), calculate

k1=hf(xn,yn)k2=hf(xn+h,yn+k1)xn+1=xn+hyn+1=yn+k1+k22\begin{aligned} k_1&=h f(x_n,y_n)\\ k_2&=h f(x_n+h,y_n+k_1)\\ x_{n+1}&=x_n+h\\ y_{n+1}&=y_n+\frac{k_1+k_2}{2} \end{aligned}

The kik_i are increments, already multiplied by hh. The endpoint in the k2k_2 calculation is an Euler prediction, not the final corrected value.

In the formula booklet.

Adapt the previous spreadsheet for f(x,y)=x+ayf(x,y)=x+ay. Keep the initial values and the two parameter cells, but replace the calculation columns and the yy update:

CellNew entryMeaning
C2\text{C2}=\text{\H$1}(\text{A2}+\text{$H$2}\text{B2})$k1k_1
D2\text{D2}=B2+C2=\text{B2}+\text{C2}Predicted endpoint height
E2\text{E2}=\text{\H$1}((\text{A2}+\text{$H$1})+\text{$H$2}\text{D2})$k2k_2
A3\text{A3}=\text{A2}+\text{\H$1}$Next xx
B3\text{B3}=B2+(C2+E2)/2=\text{B2}+(\text{C2}+\text{E2})/2Corrected next yy

Copy C2:E2\text{C2:E2} down, and copy A3:B3\text{A3:B3} down. There is no circular reference: the predicted height is calculated first, and the corrected height becomes the next row's starting value.

Worked example 3: two modified Euler steps

Question. Estimate y(0.4)y(0.4) for y′=x+yy'=x+y, y(0)=1y(0)=1, with h=0.2h=0.2.

Plan. At each step calculate the starting increment, the predicted endpoint increment, then their average.

k1=0.2(0+1)=0.2starting incrementk2=0.2(0.2+1.2)=0.28at the predicted endpointy1=1+0.2+0.282=1.24corrected first valuek1=0.2(0.2+1.24)=0.288restart from the corrected pointk2=0.2(0.4+1.528)=0.3856new prediction 1.24+0.288y2=1.24+0.288+0.38562=1.5768corrected second value\begin{aligned} k_1&=0.2(0+1)=0.2 && \text{starting increment}\\ k_2&=0.2(0.2+1.2)=0.28 && \text{at the predicted endpoint}\\ y_1&=1+\frac{0.2+0.28}{2}=1.24 && \text{corrected first value}\\ k_1&=0.2(0.2+1.24)=0.288 && \text{restart from the corrected point}\\ k_2&=0.2(0.4+1.528)=0.3856 && \text{new prediction }1.24+0.288\\ y_2&=1.24+\frac{0.288+0.3856}{2}=1.5768 && \text{corrected second value} \end{aligned}

Why this step? The second step starts from 1.241.24, not from the discarded Euler prediction 1.21.2.

Answer. y(0.4)≈1.5768y(0.4)\approx1.5768.

Check. The exact value from y=2ex−x−1y=2e^x-x-1 is approximately 1.583649401.58364940. This modified Euler estimate is closer than the earlier Euler estimate 1.481.48 at the same step length.

Change only hh to 0.10.1, as in the earlier Euler comparison.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account