Solving systems of equations
The function kontrast.solveEquations
uses a
secant
Newton-Raphson algorithm for multidimensional root
finding problems. This method works well for linear systems, as it
descents along the (numerically determined) gradient of the
function. With a good initial guess it can work for more complicated
sets of equations. Please include checks in your code to confirm
whether the output is reasonable.
Minimal example
In this minimal example, the following linear equation is solved for a:
Interactive code example
Input
The function is called as
kontrast.solveEquations(options)
, where
options
is an object with the following properties:
-
quantities
:object
A description of the quantities used in the function (as described below in detail). The property names are used as names for the quantities. -
equations
:array
An array of strings that contain the equations. Each equation must contain exactly one equality sign. -
derivativeStep
:positiveNumber
(default value:
)0.001
The step width used to numerically determine derivatives. -
maxIterationCount
:positiveInteger
(default value:
)100
The maximal number of iterations used by the algorithm. -
defaultConvergenceTolerance
:positiveNumber
(default value:
)1e-14
The (default) relative tolerance that determines the convergence of a quantity. The algorithm terminates its iterations as soon as all quantities are converged. -
numberType
:'real'
|'complex'
(default value:
)'real'
The type of numbers used in the calculation.
Quantities
Each quantity must have a type and, depending on the type, various additional properties:
-
type
:'dependent'
|'constant'
The type of the quantity. Dependent quantities are changed by the algorithm, while constants are not. -
initial
:number
|complexNumber
The initial value for the dependent quantity. For non-linear system of equations, a good choice for the initial value is crucial for the algorithm to succeed. -
stepSize
:number
(default value:
)0.001
The step size used to numerically determine finite difference derivatives within the algorithm. -
convergenceTolerance
:positiveNumber
(default value:
)1e-14
If the relative change of a quantity between two iteration steps is smaller than this value, the value is considered as being converged. -
value
:number
|complexNumber
The value of a constant
Output
-
iterationCount
:function
The number of iterations used until the result converged. -
equations
:array
An array that contains each equation and the corresponding residual value after the algorithm finished. The residual should be close to zero, if the algorithm succeeded. -
quantities
:object
An object that contains the resulting values of all quantities.