Solving systems of equations
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.
In this minimal example, the following linear equation is solved for a:
Interactive code example
The function is called as
options is an object with the following properties:
objectA description of the quantities used in the function (as described below in detail). The property names are used as names for the quantities.
arrayAn array of strings that contain the equations. Each equation must contain exactly one equality sign.
0.001The step width used to numerically determine derivatives.
100The maximal number of iterations used by the algorithm.
1e-14The (default) relative tolerance that determines the convergence of a quantity. The algorithm terminates its iterations as soon as all quantities are converged.
'real'The type of numbers used in the calculation.
Each quantity must have a type and, depending on the type, various additional properties:
'constant'The type of the quantity. Dependent quantities are changed by the algorithm, while constants are not.
complexNumberThe 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.
0.001The step size used to numerically determine finite difference derivatives within the algorithm.
1e-14If the relative change of a quantity between two iteration steps is smaller than this value, the value is considered as being converged.
complexNumberThe value of a constant
functionThe number of iterations used until the result converged.
arrayAn 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.
objectAn object that contains the resulting values of all quantities.