zerosSysEqn¶
Solves a system of n nonlinear equations \(f(x)=0\) using a modified Powell hybrid algorithm.
Synopsis¶
zerosSysEqn (fcn, n)
Required Arguments¶
- void
fcn
(n
,x[]
,f[])
(Input/Output) - User-supplied function to evaluate the system of equations to be solved,
where
n
is the size ofx
andf
,x
is the point at which the functions are evaluated, andf
contains the computed function values at the pointx
. - int
n
(Input) - The number of equations to be solved and the number of unknowns.
Return Value¶
The vector x that is a solution of the system of equations. If no solution
can be computed, then None
is returned.
Optional Arguments¶
xguess
, float[]
(Input)Array with n components containing the initial estimate of the root.
Default:
xguess
= 0jacobian
(n
,x
,fjac
) (Input/Output)- User-supplied function to evaluate the Jacobian, where
n
is the number of components inx
,x
is the point at which the Jacobian is evaluated, andfjac
is the computed n × n Jacobian matrix at the pointx
. Note that each derivative \(\partial f_i/\partial x_j\) should be returned infjac
[(i-
1)×n
+j-
1]. errRel
, float (Input)Stopping criterion. The root is accepted if the relative error between two successive approximations to this root is less than
errRel
.Default: \(\mathit{errRel}=\sqrt{\varepsilon}\) where ɛ is the machine precision
maxItn
, int (Input)The maximum allowable number of iterations.
Default:
maxItn
= 200fnorm
(Output)Scalar with the value
\[f_1^2 + \ldots + f_n^2\]at the point
x
.
Description¶
The function zerosSysEqn
is based on the MINPACK subroutine HYBRDJ,
which uses a modification of the hybrid algorithm due to M.J.D. Powell. This
algorithm is a variation of Newton’s method, which takes precautions to
avoid undesirable large steps or increasing residuals. For further
description, see Moré et al. (1980).
Examples¶
Example 1¶
The following 2 × 2 system of nonlinear equations
is solved.
from numpy import *
from pyimsl.math.zerosSysEqn import zerosSysEqn
from pyimsl.math.writeMatrix import writeMatrix
def fcn(n, x, f):
f[0] = x[0] + x[1] - 3.0
f[1] = x[0] * x[0] + x[1] * x[1] - 9.0
n = 2
x = zerosSysEqn(fcn, n)
writeMatrix("The solution to the system is", x)
Output¶
The solution to the system is
1 2
0 3
Example 2¶
The following 3 × 3 system of nonlinear equations
is solved with the initial guess (4.0, 4.0, 4.0).
from __future__ import print_function
from numpy import *
from pyimsl.math.zerosSysEqn import zerosSysEqn
from pyimsl.math.writeMatrix import writeMatrix
def fcn(n, x, f):
f[0] = x[0] + exp(x[0] - 1.0) + (x[1] + x[2]) * (x[1] + x[2]) - 27.0
f[1] = exp(x[1] - 2.0) / x[0] + x[2] * x[2] - 10.0
f[2] = x[2] + sin(x[1] - 2.0) + x[1] * x[1] - 7.0
n = 3
maxitn = 100
err_rel = 0.0001
xguess = [4.0, 4.0, 4.0]
fnorm = []
x = zerosSysEqn(fcn, n,
errRel=err_rel,
maxItn=maxitn,
xguess=xguess,
fnorm=fnorm)
writeMatrix("The solution to the system is", x)
print("\nwith fnorm = %5.4f" % (fnorm[0]))
Output¶
with fnorm = 0.0000
The solution to the system is
1 2 3
1 2 3
Warning Errors¶
IMSL_TOO_MANY_FCN_EVALS |
The number of function evaluations has
exceeded maxItn . A new initial
guess may be tried. |
IMSL_NO_BETTER_POINT |
Argument errRel is too small. No
further improvement in the approximate
solution is possible. |
IMSL_NO_PROGRESS |
The iteration has not made good progress. A new initial guess may be tried. |
Fatal Errors¶
IMSL_STOP_USER_FCN |
Request from user supplied function to stop algorithm. User flag = “#”. |