Linear Algebra - Ordinary Differential Equations

Overview

Linear Algebra can be used to numerically solve ordinary differential equations. Consider the following sample equation
{% d^2x(t)/ dt^2 = a(t,x) \times dx(t)/dt + b(t,x) \times x(t) %}
with a set of boundary conditions
{% x(0) = x_0 \\ dx(0)/dt = x_1 \\ d^2 x(0)/dt^2 = x_2 %}
The key to modeling this equation would be to re-express the equation as a set of linear algebra equations. First, we form the following vector.
{% \begin{bmatrix} \ddot{x}(t+1) \\ \dot{x}(t+1) \\ x(t+1) \\ \end{bmatrix} %}
Then we express the differential equation as
{% \begin{bmatrix} \ddot{x}(t+1) \\ \dot{x}(t+1) \\ x(t+1) \\ \end{bmatrix} = \begin{bmatrix} 0 & a(t,x) & b(t,x) \\ \Delta t & 1 & 0\\ 0 & \Delta t & 1 \\ \end{bmatrix} \begin{bmatrix} \ddot{x}(t) \\ \dot{x}(t) \\ x(t) \\ \end{bmatrix} %}

Implementation

The following code utilizes the linear algebra library to simulate the evolution of the second order differential equation
{% \frac{d^x(t)}{dt^2} = -x(t) %}
let la = await import('/code/linear-algebra/v1.0.0/linear-algebra.mjs'); let k = 1; let dt = 0.01; let x0=1; let x1=0; let x2=0;; let t = 0; let a = (t, vector) =>0; let b = (t, vector)=>-1; let vector = [[x2],[x1],[x0]]; let mult = [[0,a(t, vector[2][0]),b(t, vector[2][0])],[dt, 1, 0],[0, dt, 1]]; let data = []; for(let i=0;i<1000;i++){ vector=la.multiply(mult, vector); data.push({t:t, x:vector[2][0]}); mult=[[0,a(t, vector[2][0]),b(t, vector[2][0])],[dt, 1, 0],[0, dt, 1]]; t+=dt; }