User Manual
PolymathPlus is a math solver for students, scientists, and engineers. It numerically solves problems entered in plain text—including linear equations, nonlinear systems, differential equations, and regression analyses—and produces detailed solution reports with statistics, tables, and charts.
System of Linear Equations
When solving a simultaneous set of linear equations, you should enter each equation in text format, one per line. The number of equations must be equal to the number of unknown variables for a unique solution to exist.
The following example demonstrates how to input a system of three linear equations to be solved:
|
|
In the program, the data would be entered as follows:
# System of three linear equations a + 2*c = 50 b = -2 + c a + 23*c = 12 + b
Note: The # symbol marks a comment. Anything after it on the same line is ignored by the solution engine, so you can use it to add notes or explanations.
The Solution Report shows the values of the unknowns at the solution, the problem definition, and any settings applied by the system or hints specified by the user.
| Variable | Value | |
|---|---|---|
| 1 | a | 54 |
| 2 | b | -4 |
| 3 | c | -2 |
Hints
Hints allow you to adjust solution algorithms, error tolerances, and the solution report on a per-program basis (as part of the program syntax). The global defaults—called settings—cannot be changed directly, but you can override them by including hints within a specific program.
| # | Hint syntax sample & Description |
|---|---|
| 1 | #@Report_fix_digits = 8 |
| Significant digits to display when presenting numbers | |
| 2 | #@Report_show_header = no |
| Indicates whether a header text is displayed in the solution report Valid values are yes or no |
|
| 3 | #@Report_show_Settings = no |
| Valid values are yes or no |
System of Nonlinear Equations
When solving simultaneous nonlinear equations, the equations have to be arranged as root expressions (expressions that are equal to zero at the
solution). In addition, an initial guess should be provided for each nonlinear variable.
Below is an example for solving 2 nonlinear equations with an initial estimate for x, and y at the solution.
|
|
Rearranging the equations, and estimating x and y to be 1 at the solution, this would be the data entry to solve this problem:
f(x) = x^2 + y - 12 f(y) = 2*x + log(y+2) - 5 x(0)=1 y(0)=1
you may define any number of auxiliary expressions, to simplify long expressions:
f(x) = x^2 + y - 12 f(y) = 2*x + a a = log(y+2) - 5 x(0)=1 y(0)=1
and the resulting solution report gives:
| Variable | Value | f(x) | |
|---|---|---|---|
| 1 | x | 2 | 8.4E-10 |
| 2 | y | 8 | -4.7E-11 |
The solver can handle either a single nonlinear equation or a system of simultaneous nonlinear equations. For a single equation, you must provide a range of values that brackets the solution; the program will find all roots within that interval. For a system, you must provide an initial guess for each variable. In this case, each guess is a single value, not a range.
The nonlinear equations have to be arranged such that all terms are on one side and the other side should be set to zero, essentially building the root expression. The algorithm will search the independent variables which cause the root expression to become zero, or very close to zero. Additional auxiliary variables can be defined to simplify expressions for repetitive terms.
The problems belonging to this category contain implicit nonlinear algebraic equations that require an iterative solution and explicit algebraic equations where individual variables can be expressed as explicit functions of other variables and constants. An implicit nonlinear equation must be entered in the form:
f(x) = an expression
where x is the chosen 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.
Only real roots (non-complex) can be found. Multiple roots are determined for a single equation where the range of the variable must be provided.
Explicit Algebraic Equations
This type of problem contains a set of explicit algebraic equations. Each equation must start with a variable name followed by the equal sign and a linear or nonlinear algebraic expression. The equations may be entered in any order. During the solution, the equations are automatically ordered to achieve a solution. The following system of explicit equations calculates the two roots of a quadratic equation. Note that both problem setups have the same solution.
# Explicit System of Nonlinear Equations a = 1 b = -12 c = 35 disc = b ^2-4*a* c x1 = (-b+sqrt(disc))/(2*a) x2 = (-b-sqrt(disc))/(2*a)
The Solution Report includes the values of the unknowns (x1 = 7, x2 = 5) and all the variables at the solution.
Note that the equations do not need to be ordered for solution. Keep this option in mind for your general explicit calculational needs.
Single Nonlinear Equation
This option involves the solution of a single implicit nonlinear equation with any number of explicitly defined variables. The implicit nonlinear
equation must be arranged to an expression which is equal to zero at the solution.
For the variable associated with the implicit equation, both minimal and maximal initial estimates must be provided. Multiple roots are reported if the
problem has several solutions (roots) between the minimal and maximal initial estimates.
The equations and initial estimates can be entered in any order. PolymathPlus always reorders the equations during the problem solution.
Solve a nonlinear equation to find the value of x:
Here's the problem to enter when the solution is expected to be found in the 0 - 100 range.
# Solve nonlinear equation f(x) = 4*x^2 + x*log(x/2) - 1620 x(min) = 0 x(max) = 100
Note that we can also define an explicit equation for the variable t that can be used to shorten large equations or ones with repeated section.
# Solve nonlinear equation f(x) = 4*x^2 + t - 1620 t = x*log(x/2) x(min) = 0 x(max) = 100
The Solution Report includes the value/s of the unknown (x=20, in this case), the function value/s (f(x)= 1.0e-8), and the values of all the variables at the solution.
Nonlinear Equations
This option involves the solution of a system comprising of several, implicit, nonlinear algebraic equations with any number of explicitly defined variables. All the implicit equations must be rewritten in the form:
f(x) = an expression
where x is a variable that does not appear on the left hand side of any of the explicit equations and the "expression" must be equal to 0 at the solution. Initial estimates must be provided for all the implicit variables using the syntax: x(0) = a value. The equations may be written in any order.
Example, Solving a set of 2 nonlinear equations:
|
|
The equations can be entered in the following manner (note that lines starting with
# Solveing two nonlinear equations f(x) = 2*x + x*y + 290 - y^2 f(y) = t + x*y - 105 t = x*log(y/2) x(0) = 2 y(0) = 10
The Solution Report includes the values of the unknowns (x = 5, y = 20), the function values (f(x)= -2.7E-12, f(y)= 1.3E-12 ) and the values of all the additional variable/s at the solution.
Simultaneous Linear and Nonlinear Equations
A mixed system of linear and nonlinear equations can always be solved by treating the entire set as nonlinear. However, if you can isolate some
variables and write them explicitly in terms of the remaining nonlinear variables, you reduce the number of variables that require initial guesses and
the number of equations that must be written in root form
f(x) = 0 and, on top of that, fewer nonlinear equations generally increase the likelihood of a successful solution.
Consider a problem containing three linear and two nonlinear equations in the original form:
|
|
These can be solved as a set of 5 nonlinear equations where an initial guess should be provided for each variable.
# Simultaneous nonlinear equations f(c) = 3.8907-ln(6*c)-a f(a) = a + b - c f(b) = 5*a - 3 - b f(d) = 2*c + 4 - 3*b - d f(e) = sin(0.1309*d)/0.1 - e # Initial guess of the variables # per nonlinear equation a(0) = 2 b(0) = 2 c(0) = 2 d(0) = 2 e(0) = 2
The program text above would successfully solve the equations. We could also rewrite the set of equations as two nonlinear equations (root expressions that are set to zero at the solutions), and three explicit expressions. This approach yields:
# Nonlinear equations & explicit vars f(c) = 3.8907-ln(6*c)-a f(a) = a + b - c # The explicit variables b = 5*a - 3 d = 2*c + 4 - 3*b e = sin(0.1309*d)/0.1 # Initial guess - nonlinear equation vars c(0) = 2 a(0) = 2
Note that both programs give the same solution, but the second program requires initial estimates for variables a and c only. The solution is calculated as:
| Variable | Value |
|---|---|
| a | 1.00011 |
| b | 2.00055 |
| c | 3.00066 |
| d | 3.99967 |
| e | 4.99964 |
Constrained Nonlinear Equations
A special solution algorithm is available for problems where some of the variables can have only positive values.
Consider the following set of equations:
|
|
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.
Constraints allowed 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 shown below:
f(x1) = x1/x2 - 5*ln(0.4*x2/x3) + 4.45977 f(x2) = x2 + x1 - 1 f(x3) = x3 + 0.5*x1 - 0.4
A solution can be achieved by using a constrained solution algorithm which is done by adding the following hint to the program (at any line location):
#@ NLE_SOLUTION_METHOD_INDEX = 3
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.
PolymathPlus 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 entire problem should look like this:
f(x1) = x1/x2 - 5*ln(0.4*x2/x3) + 4.45977 f(x2) = x2 + x1 - 1 f(x3) = x3 + 0.5*x1 - 0.4 #@ NLE_SOLUTION_METHOD_INDEX = 3 x1(0)=0.35 >= 0 x2(0)=0.5 > 0 x3(0)=0.15 > 0
and the solution becomes:
| Variable | Value | f(x) | Initial Guess |
|---|---|---|---|
| x1 | 0.757396 | 0.00E+00 | 0.35 |
| x2 | 0.242604 | 0.00E+00 | 0.5 |
| x3 | 0.021302 | 0.00E+00 | 0.15 |
Note that the constrained method may only be used for sets of equations with more than 1 simultaneous nonlinear equation.
Hints
Hints allow you to adjust solution algorithms, error tolerances, and the solution report on a per-program basis (as part of the program syntax). The global defaults—called settings—cannot be changed directly, but you can override them by including hints within a specific program.
| # | Hint syntax sample & Description |
|---|---|
| 1 | #@Report_fix_digits = 8 |
| Significant digits to display when presenting numbers | |
| 2 | #@Report_show_header = no |
| Indicates whether a header text is displayed in the solution report | |
| 3 | #@Report_show_charts = no |
| Indicates whether charts should be displayed in the solution report Valid values are yes or no |
|
| 4 | #@Report_show_Settings = no |
| Valid values are yes or no | |
| 5 | #@Chart_Size = 400 450 |
| Indicates the size of the chart to be drawn in the report Values are Width; Height |
|
| 6 | #@NLE_SOLUTION_METHOD_INDEX = 1 |
|
Algorithm used to solve the NLE system. Valid method indexes are:
0 = Fast-Newton 1 = Safe-Newton 2 = Safe-Broydn 3 = Constrained |
|
| 7 | #@NLE_tol_x = 1E-07 |
| 8 | #@NLE_tol_f = 1E-07 |
| 9 | #@NLE_min_tol = 1E-07 |
| 10 | #@NLE_eps = 1E-07 |
| 11 | #@NLE_max_iter = 150 |
Ordinary Differential Equations
An example for an ODE problem is displayed below.
The boundary conditions are:
x(0)=1, y(0)=4, t(0)=0, t(f)=1.
This is the data entry to solve the problem:
d(x)/d(t) = x + 4*t^2 d(y)/d(t) = sqrt(y) - x/(5-y) x(0)=1 y(0)=4 t(0)=0 t(f)=1
or you may define any number of auxiliary expressions, to simplify long and/or repeating sections:
d(x)/d(t) = x + 4*t^2 d(y)/d(t) = sqrt(y) - a a = x/(5-y) x(0)=1 y(0)=4 t(0)=0 t(f)=1
And the resulting report would look like:
| Value | ||||
|---|---|---|---|---|
| Ini | Min | Max | Final | |
| t | 0 | 0 | 1 | 1 |
| x | 1 | 1 | 4.465 | 4.46454 |
| y | 4 | 3.736 | 4.22 | 3.73573 |
| a | 1 | 1 | 3.531 | 3.53131 |
The problems belonging to this category are comprised of systems of simultaneous first-order ordinary differential equations and explicit algebraic equations. The differential equations must be entered in the following form:
d(x)/d(t) = an expression
Here t is the independent variable and x is a dependent variable. Note that other independent and dependent variable names are possible. Auxiliary algebraic equations must be entered in the form:
x = an expression
where the same naming rules apply.
An expression may contain any combination of constants and variables. However, the auxiliary equations must be explicit (not simultaneous). PolymathPlus will sort and order the equations and will indicate if a simultaneous situation exists (by warning of 'circular dependency').
Initial conditions must be specified for all the variables defined by differential equations, as well as the independent variable. A final value must also be specified for the independent variable.
# Set of consecutive reactions d(A)/d(t) = -k1*A d(B)/d(t) = k1*A - k2*B d(C)/d(t) = k2*B k1 = 1 k2 = 2 A(0) = 1 B(0) = 0 C(0) = 0 t(0) = 0 t(f) = 3
The Solution Report includes a Table listing the initial, minimal, maximal, and final values of all the variables over the integration range. These are the solver results for the example above:
| Variable | Value | |||
|---|---|---|---|---|
| Name | Ini | Min | Max | Final |
| t | 0 | 0 | 3 | 3 |
| A | 1 | 5e-2 | 1 | 0.049787 |
| B | 0 | 0 | 0.2497 | 0.047308 |
| C | 0 | 0 | 0.9029 | 0.90290 |
The graph shows the variation of A, B and C with time.
Note that the same ODE problem can also be entered in a simplified format such as this:
# Set of consecutive reactions A' = -k1*A B' = k1*A - k2*B C' = k2*B k1 = 1 k2 = 2 A|1 B|0 C|0 t|0:3
Hints
Hints allow you to adjust solution algorithms, error tolerances, and the solution report on a per-program basis (as part of the program syntax). The global defaults—called settings—cannot be changed directly, but you can override them by including hints within a specific program.
| # | Hint syntax sample & Description |
|---|---|
| 1 | #@Report_fix_digits = 8 |
| Significant digits to display when presenting numbers | |
| 2 | #@Report_show_header = no |
| Indicates whether a header text is displayed in the solution report | |
| 3 | #@Report_show_charts = no |
| Indicates whether charts should be displayed in the solution report Valid values are yes or no |
|
| 4 | #@Report_show_Matlab = no |
| Indicates whether a Matlab formatted problem should be displayed in the solution report Valid values are yes or no |
|
| 5 | #@Report_show_Settings = no |
| Valid values are yes or no | |
| 6 | #@Report_Show_Intermediate_data_points = no |
| Valid values are yes or no | |
| 7 | #@DEQ_Intermediate_data_points = 50 |
| 8 | #@DEQ_RKF_H = 1E-06 |
| 9 | #@DEQ_RKF_TETOL = 1E-06 |
| 10 | #@DEQ_EPS = 1E-05 |
| 11 | #@DEQ_H1 = 0.001 |
| 12 | #@DEQ_HMIN = 1E-08 |
| 13 | #@DEQ_SOLUTION_METHOD_INDEX = 3 |
|
DEQ solution method algorithm. Valid indexes are: 0 = RKF45 1 = RKF56 2 = BS 3 = Stiff 4 = StiffBS |
|
| 14 | #@Chart_Size = 400 450 |
| Indicates the size of the chart to be drawn in the report Values are Width Height |
|
| 15 | #@Chart_Y_Curves = A B k3 k4 k5 |
|
Specifies the variable names, separated by space or semicolons, to include in the solution report. Invalid names will be ignored. You can divide the curves into up to three charts. Use the | symbol to specify which curves belong to each chart. For example #@Chart_Y_Curves = A B | k3 k4 k5
|
|
| 16 | #@chart_log_scales = 010 |
| This hint specifies whether to use a logarithmic y-scale for each chart, with each digit corresponding to the nth chart. A value of 1 means a logarithmic y-scale is applied, while a value of 0 indicates a standard linear y-scale. In this example, the three digits represent the first, second, and third charts respectively, where the first and third charts use a linear scale, and the second chart uses a logarithmic y-scale. |
Regressions of Data
The known data points must be entered in a strict tabulated format, enclosed within opening and closing square brackets
The first line of the table should contain the variable names, separated by spaces. Subsequent lines must contain the corresponding values for the
variables, with columns separated by spaces. All rows must have the same number of columns, and blank values are not permitted. For example:
[
a b xy
1 9 1.1
2 13 7.87
3.1 16 5.67
4.2 18.2 8.9
5 19 45.6
6 20 56.7
7 19 66.9
]
Tip: Use the Edit > Align Table menu item to align the columns nicely.
Polynomial Regression
PolymathPlus can fit a polynomial of degree n with the general form:
or a linear equation when the equivalent first degree polynomial is specified
where a0, a1, ..., an are regression parameters to a set of N tabulated values of x (a single independent variable) versus y (a single dependent variable). The highest degree allowed for a polynomial is N - 1 (thus n >= N - 1). The program calculates the coefficients a0, a1, ..., an by minimizing the sum of squares of the deviations between the calculated y or P(x) and the data for y.
A polynomial regression is specified by the polyfit command that must be followed by the name of the independent variable vector, name of
the dependent variable vector, a number representing the degree of the polynomial, and an optional string origin which indicates that the
polynomial regression curve must go through the origin. These names must be separated by commas.
Correlation of BOD data example
Consider the following BOD (biological oxygen demand) data versus Time:
| Time (days) |
BOD (mg/liter) |
|---|---|
| 1 | 0.6 |
| 2 | 0.7 |
| 4 | 1.5 |
| 6 | 1.9 |
| 8 | 2.1 |
| 10 | 2.6 |
| 12 | 2.9 |
| 14 | 3.7 |
| 16 | 3.5 |
| 18 | 3.7 |
| 20 | 3.8 |
Fitting a straight line (linear regression) to the BOD vs. Time data.
BOD = a0 + a1*Time
# Straight line fit [ Time BOD 1 0.6 2 0.7 4 1.5 6 1.9 8 2.1 10 2.6 12 2.9 14 3.7 16 3.5 18 3.7 20 3.8 ] polyfit Time BOD 1
This program yields the the following solution results:
| Variable | Value | 95% confidence |
|---|---|---|
| a0 | 0.657797 | ±0.374896 |
| a1 | 0.178056 | ±0.031674 |
| R2 | Variance (σ2) |
|---|---|
| 0.9472735 | 0.0825304 |
The Solution Report contains a table with the observed (measured) y values, the predicted y values (ycalc) and the residuals (r = y - ycalc) are also displayed.
Fitting the data to a 2nd degree polynomial.
To fit a 2nd degree polynomial, the polyfit statement is revised:
polyfit Time BOD 2
The results obtained in this case are:
| Variable | Value | 95% confidence |
|---|---|---|
| a0 | 0.224387 | ±0.353054 |
| a1 | 0.310403 | ±0.081692 |
| a2 | -0.006439 | ±0.003851 |
| R2 | Variance (σ2) |
|---|---|
| 0.9815572 | 0.0324762 |
To fit a 2nd degree polynomial which passes through the origin, the following polyfit statement is used:
polyfit Time BOD 2 origin
For this polynomial a0 = 0, by definition, and the rest of the results are:
| Variable | Value | 95% confidence |
|---|---|---|
| a1 | 0.354779 | ±0.044171 |
| a2 | -0.008236 | ±0.002724 |
| R2 | Variance (σ2) |
|---|---|
| 0.9766053 | 0.0366186 |
Below are the plots containing: BOD (the data) vs. P(x) (calculated values), and Delta BOD (residuals) vs. time.
Regression Analysis: Judging by the R2 and σ2 values, the 2nd degree polynomial represents the data the best. However, the confidence interval value of a0 shows that this parameter is not significantly different from zero, thus the use of 2nd degree polynomial passing through the origin should be preferred.
4th order Polynomial Regression Example
Consider the following set of Pressure versus Temperature data:
| Temperature, T (Deg. C) |
Pressure, P (mm Hg) |
|---|---|
| -36.7 | 1 |
| -19.6 | 5 |
| -11.5 | 10 |
| -2.6 | 20 |
| 7.6 | 40 |
| 15.4 | 60 |
| 26.1 | 100 |
| 42.2 | 200 |
| 60.6 | 400 |
| 80.1 | 760 |
The following operations should be performed on this data:
- Convert the temperature to K (by adding 273.15 to the value shown)
- Convert the pressure to kPa (by multiplying the value shown by 0.1333)
- Fit a fourth degree polynomial to the Pressure (dependent variable) vs. Temperature data.
The program to accomplish these tasks is shown below.
# Vapor Pressure Correlation [ T_C P_mmHg -36.7 1 -19.6 5 -11.5 10 -2.6 20 7.6 40 15.4 60 26.1 100 42.2 200 60.6 400 80.1 760 ] T_K = T_C + 273.15 P_kPa = P_mmHg*0.1333 polyfit T_K P_kPa 4
The Solution Report includes a table listing the parameter values along with their 95% confidence intervals. For this problem, the results are:
| Variable | Value | 95% confidence |
|---|---|---|
| a0 | 2121.85 | 434.312 |
| a1 | -33.1504 | 6.01561 |
| a2 | 0.196174 | 0.0310526 |
| a3 | -0.000522 | 7.080E-05 |
| a4 | 5.28E-07 | 6.017E-08 |
The regression plot:
The Solution Report includes also the statistical metrics: R2, R2adj, root mean squared deviation and variance (σ2). A table displaying the observed (measured) y values, the predicted y values (ycalc) and the residuals (r = y - ycalc).
To fit a polynomial that passes through the origin the "polyfit" command must also include the parameter "origin". For example:
polyfit T_K P_kPa 4 origin
Multiple Linear Regression
This PolymathPlus option will fit a linear function of the form:
where a0, a1, ..., an are regression parameters fitted to N tabulated values of the independent variables x1, x2, ..., xn and the corresponding dependent variable y. Note that the number of data points must be greater than n+1 (thus N >= n+1). The program calculates the coefficients a0, a1, ..., an by minimizing the sum of squares of the deviations between the calculated and observed y values.
Use the mlinfit command to perform a multiple linear regression. List the independent variable names first, followed by the dependent variable name. An
optional final argument 'origin' forces the fit through the origin (thus a0 = 0).
Simple multiple linear regression example
The following data show bearing wear (y) that may depend on oil viscosity (x1) and load (x2).
| y | x1 | x2 |
|---|---|---|
| 293 | 1.61 | 851 |
| 230 | 15.5 | 820 |
| 172 | 22 | 1058 |
| 91 | 45 | 1201 |
| 125 | 33 | 1357 |
| 125 | 40 | 1115 |
In this example we compare the linear regression models that includes x1 and x2 as independent variables, with a model that includes only x1.
When the multilinear model is
y = a0 + a1x1 + a2x2
we enter the following program:
# Multiple linear regression [ y x1 x2 293 1.61 851 230 15.5 820 172 22 1058 91 45 1201 125 33 1357 125 40 1115 ] mlinfit x1 x2 y
The results obtained in this case are:
| a0 | = | 360.836 (±118.076) |
| a1 | = | -3.75246 (±1.774) |
| a2 | = | -0.084265 (±0.140313) |
| R2 | = | 0.9835209 |
| σ2 | = | 159.1951 |
To remove x2 from the regression model, we set mlinfit x1 y and the revised statement yields the following results:
| a0 | = | 292.784 (±37.3385) |
| a1 | = | -4.58727 (±1.23994) |
| R2 | = | 0.963462 |
| σ2 | = | 264.7302 |
Judging by the R2 and σ2 values, the regression model that contains both x1 and x2 represents the data better. However, the 95% confidence interval for a2 includes zero, indicating that the x2 term is not statistically significant; therefore removing x2 is justified.
Considering the "vapor pressure" example to fit a regression model of the form:
Define the dependent variable as logP = log(P_kPa) and the independent variables as OneonT = 1/T_K and
logT = log(T_K). Then use mlinfit to find the parameter values.
# VP Correlation by Reduced Riedel Eqs [ T_C P_mmHg -36.7 1 -19.6 5 -11.5 10 -2.6 20 7.6 40 15.4 60 26.1 100 42.2 200 60.6 400 80.1 760 ] # auxiliary variables derived T_K = T_C+273.15 P_kPa = P_mmHg*0.1333 logP = log(P_kPa) OneonT = 1/T_K logT = log(T_K) # regression model mlinfit OneonT logT logP
The Solution Report includes a table listing the parameter values along with their 95% confidence intervals. For this problem, the results are:
Regression Model:
where y and xi map to:
| Value | 95% confidence | |
|---|---|---|
| a0 | 77.268156 | 14.5730 |
| a1 | -5027.6098 | 629.568 |
| a2 | -23.960315 | 5.03175 |
| R^2 | R^2adj | RMSD | σ2 |
|---|---|---|---|
| 0.99956 | 0.99943 | 0.0056362 | 0.00045382 |
Nonlinear Regression
Nonlinear regression involves a general mathematical function (model) of the form:
where a0, a1, ..., an are regression parameters to a set of N tabulated values of x1, x2, ...,
xn
(independent variables) versus y (dependent variable). Note that the number of data points must be greater than m + 1 (thus N >= m + 1).
Use the nlinfit command to perform a nonlinear regression fit. The argument is the nonlinear regression model: the dependent variable vector name on the left side of the "=" sign, the independent variable vectors names and the parameters on the right side. Initial estimates must be provided for all the parameter values using expressions of the form:
m(ai) = value
Fitting a nonlinear equation (Antoine) to the "vapor pressure example". The regression model is of the following form:
where P is pressure (mmHg) , T is temperature (deg C) and A, B and C are parameters need to be fitted to the vapor pressure example data
Following is the program for finding the optimal parameter values with
nlinfit.
# Nonlinear Regression # Antoine Equation parameters [ T P -36.7 1 -19.6 5 -11.5 10 -2.6 20 7.6 40 15.4 60 26.1 100 42.2 200 60.6 400 80.1 760 ] logP = ln(P) # regression model nlinfit logP = A-B/(C+T) # initial guess for A, B, C m(A) = 8 m(B) = 2000 m(C) = 200
The optimal solution obtained becomes:
| Variable | Value | 95% confidence |
|---|---|---|
| A | 13.279810 | 0.623643 |
| B | 1559.0678 | 201.261 |
| C | 153.88543 | 10.1611 |
with the following statistic results:
| R^2 | R^2adj | RMSD | σ2 |
|---|---|---|---|
| 0.999688 | 0.999599 | 0.0108747 | 0.00168941 |
Nonlinear Challenges
Nonlinear regression is a powerful method for modeling complex relationships between variables. It enables direct fitting of nonlinear equations to observed data. However, nonlinear regression has some challenges:
- Initial Guesses: Good initial guesses for parameters are required, and poor guesses may lead to failure.
- Convergence: Nonlinear regression algorithms can fail to converge or get stuck in local minima.
- Complexity: Solving nonlinear equations is computationally intensive.
The relationship between time (t) and population (P) follows the model:
where P0 is the initial population at time t=0, and k is the growth rate constant.
This can be solved using a nonlinear regression model where P is the dependent variable,
t is the independent variable, and P0 and k are the parameters to be determined (model variables), requiring initial estimates.
Nonlinear Regression in PolymathPlus:
[ t P 0 100 1 120 2 150 3 190 4 250 ] # regression model nlinfit P = P0 * exp(k * t) # initial guesses m(P0) = 90 m(k) = 0.1
And the solution becomes P0 = 94.943880, and k = 0.23854324
Linearized Model for Exponential Growth
Taking the natural logarithm of both sides:
This transforms the model into a linear equation where ln(P) is the dependent variable, and t is the independent variable. Multiple Linear Regression in PolymathPlus:
[ t P 0 100 1 120 2 150 3 190 4 250 ] logP = ln(P) # regression model mlinfit t logP
Once solving the (multi)linear model we can map a0, a1, a2, etc. to the model variables we seek while we can be guarenteed to find the
best possible solution without needing to provide initial guess.
The solution yields: a0 = 4.5759337 hence P0 = exp(4.5759337) = 97.118677, and k = a1 = 0.22921138
This example involves a single independent variable, resulting in a simple linear regression. In cases with multiple
linearly dependent variables, multiple linear regression can be applied.
Complex nonlinear equations can also be linearized by applying transformations such as logarithmic functions,
inverse functions, or rearranging terms, simplifying them into linear forms for easier solving. Unlike nonlinear regression,
multiple linear regression does not require initial guesses and offers more robust convergence.
Regression Statistics
PolymathPlus provides statistical reports for the regression fit which assess the validity and accuracy of the regression. Each parameter is reported with its 95% confidence interval. If a parameter's 95% confidence interval includes zero, that parameter is not statistically significant at the 5% level.
In addition the following calculations are provided:
The following formulas calculate variables, standard deviation, and chi squared.
The following formulas calculate MAE, MSE, RMSE.
Hints
Hints allow you to adjust solution algorithms, error tolerances, and the solution report on a per-program basis (as part of the program syntax). The global defaults—called settings—cannot be changed directly, but you can override them by including hints within a specific program.
| # | Hint syntax sample & Description |
|---|---|
| 1 | #@Report_fix_digits = 8 |
| Significant digits to display when presenting numbers | |
| 2 | #@Report_show_header = no |
| Indicates whether a header text is displayed in the solution report | |
| 3 | #@Report_show_data_points = no |
| Display calculated datapoints table Valid values are yes or no |
|
| 4 | #@Report_show_settings = no |
| Show program settings Valid values are yes or no |
|
| 5 | #@Report_show_charts = no |
| Indicates whether charts should be displayed in the solution report Valid values are yes or no |
|
| 6 | #@Chart_Size = 400; 450 |
| Indicates the size of the chart to be drawn in the report Values are Width; Height |
|
| 7 | #@NLR_SOLUTION_METHOD_INDEX = 1 |
|
Algorithm used to solve the NLR model. Valid method indexes are:
0 = MRQMIN 1 = L-M |
|
| 8 | #@NLR_maxiter = 64 |
| 9 | #@NLR_tol = 0.0001 |
Variables and Expressions
The following objects compose a valid PolymathPlus math expression:
| Object | Description |
|---|---|
| Number |
A number can be represented in decimal notation or engineering notation.
Engineering notation can be used to indicate powers of 10 using 'e' or 'E' For example, 0.123e-5 is the same as 0.123*10^(-5). Note that only the period (.) is used as the decimal separator; commas or spaces must not be used for digit grouping. Sample valid numbers: 1.0, 2300, 5E-7, 1.23E+9, 34.0046 Invalid numbers: 5,000.00 12,9 12 000 000 |
| Variable Name |
Variable names must begin with an alphabetical character and can contain alphabetic and numeric characters or underscore "_". Variable names are case-sensitive, i.e. 't' and 'T' are two different variables. Special characters such as $, &, #, @ etc. are not allowed. Samples: x1, x2, A12, T, tm4eee, VH2O, PmmHg, KL_1 |
| Arithmetic Operator |
Returns the arithmetic operations of the binary components. For example '2+3' returns 5 The '^' symbol is used for exponentiation. The +, -, *, / symbols are used for addition, subtraction, multiplication, and division respectively. The precedence of operators is ^, (* or /), (+ or -) which is invoked working from left to right in an expression. Samples: +, -, *, /, ^ (1 + 2 * 3 ^ 2 ^ 3 / 2 - 4) is equivalent to (1 + (2 * (((3 ^ 2) ^ 3) / 2)) - 4) |
| Function Name |
Various mathematical function names are available. Examples: sin, cos, exp, ... The full list of available PolymathPlus functions is presented below. Samples: 12+sin(4) exp( 5*9+log(8)) |
| Condition Operator |
These operators return "1" if the condition met, and "0" if the condition does not meet. Examples: The expression '3>6' returns 0. The expression '4>=4' returns 1. Valid conditional operators: >, <, >=, <=, == |
| Boolean Operator |
The Boolean operators return "1" if the condition is met, and "0" if the condition is not met. Example: The expression '(2>3) or (1>0)' returns 1. The expression '1 and 0' returns 0. Valid Boolean operators: And, Or |
| If-Then-Else |
There are three case-insensitive keywords which compose the if-then-else expression structure. The three keywords are: If, Then, Else. Samples: If (x>5) Then (sin(12)+4) Else (sin(12)+8) if (x>5) then (sin(12)+4) else (sin(12)+8) |
A special if statement is available, with the following syntax:
The parentheses are optional.
The condition may include the following operators: and, or (Boolean operators), > (greater than), < (less than), >= (greater than or equal),
<= (less than or equal), == (equals).
The expressions may be any formula, including another
if statement (nested statements).
For example:
A = if (x>0) then (log(x)) else(0) b = if (T<minT) then (minT) else (if (T>maxT) then (maxT) else (T)) Vol_h1 = if (a>5 and c<2) then 1.12 else 7.89
Available Functions
PolymathPlus supports a range of standard mathematical functions. Function names must be written in lowercase. For example:
a = sin(12) + 14 b = sqrt(a + 25) c = exp(ln(a))
| Function Name | Description |
|---|---|
| abs | absolute value |
| arccos | trigonometric inverse cosine with result in radians |
| arccosec | trigonometric inverse cosecant with result in radians |
| arccosech | inverse hyperbolic cosecant |
| arcsech | inverse hyperbolic secant |
| arccosh | inverse hyperbolic cosine |
| arccotan | trigonometric inverse cotangent with result in radians |
| arccotanh | inverse hyperbolic cotangent |
| arcsec | trigonometric inverse secant with result in radians |
| arcsin | trigonometric inverse sine with result in radians |
| arcsinh | inverse hyperbolic sine |
| arctan | trigonometric inverse tangent with result in radians |
| arctanh | inverse hyperbolic tangent |
| cbrt | cubic root |
| cos | trigonometric cosine with argument in radians |
| cosec | trigonometric cosecant with argument in radians |
| cosech | hyperbolic cosecant |
| cosh | hyperbolic cosine |
| cotan | trigonometric cotangent with argument in radians |
| coth | hyperbolic cotangent |
| exp | exponential (e^x ) |
| exp10 | exponential of 10 (10^x ) |
| exp2 | exponential of 2 (2^x ) |
| frac | fractional part |
| int | integer part |
| ln | natural logarithm to the base e |
| log | logarithm to the base 10 |
| psi | psi function |
| round | rounded value |
| sec | trigonometric secant with argument in radians |
| sech | hyperbolic secant |
| sign | returns + 1 or 0 or -1 |
| sin | trigonometric sine with argument in radians |
| sinh | hyperbolic sine |
| sqrt | square root |
| tan | trigonometric tangent with argument in radians |
| tanh | hyperbolic tangent |
Settings and Hints
PolymathPlus allows custom tuning of solution algorithms, error tolerance, and basic adjustments to the output solution report. Default settings apply to all solution programs but can be overridden by program-specific hints.
Default Settings
These are the default settings and values used by PolymathPlus. While settings cannot be globally modified, you can override them for individual programs by adding hints directly into the program code. Simply copy the relevant line, adjust its value, and include it in your program:
#@Report_show_header = False
#@Report_fix_digits = 10
#@Report_show_charts = True
#@chart_size = 428 300
Valid nonlinear equations solution method indexes are:
0=Fast-Newton 1=Safe-Newton 2=Safe-Broydn 3=Constrained
#@NLE_SOLUTION_METHOD_INDEX = 1
#@NLE_tol_x = 1E-07
#@NLE_tol_f = 1E-07
#@NLE_min_tol = 1E-07
#@NLE_eps = 1E-07
#@NLE_max_iter = 150
Valid differential equations solution method indexes are:
0=RKF45 1=RKF56 2=BS 3=Stiff 4=StiffBS
#@DEQ_SOLUTION_METHOD_INDEX = 0
#@DEQ_Intermediate_data_points = 50
#@DEQ_RKF_H = 1E-06
#@DEQ_RKF_TETOL = 1E-06
#@DEQ_EPS = 1E-05
#@DEQ_H1 = 0.001
#@DEQ_HMIN = 1E-08
Valid nonlinear regression solution method indexes are:
0=MRQMIN, 1=L-M
#@NLR_SOLUTION_METHOD_INDEX = 1
#@NLR_maxiter = 128
#@NLR_tol = 0.0001
Hints are pre-fixed with #@ in the program file.
The following sample DEQ problem has a hint line to propose the size of the chart on the solution report.
# demo the use of hints #@ chart_size = 350; 300 d(x)/d(t) = x + 4*t^2 d(y)/d(t) = sqrt(y) - x/(5-y) t(0)=0 t(f)=1 x(0)=1 y(0)=4