This website uses small doses of Javascript, like in user interaction with animations as discussed here.
But it can also used to do some pretty complex math.
The sections on state space analysis of airplane stability and control models routinely use matrix equations. Solving these equations goes beyond the capa­bili­ties of the typical pocket calculator.
Having done this work by hand in my younger years,
and later in life by the likes of
Matlab©
and its precursors and successors,
it is with considerable relief that I find that any
simple browser can do these calculations today.
The language of choice is Javascript,
and the library of choice is
math.js.
However, as seems unavoidable in the computing world,
both have a surprising history of quirks.
Some caveats and workarounds are given below.
Javascript has some native capabilities for everyday
math and arithmetic,
but it soon runs out of steam when the numbers get complex,
and when the equations get organised in matrices.
Statements like
a = Math.sqrt(2)
are valid for integers
and floating point numbers, but not for complex numbers or matrices.
These Math functions with a capital "M"
are part of the Javascript language definition, but they must be preceded
by the Math prefix unless special tricks are used.
A universally accepted library extending
Javascript to more complicated math is called math.js.
Interestingly, the functions from this library are
preceded by the standard keyword math,
but this time with a lowercase "m".
The standard Javascript Math.sqrt() will return
NaN when
the argument is negative, like for
Math.sqrt(−4).
By con­trast, math.sqrt(−4)
will dutifully return the complex root 2 i.
The math library also supports matrix operations
and many other useful functions.
There are several ways of "installing"
math.js on a computer so it can be used
by browsers and other pro­grams or from the command line.
But it can also be used as an external library,
by referencing the math.js website.
Using math.js on a web page like this
has its dangers. If it relies on "installed" code,
then it may not run on someone else's computer,
or they may run an incompatible version.
If the external option is used, the code may not run offline.
My preferred solution is to keep a trusted copy of the
Javascript code in the same directory
as the web pages using it, whether online or offline.
On this website, the math.js code is
located here.
The software world has a long and sad history of ( near ) free software disappearing behind paywalls by com­panies buying up or otherwise killing the competition, and of unsollicited changes in syntax breaking old code when running a later version.
For the moment it looks like Javascript and math.js
are safer than most, but precisely in the code used below
there was a change in the interface of the math.eigs()
function that broke existing code at the time.
This website uses math.js version 15.2.0
in a local copy, in an attempt to keep its functionality
unaffected by the vagaries of the outside world.
This website uses matrix eigenvalues and eigenvectors
in state space analysis of aircraft.
It will be helpful to show the relevant
Math.js syntax, and some of its idiosyncrasies.
A matrix can be defined as a standard two-dimensional Javascript array :
let A = [ [ 1 2 ]
[ 3 4 ] ];
|
(1) |
The eigenvalues and eigenvectors are obtained by
a single call to the relevant Math.js routine :
let E = eigs( A); |
(2) |
The output is an array of objects, one per eigenvalue. These objects each have two proper­ties, called value and eigenvector. It is best to ignore the first property, because it is repeated in the second. The eigenvector property itself has two array properties of its own, one of them again called value, and the other one vector.
It would have been nice if the outputs had been an array of eigenvalues and a matrix of eigenvectors, but no such luck. We will show later how to bring the output into this more conventional matrix-vector form.
However, for our typically small matrices it is often sufficient, and indeed much simpler to park each of the eigen­vectors into its own vector variable like this :
let eig_val_0 = E[0].eigenvectors.value;
let eig_vec_0 = E[0].eigenvectors.vector;
let eig_val_1 = E[1].eigenvectors.value;
let eig_vec_1 = E[1].eigenvectors.vector;
( etc.)
|
(3) |
The index [ 0 ] selects the first eigenvalue and its associated eigenvector. In keeping with the unfortunate "C" language heritage, the indices of an array of length N run from 0 to N − 1 in Javascript.
There are as many eigenvalues and eigenvectors as the size N of the system matrix, but the ones having an imaginary component come in complex pairs, a ± i b.
The eigs( ) function lists the eigenvalues sorted from small to large by their absolute values. In complex pairs the one with the positive imaginary part is listed first. This is an arbitrary but reasonable choice. It is up to the human user to physically interpret each eigenvalue and its eigenvector, and often the slowest phenomena ( lowest eigen­values ) are a good place to start.
Each eigenvector has its own distinctive "shape", namely the ratio between its elements. However, the overall size is arbitrary. If [ 0, 1 ] is an eigenvector, then so is [ 0, 2 ]. For instance, in a first order system ( like a damper ), all elements of the eigenvector will decay at the same rate with time, and in fact [ 0, 2 ] will soon decay to [ 0, 1 ] and further, without changing the shape.
Since the size of an eigenvector is arbitrary,
we can scale it to any value for easier human interpretation.
Some packages scale the eigenvector to have an overall
"vector length" of 1.
This helps to give readable numerical outputs,
but apart from this this it is as arbitrary a choice as any.
Math.js as far as I can see does not seem
to bother giving the eigenvectors any specific scale.
It is often the case that one of the elements in the state has an easy physical interpretation, like an externally visible position or angle. In such cases, it is a good option to scale the vector to give 1 in this element.
Suppose we wish to scale the vector relative to its element number [ 4 ] ( with unfortunately, array index [ 3 ] ). Continuing the example given in (3), we only need to do this :
// scale EV_0 to its 4th element ( index [3] )
EV_0 = math.divide( EV_0, EV_0[3] );
|
(4) |
In this example we keep using the same variable name here because after all it still is the same eigenvector, but a new name can be used too. The reason to use math.divide ( ) instead of the simple fractional slash is that this allows the elements to be complex numbers. Unlike in Matlab ©, the simple slash does not divide complex numbers in standard Javascript.
Eigenvalues can come in complex pairs, and then they will typically have complex eigenvectors. Complex eigenvalues describe oscillating systems. The real part of the eigenvalue represents a decay with time ( if it is negative; otherwise it is a growth rate ). The imaginary part is the oscillation frequency of the motion, expressed in radians per second.
The elements of the eigenvector do not necessarily oscillate in sync. They may be out of "phase" with each other.
Discuss ( and plot ) rotating eigenvectors on a separate page ? Becasue that is not math, it's physics. Use the the damped pendulum to highlight the more than 90° phase between an element and its "derivative" ( if the derivative has its own decay ).
It can be useful to organize the eigenvectors into a column matrix.
There are generic ways of converting the eigenvector
object to this conventional form, by first
"destructuring" it with the ( { } ) syntax
and an "arrow function" arg=>arg.vector
to extract an array of just the vector 's,
and then feeding this array to math.matrixFromColumns()
using the spread operator "..." ( three dots )
to indicate "run over all the items of the array".
But for our typically small matrices,
it may be more readable to just assemble the matrix
from the individual vector variables found earlier in (3) :
let EV = math.matrixFromColumns( eig_vec_0, eig_vec_1, ( etc. ) ); |
(4) |
The procedure with the function call seems a bit contrived, but it is needed because assembling a matrix from vectors in the obvious way of pushing the arrays into a surrounding array will put the vectors into the matrix's rows instead of in its columns.
Unlike in Matlab©, Javascript
math vectors are neither row nor column vectors.
They just "are". The math.js library routines choose
the most likely interpretation
when they are passed a vector, unless the user forces
their hand by definining the input vector
as a 2D matrix with either specific row or column dimension.
This default behaviour is normally a good thing, but in the case of assembling a matrix from columns it requires the above bit of extra care.
modal coordinates ( q or α )
Dahleh