ptolemy.domains.algebraic.kernel

## Class AlgebraicLoopDirector.NewtonRaphson

• Enclosing class:
AlgebraicLoopDirector

```class AlgebraicLoopDirector.NewtonRaphson
extends AlgebraicLoopDirector.AlgebraicLoopSolver```
Class for solving algebraic loops using the Newton-Raphson method.
Author:
Michael Wetter
• ### Field Summary

Fields
Modifier and Type Field and Description
`protected double[]` `_deltaX`
Step size for finite difference approximation
`protected double[]` `_f`
Temporary variable used to store the result of f(x) = x-g(x)
• ### Fields inherited from class ptolemy.domains.algebraic.kernel.AlgebraicLoopDirector.AlgebraicLoopSolver

`_converged, _iterationCount, _maxIterations, _tolerance, _variableNames`
• ### Constructor Summary

Constructors
Constructor and Description
```NewtonRaphson(java.lang.String[] variableNames, double[] tolerance, int maxIterations)```
Construct an algebraic loop solver.
• ### Method Summary

All Methods
Modifier and Type Method and Description
`protected double[]` ```_gaussElimination(double[][] A, double[] f)```
Return vector x that solves A*x=f by a Gauss elimination with normalization and interchange of rows.
`protected double[]` ```_newtonStep(double[] x, double[] f)```
Return the new iterate of a Newton step.
`protected void` ```_residual(double[] x, double[] f)```
Evaluate the residual function f(x) = x-g(x).
`void` `solve(double[] xIni)`
Solve the algebraic loop using the specified array as the initial guess for the variables being solved for and replace the contents of the specified array with the solution that is found.
• ### Methods inherited from class ptolemy.domains.algebraic.kernel.AlgebraicLoopDirector.AlgebraicLoopSolver

`_didConverge, converged, getIterationCount`
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

• #### _deltaX

`protected double[] _deltaX`
Step size for finite difference approximation
• #### _f

`protected double[] _f`
Temporary variable used to store the result of f(x) = x-g(x)
• ### Constructor Detail

• #### NewtonRaphson

```public NewtonRaphson(java.lang.String[] variableNames,
double[] tolerance,
int maxIterations)
throws IllegalActionException```
Construct an algebraic loop solver.
Parameters:
`variableNames` - Names of each break variable.
`tolerance` - Tolerance for each variable.
`maxIterations` - Maximum number of iterations.
Throws:
`IllegalActionException`
• ### Method Detail

• #### solve

```public void solve(double[] xIni)
throws IllegalActionException```
Solve the algebraic loop using the specified array as the initial guess for the variables being solved for and replace the contents of the specified array with the solution that is found.

This method iterates until a solution is found. If it does not converge within the maximum number of iterations, it throws an IllegalActionException. A method that calls solve(double[] xIni) can then call converged() to check whether the exception is thrown because of lack of convergence.

Specified by:
`solve` in class `AlgebraicLoopDirector.AlgebraicLoopSolver`
Parameters:
`xIni` - Array with the initial values of the variables, which will be replaced by this method with the solution.
Throws:
`IllegalActionException` - If the prefire() method returns false having previously returned true in the same iteration, or if the prefire() or fire() method of the actor throws it, or if evaluating the function yields a value that is not a double, or if the solver fails to find a solution.
• #### _newtonStep

```protected double[] _newtonStep(double[] x,
double[] f)
throws IllegalActionException```
Return the new iterate of a Newton step.
Parameters:
`x` - The best known iterate.
`f` - The function value f(x)=g(x)-x.
Returns:
The new guess for the solution f(x) = 0.
Throws:
`IllegalActionException` - If the solver fails to find a solution.
• #### _gaussElimination

```protected double[] _gaussElimination(double[][] A,
double[] f)```
Return vector x that solves A*x=f by a Gauss elimination with normalization and interchange of rows.
Parameters:
`A` - A square matrix
`f` - Array with solution of A*x=f
Returns:
x Array x = A^-1 * f
• #### _residual

```protected void _residual(double[] x,
double[] f)
throws IllegalActionException```
Evaluate the residual function f(x) = x-g(x).
Parameters:
`x` - Input to the loop function g(x).
`f` - Double vector of the same size as x. The result will be stored in this function.
Throws:
`IllegalActionException` - If the prefire() method returns false having previously returned true in the same iteration, or if the prefire() or fire() method of the actor throws it, or if evaluating the function yields a value that is not a double.