Solves a general, nonlinear system F(x) = 0. For over determined systems (more equations than variables), the system is solved in the least squares sense. |
||
Syntax |
x, ph, oh = NLSolve(sys, init, N, maxIter, tol, userdata) x, ph, oh = NLSolve(sys, jac, init, N, maxIter, tol, userdata) |
|
Arguments |
Name |
Description |
sys |
Name of the user defined function where the system is defined. Must be a string. It must return a real vector of equation residuals. If the vector has one element, then a real number may be returned instead. See Comments section. |
|
jac |
A string containing the function name that implements the Jacobian matrix of the system. If the matrix has one element, then a real number may be returned instead. See Comments section. |
|
init |
Initial estimates for the variables. |
|
N |
A one element vector containing the number of equations in the system. If it is empty or omitted, it defaults to the length of init. For overdetermined systems, it needs to be specified and must be equal to the number of equations. |
|
maxIter (optional) |
A one element vector containing the maximum number of iterations allowed. If it is empty or omitted it defaults to 200. |
|
tol (optional) |
A one element vector containing the convergence tolerance for the algorithm. Defaults to 1.0E-6. |
|
userdata (optional) |
Data matrix that is passed to the user defined function at each iteration. This can be used to supply data that are defined outside the function. |
|
Output |
Name |
Description |
x |
The solution to the system. It will have the same dimension as init. |
|
ph |
A matrix containing the parameter history. Each column of the matrix contains the values for an iteration step. |
|
oh |
A row vector containing the objective function history at each iteration step. |
|
Example 1 |
Find the value of x such that x = cos(x). The problem can be formulated as a set of two equations as: y = x and y = cos(x). Rewritten in the form F(x) = 0, this becomes y – x = 0 y – cos(x) = 0 |
|
|
Syntax// Define the system in a function function Sys(p, d) { // p is the estimate of the intersection // p(1) = x // p(2) = y // compute the residual for each equation in the system out = [] out(1) = p(2) – p(1) out(2) = p(2) - Cos(p(1))
return out }
// perform fit initial = [1, Cos(1)] // estimate of intersection p = NLSolve("Sys", initial, [2], [50], [1.0e-4]) print(p) |
|
|
ResultThe solution to the system, where the value where x equals cos(x): |
|
|
p = 0.73909 0.73909 |
|
Example 2 |
Find an approximate intersection of three circles:x2 + y2 = r2 (x-2)2 + y2 = r2 (x-1)2 + (y-3)2 = r2 There are three equations but two unknowns (x,y). Hence, it will be solved as a least squares problem.The system is rewritten in the form F(x) = 0 asx2 + y2 - r2 = 0 (x-2)2 + y2 - r2 = 0 (x-1)2 + (y-3)2 - r2 = 0 |
|
Syntax// Define the function function Residuals(p) { // p is the estimate of the intersection // p(1) = x // p(2) = y // compute the residual for each equation in the system r = 2 // Given value of the radii out = []; out(1) = p(1)^2 + p(2)^2 - r^2 out(2) = (p(1)-2)^2 + p(2)^2 - r^2 out(3) = (p(1)-1)^2 + (p(2)-3)^2 - r^2
return out } // perform fit initial = [0.5, 1.0] // estimate of intersection p = NLSolve("Residuals", initial, [3], [50], [1.0e-6]) print ("Intersection Point =", p) |
||
ResultThe approximate intersection points of the circles. |
||
p = 0.99999 1.4628 |
||
Example 3 |
Modify the previous example to use the analytical Jacobian matrix. |
|
Syntax function Jacobian(p) { out = [] out(1,1) = 2*p[1] out(1,2) = 2*p[2] out(2,1) = 2*(p[1]-2) out(2,2) = 2*p[2] out(3,1) = 2*(p[1]-1) out(3,2) = 2*(p[2]-3) return out }
// perform the fit initial = [0.5, 1.0] // estimate of intersection p = NLSolve("Residuals", "Jacobian", initial, [3], [100], [1.0e-6]) print ("Intersection Point =", p) |
||
Results |
||
The approximate intersection points of the circles. p = 0.99999 1.4628 |
||
Comments |
The user defined function gets called by the solver until all the residuals are within the given tolerance. It must return the residuals in a vector. It also needs to accept a vector of the same size as init for the parameter values and this must be the first input. A optional matrix of user data may be supplied as a second input. If the function that computes the Jacobian matrix is omitted, the matrix will be computed internally with numerical derivatives. The solution vector x will have the same orientation as the initial condition vector init. However, the vector for the parameters that is passed to the user functions will be a column vector. |
|
See Also: |