9 Ordinary Differential Equations

Numerical Methods · Unit 9

Ordinary Differential Equations

Exam-focused notes for Ordinary Differential Equations (Numerical Methods, BIT203): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.

Ordinary Differential Equations in short

Many laws of science are written as differential equations. Most of them cannot be solved by formula. Numerical methods build the solution step by step from a known starting value.

Ordinary Differential Equations: notes

Initial value problems and solution methods

An initial value problem (IVP) is a first order equation with a starting condition.

$$\frac{dy}{dx} = f(x, y), \qquad y(x_0) = y_0$$

We choose a step size $h$ and find $y_1, y_2, \ldots$ at $x_1 = x_0 + h$, $x_2 = x_0 + 2h$, and so on. The common one-step methods are Euler's method, Heun's method and the Runge-Kutta methods. Each uses only the last point to find the next. They differ in how many slope values they use per step, and so in accuracy.

Euler's method for ODE solving

Euler's method follows the tangent line for one step. It comes from the first two terms of the Taylor series.

$$y_{n+1} = y_n + h,f(x_n, y_n)$$

The local error is $O(h^2)$ and the global error is $O(h)$. It is simple but not very accurate.

Example. Solve $\dfrac{dy}{dx} = 2x + y$, $y(0) = 1$, with $h = 0.1$ and find $y(0.4)$.

$$\begin{aligned} y_1 &= 1 + 0.1(0 + 1) = 1.1 \ y_2 &= 1.1 + 0.1(0.2 + 1.1) = 1.23 \ y_3 &= 1.23 + 0.1(0.4 + 1.23) = 1.393 \ y_4 &= 1.393 + 0.1(0.6 + 1.393) = 1.5923 \end{aligned}$$

So $y(0.4) \approx 1.5923$.

Heun's method for ODE solving

Heun's method (the improved Euler method) averages the slope at both ends of the step. First predict with Euler, then correct.

$$\begin{aligned} y^{}{n+1} &= y_n + h,f(x_n, y_n) \ y{n+1} &= y_n + \frac{h}{2}\left[f(x_n, y_n) + f(x_{n+1}, y^{}_{n+1})\right] \end{aligned}$$

The global error is $O(h^2)$, so it is better than Euler.

Example. Solve $\dfrac{dy}{dx} = 1 - 2xy$, $y(0) = 0$, $h = 0.2$. Find $y(0.6)$.

Step 1, from $x = 0$, $y = 0$:

$$\begin{aligned} k_1 &= 1 - 0 = 1 \ y^{*} &= 0 + 0.2(1) = 0.2 \ k_2 &= 1 - 2(0.2)(0.2) = 0.92 \ y_1 &= 0 + 0.1(1 + 0.92) = 0.192 \end{aligned}$$

Step 2, from $x = 0.2$, $y = 0.192$:

$$\begin{aligned} k_1 &= 1 - 2(0.2)(0.192) = 0.9232 \ y^{*} &= 0.192 + 0.2(0.9232) = 0.37664 \ k_2 &= 1 - 2(0.4)(0.37664) = 0.69869 \ y_2 &= 0.192 + 0.1(0.9232 + 0.69869) = 0.35419 \end{aligned}$$

Step 3, from $x = 0.4$, $y = 0.35419$:

$$\begin{aligned} k_1 &= 1 - 2(0.4)(0.35419) = 0.71665 \ y^{*} &= 0.35419 + 0.2(0.71665) = 0.49752 \ k_2 &= 1 - 2(0.6)(0.49752) = 0.40298 \ y_3 &= 0.35419 + 0.1(0.71665 + 0.40298) = 0.46615 \end{aligned}$$

So $y(0.6) \approx 0.4662$.

What this unit covers

  • Initial value problems and solution methods
  • Euler's method for ODE solving
  • Heun's method for ODE solving
  • Runge-Kutta fourth order method
  • Boundary value problems and initial value problems
  • Shooting method for boundary value problems
  • Higher order differential equation solving

Runge-Kutta fourth order method

2082 · 5 marks

Solve $\frac{dy}{dx} = x^2 + y$, with $y(0) = 1$ for $x = 1.5$, using RK fourth order method. [5]

Show solved answer

RK4 Method: dy/dx = x² + y, y(0) = 1, find y(1.5)

Given data

  • ODE: $\dfrac{dy}{dx} = f(x,y) = x^2 + y$
  • Initial condition: $x_0 = 0$, $y_0 = 1$
  • Target: $y$ at $x = 1.5$
  • Step size (chosen): $h = 0.5$, giving 3 steps: $0 \to 0.5 \to 1.0 \to 1.5$

RK4 formulas

$$k_1 = h f(x_n, y_n),\quad k_2 = h f!\left(x_n+\tfrac h2, y_n+\tfrac{k_1}{2}\right)$$ $$k_3 = h f!\left(x_n+\tfrac h2, y_n+\tfrac{k_2}{2}\right),\quad k_4 = h f(x_n+h, y_n+k_3)$$ $$y_{n+1} = y_n + \tfrac16(k_1 + 2k_2 + 2k_3 + k_4)$$


Step 1: $x_0=0 \to x_1=0.5$, $y_0=1$

$$k_1 = 0.5(0^2+1) = 0.5$$ $$k_2 = 0.5\big(0.25^2 + (1+0.25)\big) = 0.5(0.0625+1.25) = 0.65625$$ $$k_3 = 0.5\big(0.0625 + (1+0.328125)\big) = 0.5(1.390625) = 0.6953125$$ $$k_4 = 0.5\big(0.25 + (1+0.6953125)\big) = 0.5(1.9453125) = 0.9726563$$

$$y_1 = 1 + \tfrac16(0.5 + 1.3125 + 1.390625 + 0.9726563) = 1 + \tfrac16(4.1757813)$$ $$\boxed{y_1 = 1.6959635}$$


Step 2: $x_1=0.5 \to x_2=1.0$, $y_1=1.6959635$

$$k_1 = 0.5(0.25 + 1.6959635) = 0.9729818$$ $$k_2 = 0.5\big(0.5625 + (1.6959635+0.4864909)\big) = 0.5(2.7449544) = 1.3724772$$ $$k_3 = 0.5\big(0.5625 + (1.6959635+0.6862386)\big) = 0.5(2.9447021) = 1.4723511$$ $$k_4 = 0.5\big(1.0 + (1.6959635+1.4723511)\big) = 0.5(4.1683146) = 2.0841573$$

$$y_2 = 1.6959635 + \tfrac16(0.9729818 + 2.7449544 + 2.9447022 + 2.0841573)$$ $$= 1.6959635 + \tfrac16(8.7467957) = 1.6959635 + 1.4577993$$ $$\boxed{y_2 = 3.1537628}$$


Step 3: $x_2=1.0 \to x_3=1.5$, $y_2=3.1537628$

$$k_1 = 0.5,f(1.0, 3.1537628) = 0.5(1.0 + 3.1537628) = 0.5(4.1537628) = 2.0768814$$

$y_2 + k_1/2 = 3.1537628 + 1.0384407 = 4.1922035$ $$k_2 = 0.5,f(1.25, 4.1922035) = 0.5(1.5625 + 4.1922035) = 0.5(5.7547035) = 2.8773518$$

$y_2 + k_2/2 = 3.1537628 + 1.4386759 = 4.5924387$ $$k_3 = 0.5,f(1.25, 4.5924387) = 0.5(1.5625 + 4.5924387) = 0.5(6.1549387) = 3.0774693$$

$y_2 + k_3 = 3.1537628 + 3.0774693 = 6.2312321$ $$k_4 = 0.5,f(1.5, 6.2312321) = 0.5(2.25 + 6.2312321) = 0.5(8.4812321) = 4.2406161$$

$$y_3 = 3.1537628 + \tfrac16\big(2.0768814 + 2(2.8773518) + 2(3.0774693) + 4.2406161\big)$$ $$= 3.1537628 + \tfrac16(2.0768814 + 5.7547036 + 6.1549386 + 4.2406161)$$ $$= 3.1537628 + \tfrac16(18.2271397)$$ $$= 3.1537628 + 3.0378566$$ $$\boxed{y_3 = y(1.5) \approx 6.1916}$$


Result

$$y(1.5) \approx 6.1916$$

(Exact solution $y = 3e^x - x^2 - 2x - 2$ gives $y(1.5) = 3e^{1.5} - 2.25 - 3 - 2 = 13.4450 - 7.25 = 6.1950$, confirming the RK4 result to about 3 significant figures.)

Open this answer on its own page →

Boundary value problems and initial value problems

2082 · 5 marks

Explain boundary value problem with example. Describe how higher order differential equation can be solved. [2+3]

--- (a) Boundary Value Problem A Boundary Value Problem (BVP) is a differential equation together with a set of conditions specified at two or more different points (the boundaries of the domain), rather than all conditions given at a single point. In contrast, an Initial Value Problem (IVP) has ...

Open this answer on its own page →

Shooting method for boundary value problems

2080 · 10 marks

Solve the following ordinary differential equation using shooting method. $y'' + xy' - xy = 2x$ with boundary conditions $y(0) = 1$ and $y(2) = 10$ [10]

  • ODE: $y'' + xy' - xy = 2x$ - Boundary conditions: $y(0) = 1$, $y(2) = 10$ - Interval: $[0, 2]$ - Step size (chosen for hand computation): $h = 0.5$ (4 steps) - Integration method: Euler's method --- Let $y1 = y$, $y2 = y'$. Then: $$y1' = y2$$ $$y2' = y'' = 2x - xy' + xy = 2x - x y2 + x y1$$ Ini...
Open this answer on its own page →

2078 · 10 marks

What do you mean by boundary value problem? Use shootuing method, solve the equation: y'' = 6x², with y(0) = 1 and y(1) = 2 in the interval (0, 1) for y(0.5) taking h = 0.5[10]

Show solved answer

Boundary Value Problem and Shooting Method

Given

  • ODE: $y'' = 6x^2$
  • Boundary conditions: $y(0) = 1$, $y(1) = 2$
  • Interval: $(0, 1)$
  • Step size: $h = 0.5$
  • Required: $y(0.5)$

Boundary Value Problem (Definition)

A boundary value problem (BVP) is a differential equation together with conditions specified at two or more different points (the boundaries of the domain), rather than all at a single point.

General second order form: $$y'' = f(x, y, y'), \quad a \le x \le b, \quad y(a) = \alpha, \ \ y(b) = \beta$$


Shooting Method (Idea)

The shooting method converts a BVP into an initial value problem (IVP) by guessing the unknown initial slope $y'(0) = t$, integrating forward, and adjusting $t$ until the far boundary condition $y(b) = \beta$ is met. Since this ODE is linear, only two trial slopes (or the decomposition method) are needed, and the correct $t$ follows by linear interpolation.


Solution

Reduce to a first-order system with $y_1 = y$, $y_2 = y'$: $$y_1' = y_2, \qquad y_2' = 6x^2$$ with $y_1(0) = 1$, $y_2(0) = t$ (unknown).

Use Euler's method, $h = 0.5$, two steps ($x = 0, 0.5, 1.0$).

Trial 1: guess $t = t_1 = 0$

$x$$y_1$$y_2$$y_2'=6x^2$
0.01.00000.00000.0
0.5$1+0.5(0)=1.0000$$0+0.5(0)=0.0000$$6(0.25)=1.5$
1.0$1+0.5(0)=1.0000$$0+0.5(1.5)=0.75$--

So with $t_1 = 0$: $y(1) = 1.0000 \equiv \beta_1$

Trial 2: guess $t = t_2 = 1$

$x$$y_1$$y_2$$y_2'=6x^2$
0.01.00001.00000.0
0.5$1+0.5(1)=1.5000$$1+0.5(0)=1.0000$$1.5$
1.0$1.5+0.5(1)=2.0000$$1+0.5(1.5)=1.75$--

So with $t_2 = 1$: $y(1) = 2.0000 \equiv \beta_2$

Linear interpolation for correct slope

Target $\beta = 2$. Since the ODE is linear: $$t = t_1 + (t_2 - t_1)\cdot \frac{\beta - \beta_1}{\beta_2 - \beta_1} = 0 + (1-0)\cdot\frac{2 - 1}{2 - 1} = 1$$

Thus the correct slope is $t = 1$, and Trial 2 is exactly the required solution.

Read off $y(0.5)$

From Trial 2 at $x = 0.5$: $$\boxed{y(0.5) = 1.5000}$$


Verification with Exact Solution

Integrating twice: $$y' = 2x^3 + C_1, \qquad y = \frac{x^4}{2} + C_1 x + C_2$$

Apply $y(0)=1 \Rightarrow C_2 = 1$. Apply $y(1)=2 \Rightarrow \tfrac12 + C_1 + 1 = 2 \Rightarrow C_1 = \tfrac12$.

$$y = \frac{x^4}{2} + \frac{x}{2} + 1$$

Exact value: $$y(0.5) = \frac{(0.5)^4}{2} + \frac{0.5}{2} + 1 = \frac{0.0625}{2} + 0.25 + 1 = 0.03125 + 1.25 = 1.28125$$

Note: The Euler shooting result $y(0.5) = 1.5$ differs from the exact $1.28125$. This gap is due to the very large step size $h = 0.5$ used by Euler's method (only two steps). Euler's method reproduces the correct boundary values $y(0)=1$ and $y(1)=2$ exactly because the slope-parameter interpolation is exact for the linear problem, but the intermediate point $y(0.5)$ carries the local truncation error of Euler's scheme. The method-consistent answer requested by the problem (using shooting with $h=0.5$) is:

$$y(0.5) \approx 1.5$$

while the true analytic value is $1.28125$.

Open this answer on its own page →

0 · 5 marks

How boundary value problems differs from initial value problems? Discuss shooting method for solving boundary value problem. [5]

Feature Initial Value Problem (IVP) Boundary Value Problem (BVP) --------- Conditions given All conditions at a single point (initial point) Conditions given at two or more different points (boundaries) Solution direction Marches forward from initial point Must satisfy conditions at both ends sim...

Open this answer on its own page →

Euler's method for ODE solving

2080 · 5 marks

Solve the following differential equation $$\frac{dy}{dx} = 3x + \frac{y}{2}$$ with $y(0) = 1$ for $x = 0.2$ $(h = 0.1)$ using Euler's Method. [5]

  • ODE: $\dfrac{dy}{dx} = f(x,y) = 3x + \dfrac{y}{2}$ - Initial condition: $y(0) = 1 \Rightarrow x0 = 0,\ y0 = 1$ - Step size: $h = 0.1$ - Target: $y$ at $x = 0.2$ (2 steps) Euler's formula: $$y{n+1} = yn + h, f(xn, yn)$$ Iteration 1: $x0 = 0,\ y0 = 1$ $$f(0,1) = 3(0) + \frac{1}{2} = 0.5$$ $$y1 =...
Open this answer on its own page →

2078 · 5 marks

Given the equation y′=3x2+1y' = 3x^2 + 1y′=3x2+1 with y(1) = 2, estimate y(2) by Euler's Method using h = 0.2. [5]

  • $f(x,y) = y' = 3x^2 + 1$ - $x0 = 1.0$, $y0 = 2.0$ - $h = 0.2$ - Target: $y(2)$ → requires $\frac{2-1}{0.2} = 5$ steps Euler formula: $$y{n+1} = yn + h,f(xn, yn)$$ Step 1 ($x0=1.0,\ y0=2.0$): $$f = 3(1.0)^2+1 = 4 \implies y1 = 2.0 + 0.2(4) = 2.800$$ Step 2 ($x1=1.2,\ y1=2.800$): $$f = 3(1.44)+1...
Open this answer on its own page →

0 · 5 marks

Approximate the solution of y'=2x+y, y(0)=1 using Euler's method with step size of 0.1. Approximate the value of y(0.4). [5]

Given data: - ODE: $y' = f(x,y) = 2x + y$ - Initial condition: $y(0) = 1$, so $x0 = 0$, $y0 = 1$ - Step size: $h = 0.1$ - Target: $y(0.4)$ (requires 4 steps) Euler's formula: $y{n+1} = yn + h \cdot f(xn, yn)$, with $f(x,y) = 2x + y$. Step 1: $y(0.1)$ $$f(0, 1) = 2(0) + 1 = 1$$ $$y1 = 1 + 0.1(1) =...

Open this answer on its own page →

Heun's method for ODE solving

2079 · 5 marks

Find the approximate value of y when x = 0.6 of $dy/dx = 1-2xy$, given that y = 0 when x = 0 with h=0.2 using Heun's method. [5]

  • ODE: $\frac{dy}{dx} = f(x,y) = 1 - 2xy$ - Initial condition: $y = 0$ at $x = 0$ - Step size: $h = 0.2$ - Target: $y$ at $x = 0.6$ (requires 3 steps) Heun's formulas: $$y^{n+1} = yn + h,f(xn, yn)$$ $$y{n+1} = yn + \frac{h}{2}\left[f(xn, yn) + f(x{n+1}, y^{n+1})\right]$$ $$k1 = f(0,0) = 1 - 0 = ...
Open this answer on its own page →