Nonlinear Equations Solver

See Also: Linear Equations Solver  Differential Equations Solver


Expand All | Collapse All

Overview

The purpose of this program is to allow you to solve systems of nonlinear algebraic equations. The system may both nonlinear simultaneous (implicit) and auxiliary (explicit) equations. Only real roots (non-complex) can be found. All equations are checked for correct syntax and other errors upon entry. Multiple roots are given for a single equation.

The equation input/modification window is shown below.

The options and information available on this screen are the following:

Icon Description
Add NLE: Input a new nonlinear (implicit) algebraic equation.
Add EE: Input a new explicit algebraic equation.
ini-finl Enter minimum and maximum boundaries for single nonlinear equation.
Opens a window showing a list of defined and undefined variables.
Problem setup state. Blue color - complete, ready to be solved.
Problem setup state. Red color - incomplete (missing definition, initial state etc.).
Solve the system of equations. Note that this option is displayed only when a valid set of equations, including initial guesses for the variables, have been entered.

Comments: Use the "#" character to put a comment in the program text.

Algorithm Drop-down box: Select the preferred solution algorithm. The default algorithm is recommended for most problems and is usually quite adequate.

At the top of the window the number of nonlinear and auxiliary equations is listed and problem setup related warning messages (undefined variables, missing initial values etc.) are given. This is invaluable information as equations are entered.

Detailed additional information is provided with regard to the following subjects:


Entering the Equations and Initial Estimates

There can be two types of equations in a system of nonlinear algebraic equations: equations that require simultaneous iterative solution (implicit nonlinear equations) and equations where one of the variables can be expressed as explicit function of other variables and constants. An implicit nonlinear equation must be entered in the form:

	f(x) = an expression

where 'x' is the variable name , and f(x) is an expression that will have the value of zero at the solution.

An explicit equation should be rewritten in the form:

	x = an expression 

where the expression may contain constants, implicit variables, and explicit variables.

Entering Implicit Nonlinear Equations

An implicit nonlinear equation may be entered by selecting “Nonlinear Equations (NLE)” from the file menu or clicking the NLE icon. This results in the following dialog box:

In this dialog box enter the name of one of the nonlinear variables between the brackets of the f( ) field. Enter the right hand side expression in the window between “=” and “=0”, and enter an initial estimate for the nonlinear variable. You can enter a comment (describing the variable and the units for example) if you wish. Press “Done” to finish entering the equation.  Note that this entry defines the nonlinear variable to Polymath, but the variable does not need to appear in the particular nonlinear equation.

Entering Explicit Equations

An explicit equation can be entered by pressing the “Add EE” button. The following dialog box opens:

In this dialog box enter the name of the variable in the box to the left of the “=”. Enter the right hand side expression into the window on the right of the “=” sign. You can also enter a comment if you wish. Press “Done” to finish entering the equation.

All the variables must be defined and the initial conditions specified before a set of equations can be solved. Thus variable must appear in the left side of either an implicit nonlinear equation or an explicit equation.

Variable Tracking

A convenient feature of Polymath is that the undefined variables and missing initial guesses are indicated directly above the entry window as shown below to the right of the red X:

This feature helps you to insure that missing or misnamed variables are identified and easily entered or corrected.  Also note that pressing or choosing "Show Variables..." from the "Problem" drop-down menu opens a window with a summary of the problem variables as shown below:

Ordering of Equations

You need not be concerned with the order in which you enter the equations or the initial guesses.  Polymath will automatically order the equations during problem solution.  You may have your equations ordered at any time by selecting "Order Equations" from the drop-down "Problem" menu.

Editing Equations

Standard editing is available for entered equations from the "Edit" drop-down menu. Also note that you can copy an expression or part of it while in the equation entry dialog box or in the entered equation by marking it and pressing “Ctrl+C”. Pasting involves the key press combination “Ctrl+V”.

Experienced users can use the full-screen Polymath editor to directly enter all equations and initial guesses.  This feature also allows you to copy all or parts of other Polymath programs to your current problem.  The "#" character can be used to put a comment in the program text.


Selection of Initial Estimates

The solution algorithms require specification of initial estimates for all the variables in the implicit nonlinear equations. Generally speaking, closer initial estimates have a better chance of converging to the desired solution.

Single Nonlinear Equation

When solving a single nonlinear equation, you must enter an estimated range within which the solution lies. This can be accomplished with the button . The syntax for enter the minimum and maximum range of the problem variable involves arguments of "min" and "max" respectively. These can alternately be entered as individual lines during problem input. For example, see the range for the variable x in the problem below:
For example:

f(x) = sin(2 * x)
x(min) = 0
x(max) =
5

Polymath will identify multiple solutions if they exist within this range. You may also alternatively request a plot of the function values in the specified range of the independent variable, so that the location of the roots (if any) can be seen. The program will then show the roots. If no roots exist in the estimated range, the plot can indicate in what direction you should seek the solution.

Two or More Nonlinear Equations

If there are two or more implicit nonlinear equations, the selection of initial guesses is more challenging. First, try to set initial guesses for the variables using physical considerations. (For example: The mole or mass fraction of a component can be neither negative nor greater than 1; the temperature of cooling water can be neither below freezing nor above boiling; etc.).

Be especially careful not to select initial guesses where some of the functions may be undefined. For example:
       f(xa) = 1/(xa-xb)+ ...
  is undefined whenever xa=xb
       f(xb) = log(1-xb)
  is undefined whenever xb>=1
Selecting such a set of initial guesses will probably stop the solution process, and an error message will be displayed.

When solving nonlinear equations simultaneously, initial guesses must be set for each nonlinear variable. The initial guess for each equation is entered in the nonlinear equation entry dialog box.  Alternatively the syntax for supplying the initial guess in the problem  is the particular variable with an argument of "0".  For examples, see the input for initial guesses for variables x1, x2, and x3 given in the problem below:

f(x1) = midtm ^ 0.8 - x2 * 2 - 12

f(x2) = 2 * x1 - 9 / (1 + x1) + (x2 + x3) * nlh

f(x3) = 7 * x3 - 9 * v

v = x1 + x2

midtm = x1

nlh = x1 + midtm + v

x1(0) = 2

x2(0) = 0.2

x3(0) = 9

Problem Solutions

Remember that Polymath will have a better chance of finding a solution to a complex set of simultaneous nonlinear equations and explicit algebraic equations where the initial guesses are close to the solution values.  Polymath solutions typically are found that are in the region of the initial guesses.  Note that not all sets of nonlinear equations have solutions.  Your problem must be posed properly.  Also, there may be multiple solutions to a particular set of equations which can be identified by starting at different sets of initial guesses.


Examples
Example 1 - Explicit Equations Only

The pressure (P) is to be calculated at a given temperature (T) and volume (V) according to the equation:

P = R·T/(V-b) – a/V2

where
a  = (27/64)(R2·Tc2/Pc)

b  = R·Tc/(8Pc)
R  = 0.08206

Tc = 304.2

Pc = 72.9

In this example, all of the equations are explicit algebraic equations. The equations can be inputted in any order as Polymath will order equations as necessary during the problem solution. The expression for the variable “a” can be entered by pressing the “Add EE” button. In the dialog box that opens put “a” in the box preceding the “=” sign and put the expression “(27/64)*(R^2*Tc^2/Pc)” in the window after the equal sign. A comment indicating that "a" is a Van-der-Waals equation constant can also be added. The entry of "a" with the dialog box is shown below.

After pressing “Done” the equations for R, Tc, Pc and b can be entered similarly. Suppose that the calculation of pressure is desired at three different combinations of temperature and volume.  The three corresponding pressures can be designated by P!, P2, and P3 at corresponding temperatures and volumes. The pressure P1 can be calculated for T = 400 K and V = 0.8 liters by entering the  equation as shown below:

After pressing “Done” to enter the equation for P!, the entire equation can be copied and edited in order to duplicate the same equation with different T, V values along with names for the pressure. The final for of the equation system with three expressions for P1, P2 and P3 for three different T and V sets is shown below.

Experienced users can enter all the lines of this problem directly into the full-screen editor window.

A mouse click on the pink arrow button solves this problem and results in the Polymath Report shown below:


Example 2 - One Implicit Nonlinear Algebraic Equation and Two Explicit Algebraic Equations

Consider the following set of three equations:

 f(T) = x1P1 + x2P2 – 760  (will be zero at solution) 	(1)
 log(P1) = 6.85221 - 1064.63/(T + 232.0)	(2)
 log(P2) = 6.87776 - 1171.53/(T + 224.366)	(3)

with x1 = 0.1 and x2 = 0.9.  The equation set  must be solved for the unknown T in the range of Tmin = 36 and Tmax = 70. The log represents the logarithm to the base 10.

In order to solve this set with Polymath, equations (2) and (3) should preferably be rewritten as explicit equations for P1 and P2. Thus:

P1 = 10^(6.85221-1064.63/(T+232.0))  
P2 = 10^(6.87776-1171.53/(T+224.366))

The equations can be inputted in any order. The nonlinear implicit equation for T can be entered by pressing the “Add NLE” button. In the dialog box that opens, put “T” (without the quotation marks) in the box between the parentheses and place the expression: “x1*P1+x2*P2-760” (without the quotation marks) in the window between “=” and “=0”. (Note that POLYMATH does not allow use of subscripts or superscripts). Finally enter an initial guess in the middle of the initial range yielding T(0)=53. The dialog box after entering the equation is shown below.

Press “Done” when finished. To enter the equation for P1 press the “Add EE” button. In the dialog box that opens put “P1” in the box preceding the “=” sign and put the expression “10^(6.85221-1064.63/(T+232.0))” in the window after the equal sign. Before pressing “Done” you can copy the expression by marking it and pressing “Cntr C”. The explicit equation input window, with the equation for P1 is shown below:

Press “Done” to finish entering this equation and “Add EE” to enter the equation for P2. To enter the expression bring the cursor to the window after the “=” sign, press “Cntr V” to paste the expression of P1 and change the numerical constants to the values as they appear in the expression of P2. The constants: x1 = 0.1 and x2 = 0.9 should be added as two additional explicit equations. For a system with one nonlinear equation the program requires entering upper and lower limits for the implicit variable. Enter 30 as lower limit and 70 as upper limit for T. The final set of equations, ready to be solved is shown below.

Experienced users can enter all the lines of this problem directly into the full-screen editor window.

The solution is obtained by pressing the pink arrow key with the Graph box checked.  The Report is given below:

The graphical solution (unedited)  is presented below:

The graphical result confirms the calculated result given in the Polymath report.


Example 3 - Two Implicit Nonlinear Algebraic Equations and Several Explicit Algebraic Equations

Consider the following set of equations:

k·CA12 = v·(CA0-CA1)/V
k·CA22 = v·(CA1-CA2)/V

where k = 0.075, v = 30, CA0 = 1.6, and CA2 = 0.2 CA0. The resulting equation set must be solved for the variables CA1 and V using the initial estimates: CA1=1 and V=300.

To solve this system of equations, each nonlinear algebraic equation must be rewritten in the form f(x)=(an expression that is to have the value of zero at the solution). The appropriate forms for these equations are:

f(CA1) = k·CA12 – v(CA0 - CA1)/V 
f(V) =  k·CA22 – v(CA1-CA2)/V

To input the first nonlinear equation press the “Add IE” button. In the dialog box that opens put “CA1” (without the quotation marks) in the box between the parentheses of: f(…)=. Put the expression: “k·CA12 - v(CA0 - CA1)/V” (without the quotation marks) in the window between “=” and “=0” (Note that POLYMATH does not allow use of subscripts or superscripts). The expression can be copied for future use by marking it and pressing “Cntr C”. To finish entering the implicit equation type in the initial guess for Ca1 (=1.0) in the appropriate box. Press “Done” when finished.

To input the second implicit equation press the “Add IE” button. In the dialog box that opens put “V” in the box between the parentheses of: f(…)=. To enter the expression, bring the cursor to the window after the “=” sign, press “Cntr V” to paste the expression of Ca1 and change the variable indexes: CA0 to CA1 and CA1 to CA2 as shown in Eq. 5. Enter the initial estimate 300 in the appropriate box. Press “Done” to finish entering this equation. Enter the constants k,v, CA0 and CA2 as four additional explicit equations. The set of equations, ready for solution is shown below.

Experienced users can enter all the lines of this problem directly into the full-screen editor window.

The resulting Polymath Report is given below:


Example 4 - Constrained Variables

Consider the following set of equations:

f(x1) = x1/x2 - 5 ln(0.4 x2/x3) + 4.45977 = 0

 

where

x2 = 1 - x1

x3 = 0.4 - 0.5 x1

In this problem, the variables x1, x2 and x3 are constrained. At the solution x1>0 because of physical considerations; x2 and x3 must be greater than zero throughout the solution process otherwise a negative value inside the ln function will yield an execution error. POLYMATH allows constraints on the variables of the type x>0 or x<0.

For the solution, the equations for x2 and x3 must be rewritten in the implicit form as:

f(x2) = x2 + x1 –1 = 0

f(x3) = x3 + 0.5 x1 – 0.4 = 0.

A Polymath solution can be achieved by first selecting “constrained” as the solution method. With this solution method selected additional input fields are added to the NLE equation input dialog box for defining constrains. Refer to the dialog box below to where the equation f(x1) has been already entered:

The options for constrains of x1 are “Any value” that indicates no constraints, “positive” that means it must be positive at the solution only and “always positive” that forces the variable to be positive all the way to the solution. For x1 the “positive” definition is sufficient. Since the variables x2 and x3 must be positive for all calculation leading to the solution, the option “Always positive” must be marked. The initial guesses for x2 and x3 are 0.5 and 0.15 respectively, which are consistent with initial guess of x1=0.5.

POLYMATH designates the constrained variables in the initial guess line as follows: Positive value will have ">= 0" added to the initial guess value, Always positive values will have "> 0" added to the initial guess value. The Polymath editor screen for the above problem is given below:

Experienced users can enter all the lines of this problem directly into the full-screen editor window.

The resulting Polymath Report screen presents the results of this problem:

Note that the constrained method may only be used for sets of equations with more than 1 simultaneous nonlinear equation.


The Conditions for Solution and General Settings are presented at the end of each of the Polymath Reports as shown above.  This provides a summary of the calculations carried out during the problem solution.


Solution Algorithms

There are four algorithms available in Polymath for solution of nonlinear equations, and all of them are based on the Newton-Raphson (NR) method. This method uses a truncated Taylor series estimate of the function values to obtain better estimates of the unknowns. In each iteration the function values and the matrix of partial derivatives are calculated. Numerical perturbation is used for calculating the matrix of partial derivatives. The iterative solution stops if either the sum of the magnitudes of the functions is less than TolF (default value 1.0E-7) or the sum of the absolute values of the corrections to the unknowns is less than TolX (default value 1.0E-7). NTrial (default value 150) is the maximal number of iterations that are allowed.

The default solution method uses the NR method with line search to ensure reduction of the norm of the function values in every iteration. This is a “safe” method that usually finds the solution (unless it converges to a local minimum) but it requires a large number of function evaluations per iteration and may be too slow for large scale problems. Calculations can be speeded up by using the Newton-Raphson method without the line search where the function evaluations associated with the line search are saved. This method is faster if it converges but it may diverge frequently. Another way to reduce the number of function evaluations is to use Broyden’s method where the matrix of partial derivatives is calculated only once by numerical perturbation after that this matrix is updated rather than being recalculated. For detailed explanation of the above three algorithms see the book by Press et al.

For system of equations containing constrained variables (some or all of the variables must be positive throughout the solution process or only at the solution), an algorithm which combines the step length restricted NR method with a continuation type method is used. In the step length restricted NR method the progress in the NR direction is restricted so that none of the constrained variables become zero or negative. In the continuation method, the original problem is converted into a sequence of easier to solve problems where the solution is closer to the initial estimate than in the original problem. For detailed explanation of the above three algorithms see the publication by Shacham.

If there is only one implicit nonlinear equation, the program will attempt to find all the solutions. For such a system you must specify an interval in which the root(s) can be found. The function is plotted and smaller intervals in which root(s) are located are identified according to sign change of the function. Either the “fast” or the “safe” NR method is used to converge to the exact solution values for each root that has been identified.


Solution Report

The solution report obtained after solving Example 3, that is discussed in the “Entering the equations and initial estimates into the program” section is used to show the various information presented in the report page. The first part of this report is shown below.

 

POLYMATH Report Sample problem 1
Nonlinear Equations  

 

Calculated values of NLE variables

 

Variable

Value

f(x)

Initial Guess

1

CA1

0.6019966

-8.246E-09

1.

2

V

1101.549

-2.772E-09

300.

 

 

Variable

Value

1

k

0.075

2

v

30.

3

CA0

1.6

4

CA2

0.32

In this section the title of the problem as entered by the user, the date when the solution was obtained, and the values of all the variables at the solution are shown. For the nonlinear algebraic equations, the function values (which should theoretically be zero) are also shown. For an acceptable solution the function values should be close to zero. If the program is unable to find a solution, then an appropriate error message is shown. In such case or in case that the function values are too high, you can refer to "Troubleshooting" to determine the cause for the failure of the program to reach an acceptable solution.

In the Polymath Report the entered equations are shown:

 

Nonlinear equations

1

f(CA1) = k * CA1 ^ 2 - v * (CA0 - CA1) / V = 0

2

f(V) = k * CA2 ^ 2 - v * (CA1 - CA2) / V = 0

 

Explicit equations

1

k = 0.075

2

v = 30

3

CA0 = 1.6

4

CA2 = 0.2 * CA0

The nonlinear (implicit) equations and the explicit equations are grouped separately. In case that constraints on the variables were defined, they are also shown here. Additional information presented includes the comments associated with each equation (if any), information related to settings, and the location where the problem file is stored.

Settings
Tolerance X 0.0000001
Tolerance F 0.0000001
Max trials 150

General
Number of implicit equations 2
Number of explicit equations 4
Solution method FASTNEWT
Elapsed time 0.0000 sec


Troubleshooting

Most error messages issued by the program are self-explanatory and suggest the type of action that should be taken to correct the problem. In some cases, however, no error message is given or the message is less obvious. Discussion of some of those cases and the corrective actions that can be taken follows:

The arrow does not show after finishing problem input.

The appearance of the pink arrow on the equation input window indicates that the problem has been completely defined and the program is ready to solve it. The input errors/omissions are usually indicated just above the input window.  Two of the most common reasons for the arrow to be gray are:

Circular definition of explicit variables not allowed.

This message appears while trying to solve a set of equations when one (or more) variable is defined as a function of another variable, which itself is a function of the first variable. For example, attempting to define y=z/x, when z was already defined as a function of y, will cause this error message. This can be prevented by writing the equation using an implicit form: f(y)=y-z/x.

No solution appears beneath the NLE Solution heading in the report page.

No solution can be found in the specified region for a single nonlinear equation. Plot the function value by marking the “Graph” option in the equation input window and resolve the equation. Use the graph obtained to extend the solution interval in the direction where the absolute function value is diminishing.

Solution did not converge.

This message usually appears when solving several simultaneous nonlinear equations,

The most common causes for the program to indicate that the solution has not been found are the following: