5 Solution Of Ordinary Differential Equations

Numerical Method Ā· Unit 5 Ā· 8 hrs

Solution of Ordinary Differential Equations

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

Solution of Ordinary Differential Equations in short

An ordinary differential equation (ODE) specifies how a function changes rather than giving the function itself; this chapter develops step-by-step numerical methods that reconstruct the function from its rate of change, given a starting value.

Solution of Ordinary Differential Equations: notes

Review of differential equations

A differential equation relates a function to one or more of its derivatives, for example $\dfrac{dy}{dx} = f(x,y)$. Its order is the order of the highest derivative appearing in it: $y'=f(x,y)$ is first order, $y''+2y'-3y=6x$ is second order. A solution is any function $y(x)$ that satisfies the equation identically; the general solution of an $n$th-order equation contains $n$ arbitrary constants, and fixing those constants with $n$ extra conditions gives a particular solution. Most differential equations arising from real models (varying coefficients, nonlinear terms) have no elementary closed-form solution, which is why numerical methods that step the solution forward from a known starting value are needed.

Initial value problem

An initial value problem (IVP) is a first-order differential equation together with the value of the solution at one point, written $y'=f(x,y)$, $y(x_0)=y_0$. This single condition, together with the equation, determines a unique solution curve (whenever $f$ is well-behaved), and every method in this chapter takes exactly this form as input: a right-hand-side function $f(x,y)$, a starting point $(x_0,y_0)$, and a step size $h$ with which to advance $x$.

Taylor series method

The Taylor series method expands the unknown solution about the current point using its own differential equation to generate the higher derivatives: $$y(x_0+h) = y(x_0) + h,y'(x_0) + \frac{h^2}{2!}y''(x_0) + \frac{h^3}{3!}y'''(x_0) + \cdots,$$ where $y'(x_0)=f(x_0,y_0)$ directly, and $y''=\dfrac{d}{dx}f(x,y)$, $y'''=\dfrac{d^2}{dx^2}f(x,y)$ are obtained by repeatedly differentiating $f$ using the chain rule (since $y$ itself depends on $x$).

Worked example. Solve $y'=3x^2$, $y(1)=1$, for $y(2)$ using the first four terms. Here $y'=3x^2$, $y''=6x$, $y'''=6$, all independent of $y$. At $x_0=1$: $y'(1)=3$, $y''(1)=6$, $y'''(1)=6$. With $h=2-1=1$, $$y(2) \approx 1 + 1(3) + \frac{1^2}{2}(6) + \frac{1^3}{6}(6) = 1+3+3+1 = 8.$$ Since $y'=3x^2$ integrates exactly to $y=x^3+C$ with $C=0$, the true solution is $y=x^3$ and $y(2)=8$ exactly: four Taylor terms exactly reproduce a cubic, just as in polynomial interpolation.

Picard's method

Picard's method of successive approximations rewrites the IVP $y'=f(x,y)$, $y(x_0)=y_0$ as the integral equation $y(x) = y_0 + \int_{x_0}^{x} f(t,y(t)),dt$, and generates successively better approximations by substituting the previous approximation for $y(t)$ inside the integral: $$y^{(k+1)}(x) = y_0 + \int_{x_0}^{x} f\big(t, y^{(k)}(t)\big),dt, \qquad y^{(0)}(x) = y_0.$$ Each iteration integrates a fully known function of $t$, so it can be carried out by ordinary (symbolic or numerical) integration.

Worked example. Solve $y'=1+xy$, $y(0)=1$, for $y(0.2)$. With $y^{(0)}=1$, $$y^{(1)}(x) = 1 + \int_0^x (1+t),dt = 1+x+\frac{x^2}{2} ;\Rightarrow; y^{(1)}(0.2) = 1.220.$$ $$y^{(2)}(x) = 1+\int_0^x\left(1+t\Big(1+t+\frac{t^2}{2}\Big)\right)dt = 1+x+\frac{x^2}{2}+\frac{x^3}{3}+\frac{x^4}{8} ;\Rightarrow; y^{(2)}(0.2) = 1.22287.$$ $$y^{(3)}(0.2) = 1.22289.$$ The successive approximations are converging rapidly; correct to three decimal places, $y(0.2) = 1.223$, matching a high-accuracy numerical solution of the same IVP.

What this unit covers

  • Review of differential equations
  • Initial value problem
  • Taylor series method
  • Picard's method
  • Euler's method and its accuracy
  • Heun's method
  • Runge-Kutta methods
  • Solving System of ordinary differential equations
  • Solution of the higher order equations
  • Boundary value problems
  • Shooting method and its algorithm

Review of differential equations

2081 Ā· 5 marks

What is differential equation? Differentiate between ODE and PDE with example. [5]

Show solved answer

Differential Equation, ODE and PDE

Differential Equation

An equation which uses differential calculus to express relationship between variables is known as a differential equation.

In other words, a differential equation is an equation that contains one or more derivatives of a dependent variable with respect to one or more independent variables.


Types of Differential Equations

Differential equations are of two types:

1. Ordinary Differential Equation (ODE)

A differential equation with a single independent variable (i.e., the quantity with respect to which the dependent variable is differentiated) is called an Ordinary Differential Equation (ODE).

Example:

$$\frac{dy}{dx} + 2y = x$$

Here, y is the dependent variable and x is the only independent variable. Since there is only one independent variable, this is an ODE.

Another example:

$$\frac{d^2y}{dx^2} + 3\frac{dy}{dx} + y = 0$$


2. Partial Differential Equation (PDE)

A differential equation with more than one independent variable is called a Partial Differential Equation (PDE).

Example:

$$\frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} = 0$$

Here, u is the dependent variable and x, y are two independent variables. Since there are more than one independent variable, this is a PDE.


Comparison Table: ODE vs PDE

FeatureODEPDE
Independent VariablesSingle independent variableMore than one independent variable
Derivative TypeOrdinary derivativesPartial derivatives
ComplexityRelatively simplerMore complex
Notation$\frac{dy}{dx}$, $\frac{d^2y}{dx^2}$$\frac{\partial u}{\partial x}$, $\frac{\partial^2 u}{\partial x^2}$
Example$\frac{dy}{dx} + 2y = x$$\frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} = 0$

Note: The relationship between dependent and independent variables that satisfies a differential equation is called the solution of the differential equation.

Open this answer on its own page →

Runge-Kutta methods

2081 Ā· 5 marks

Solve $\frac{dy}{dx} = \frac{x}{y}$, $y(0) = 1$, at $x = 0.4$ using Runge-Kutta's 4th order method. [5]

Show solved answer

Runge-Kutta 4th Order Method

Given Data

$$\frac{dy}{dx} = f(x,y) = \frac{x}{y}, \quad y_0 = 1 \text{ at } x_0 = 0, \quad \text{find } y(0.4)$$

Step size: $h = 0.4$ (single step).

RK4 Formulas

$$k_1 = h f(x_0, y_0)$$ $$k_2 = h f\left(x_0+\tfrac{h}{2}, y_0+\tfrac{k_1}{2}\right)$$ $$k_3 = h f\left(x_0+\tfrac{h}{2}, y_0+\tfrac{k_2}{2}\right)$$ $$k_4 = h f\left(x_0+h, y_0+k_3\right)$$ $$y_1 = y_0 + \tfrac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4)$$

Calculation

$k_1$: $$k_1 = 0.4 \cdot f(0, 1) = 0.4 \cdot \frac{0}{1} = 0$$

$k_2$: $$k_2 = 0.4 \cdot f(0.2, \ 1+0) = 0.4 \cdot \frac{0.2}{1} = 0.08$$

$k_3$: $$k_3 = 0.4 \cdot f(0.2, \ 1+0.04) = 0.4 \cdot \frac{0.2}{1.04} = 0.4 \cdot 0.192308 = 0.076923$$

$k_4$: $$k_4 = 0.4 \cdot f(0.4, \ 1+0.076923) = 0.4 \cdot \frac{0.4}{1.076923} = 0.4 \cdot 0.371429 = 0.148571$$

Final Result

$$y_1 = 1 + \frac{1}{6}\big(0 + 2(0.08) + 2(0.076923) + 0.148571\big)$$

$$= 1 + \frac{1}{6}\big(0 + 0.16 + 0.153846 + 0.148571\big)$$

$$= 1 + \frac{1}{6}(0.462418) = 1 + 0.077070$$

$$\boxed{y(0.4) \approx 1.0771}$$

Verification (Exact Solution)

Separating variables: $y,dy = x,dx \Rightarrow \tfrac{y^2}{2} = \tfrac{x^2}{2} + C$. With $y(0)=1$, $C = \tfrac12$, so $y = \sqrt{1+x^2}$.

At $x=0.4$: $y = \sqrt{1.16} = 1.07703$.

The RK4 result $1.0771$ matches the exact value very closely.

Open this answer on its own page →

2079 Ā· 5 marks

Solve the following differential equation for $0 \leq x \leq 1$ taking $h = 0.5$ using Runge Kutta 4th order method.

$$y'(x) + y = 3x \quad \text{with} \quad y(0) = 2$$

[5]

Show solved answer

Runge-Kutta 4th Order Method

Given

  • ODE: $y'(x) + y = 3x \Rightarrow y' = f(x,y) = 3x - y$
  • Initial condition: $y(0) = 2$
  • Step size: $h = 0.5$
  • Interval: $0 \le x \le 1$
  • Number of steps: 2 (from $x=0$ to $x=1$)

RK4 Formulas

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

with $f(x,y) = 3x - y$.


Solution

Iteration 1: $x_0 = 0$, $y_0 = 2$

$$k_1 = 0.5,(3(0) - 2) = 0.5(-2) = -1.0$$

$$k_2 = 0.5,f(0.25,\ 2 - 0.5) = 0.5,(0.75 - 1.5) = 0.5(-0.75) = -0.375$$

$$k_3 = 0.5,f(0.25,\ 2 - 0.1875) = 0.5,(0.75 - 1.8125) = 0.5(-1.0625) = -0.53125$$

$$k_4 = 0.5,f(0.5,\ 2 - 0.53125) = 0.5,(1.5 - 1.46875) = 0.5(0.03125) = 0.015625$$

$$y_1 = 2 + \tfrac{1}{6}(-1.0 + 2(-0.375) + 2(-0.53125) + 0.015625)$$ $$= 2 + \tfrac{1}{6}(-1.0 - 0.75 - 1.0625 + 0.015625) = 2 + \tfrac{1}{6}(-2.796875)$$ $$= 2 - 0.466146 = \boxed{1.533854}$$

$$y(0.5) \approx 1.5339$$

Iteration 2: $x_1 = 0.5$, $y_1 = 1.533854$

$$k_1 = 0.5,f(0.5,\ 1.533854) = 0.5,(1.5 - 1.533854) = 0.5(-0.033854) = -0.016927$$

$$k_2 = 0.5,f(0.75,\ 1.533854 - 0.008464) = 0.5,(2.25 - 1.525390) = 0.5(0.724610) = 0.362305$$

$$k_3 = 0.5,f(0.75,\ 1.533854 + 0.181153) = 0.5,(2.25 - 1.715007) = 0.5(0.534993) = 0.267497$$

$$k_4 = 0.5,f(1.0,\ 1.533854 + 0.267497) = 0.5,(3.0 - 1.801351) = 0.5(1.198649) = 0.599325$$

$$y_2 = 1.533854 + \tfrac{1}{6}(-0.016927 + 2(0.362305) + 2(0.267497) + 0.599325)$$ $$= 1.533854 + \tfrac{1}{6}(-0.016927 + 0.724610 + 0.534993 + 0.599325)$$ $$= 1.533854 + \tfrac{1}{6}(1.842001) = 1.533854 + 0.307000$$ $$= \boxed{1.840854}$$

$$y(1.0) \approx 1.8409$$


Summary Table

$x$$y$
$0.0$$2.0000$
$0.5$$1.5339$
$1.0$$1.8409$

Check (exact): $y = 3x - 3 + 5e^{-x}$; at $x=1$: $y = 0 + 5e^{-1} = 1.8394$. RK4 value $1.8409$ agrees closely.

Open this answer on its own page →

2075 Ā· 5 marks

From the following differential equation estimate y(1) using RK 4th order method.

$$\frac{dy}{dx} + 2x^2y = 4 \quad \text{with} \quad y(0) = 1$$

[Take $h = 0.5$] [5]

  • ODE: $\dfrac{dy}{dx} + 2x^2 y = 4 \Rightarrow \dfrac{dy}{dx} = 4 - 2x^2 y = f(x,y)$ - Initial condition: $x0 = 0,\ y0 = 1$ - Step size: $h = 0.5$ - Target: $y(1)$ → requires 2 steps ($0 \to 0.5 \to 1.0$) RK4 formulas: $$y{n+1} = yn + \tfrac{h}{6}(k1 + 2k2 + 2k3 + k4)$$ $$k1 = f(0, 1) = 4 - 2(0)...
Open this answer on its own page →

Taylor series method

2080 Ā· 5 marks

Approximate the solution of $y' = 3x^2$, $y(1) = 1$ using Taylor's series method using first four terms. Approximate the value of $y(2)$.

$$y' = 3x^2, \quad y(1) = 1$$

[5]

  • ODE: $y' = 3x^2$ - Initial condition: $y(1) = 1$, so $x0 = 1$, $y0 = 1$ - Target: $y(2)$ using first four terms of Taylor's series - Step: $h = x - x0 = 2 - 1 = 1$ $$y(x) = y(x0) + (x-x0)\frac{y'(x0)}{1!} + (x-x0)^2\frac{y''(x0)}{2!} + (x-x0)^3\frac{y'''(x0)}{3!} + \cdots$$ $$y' = 3x^2$$ $$y'' ...
Open this answer on its own page →

Heun's method

2080 Ā· 5 marks

Write down the program for solving ordinary differential equation using Heun's method. [5]

Heun's method is a predictor-corrector method (also called the improved Euler's method) for solving ODEs of the form: $$\frac{dy}{dx} = f(x, y), \quad y(x0) = y0$$ The core formula is: $$y{n+1} = yn + \frac{h}{2}(m1 + m2)$$ Where: - $m1 = f(xn,\ yn)$ (slope at the beginning of the interval) -

Open this answer on its own page →

2079 Ā· 5 marks

Write down the program for solving ordinary differential equation using Heun's method. [5]

Heun's method is a predictor-corrector method (also called the improved Euler's method) for solving ODEs of the form: $$\frac{dy}{dx} = f(x, y), \quad y(x0) = y0$$ The formula is: $$y{n+1} = yn + \frac{h}{2}(m1 + m2)$$ Where: - $m1 = f(xn, yn)$ (slope at the beginning of the interval) -

Open this answer on its own page →

2078 Ā· 5 marks

Solve the following differential equation for $1 \leq x \leq 2$, taking $h = 0.25$ using Heun’s method. $y'(x) + x^2y = 3x$, with $y(1) = 1$. [5]

  • ODE: $y'(x) + x^2 y = 3x$, so $y'(x) = 3x - x^2 y = f(x,y)$ - Initial condition: $y(1) = 1$ - Step size: $h = 0.25$ - Interval: $1 \le x \le 2$ - Number of steps: $(2-1)/0.25 = 4$ $$y{i+1} = yi + \frac{h}{2}(m1 + m2)$$ where $m1 = f(xi, yi)$ and $m2 = f(xi + h,\ yi + h,m1)$. ---
Open this answer on its own page →

Solution of the higher order equations

2078 Ā· 10 marks

Higher-Order Differential Equations

A higher-order differential equation is a differential equation in which the highest derivative of the dependent variable is of order two or more. The given equation $$\frac{d^2y}{dx^2} + 3\frac{dy}{dx} + 5y = 0$$ is a second-order ODE (highest derivative is $\frac{d^2y}{dx^2}$). The standard tec...

Open this answer on its own page →

Euler's method and its accuracy

2077 Ā· 5 marks

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

  • 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) $$y{n+1} = yn + h \cdot f(xn, yn), \qquad f(xn,yn) = 2xn + yn$$ Step 1: $x0 = 0,\ y0 = 1$ $$f(0, 1) = 2(0) + 1 = 1$$ $$y1 = 1 + 0.1(1) = 1.1$$ Step 2: ...
Open this answer on its own page →

Shooting method and its algorithm

2077 Ā· 5 marks

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

Aspect Initial Value Problem (IVP) Boundary Value Problem (BVP) --------- Conditions All conditions are specified at a single point (initial point) Conditions are specified at two or more different points (boundaries) Solution approach Can be solved by marching forward step by step Cannot be solv...

Open this answer on its own page →

2075 Ā· 10 marks

What is initial value problem and boundary value problem? Write an algorithm and program to solve the boundary value problem using shooting method.[10]

An Initial Value Problem is a differential equation in which all the conditions (values of the dependent variable and its derivatives) are specified at a single point (the initial point). General Form: Here, the value of y is known at x = xā‚€. The solution is found by marching forward from this si...

Open this answer on its own page →