electricpy.sim.NewtonRaphson

electricpy.sim.NewtonRaphson(F, J, X0, eps=0.0001, mxiter=100, lsq_eps=0.25)[source]

Newton Raphson Calculator.

Solve nonlinear system F=0 by Newton’s method. J is the Jacobian of F. Both F and J must be functions of x. At input, x holds the start value. The iteration continues until ||F|| < eps.

Parameters:
F : array_like

The Non-Linear System; a function handle/instance. The input function must accept only one (1) argument as an array or int/float representing the variables required.

J : array_like

The Jacobian of F; a function handle/instance. The input Jacobian of F must accept only one (1) argument as an array or int/float representing the variables required.

X0 : array_like

The Initial Value (or initial guess); a representative array.

eps : float, optional

Epsilon - The error value, default=0.0001

mxiter : int, optional

Maximum Iterations - The highest number of iterations allowed, default=100

lsq_eps : float, optional

Least Squares Method (Failover) Epsilon - the error value. default=0.25

Returns:

  • X0 (array_like) – The computed result

  • iteration_counter (int) – The number of iterations completed before returning either due to solution being found, or max iterations being surpassed.

Examples

>>> 
>>> import numpy as np
>>> from electricpy import sim # Import Simulation Submodule
>>> def F(x):
...     matr = np.array([[x[1]*10*np.sin(x[0])+2],
...         [x[1]*(-10)*np.cos(x[0])+x[1]**2*10+1]])
...     return(matr)
>>> def J(x):
...     matr = np.array([[10*x[1]*np.cos(x[0]), 10*np.sin(x[0])],
...         [10*x[1]*np.sin(x[0]), -10*np.cos(x[0])+20*x[1]]])
...     return(matr)
>>> # Now compute Newton-Raphson
>>> X0 = [0, 1]
>>> results, iter = sim.NewtonRaphson(F,J,X0)
>>> print(results)
[-0.236,0.8554]
>>> print(iter) # Iteration Counter
4

See also

nr_pq

Newton-Raphson System Generator

mbuspowerflow

Multi-Bus Power Flow Calculator