2
* Definitions for lsquares::
5
@node Definitions for lsquares, , lsquares, lsquares
6
@section Definitions for lsquares
9
@defvr {Global variable} DETCOEF
11
This variable is used by functions @code{lsquares} and @code{plsquares} to store the Coefficient of Determination which measures the goodness of fit. It ranges from 0 (no correlation) to 1 (exact correlation).
13
When @code{plsquares} is called with a list of dependent variables, @var{DETCOEF} is set to a list of Coefficients of Determination. See @code{plsquares} for details.
15
See also @code{lsquares}.
19
@deffn {Function} lsquares (@var{Mat},@var{VarList},@var{equation},@var{ParamList})
20
@deffnx {Function} lsquares (@var{Mat},@var{VarList},@var{equation},@var{ParamList},@var{GuessList})
21
Multiple nonlinear equation adjustment of a data table by the
22
"least squares" method. @var{Mat} is a matrix containing the data,
23
@var{VarList} is a list of variable names (one for each @var{Mat} column),
24
@var{equation} is the equation to adjust (it must be in the form:
25
@code{depvar=f(indepvari,..., paramj,...)}, @code{g(depvar)=f(indepvari,..., paramj,...)}
26
or @code{g(depvar, paramk,...)=f(indepvari,..., paramj,...)}), @var{ParamList} is the
27
list of the parameters to obtain, and @var{GuessList} is an optional list of initial
28
approximations to the parameters; when this last argument is present, @code{mnewton} is used
29
instead of @code{solve} in order to get the parameters.
31
The equation may be fully nonlinear with respect to the independent
32
variables and to the dependent variable.
33
In order to use @code{solve()}, the equations must be linear or polynomial with
34
respect to the parameters. Equations like @code{y=a*b^x+c} may be adjusted for
35
@code{[a,b,c]} with @code{solve} if the @code{x} values are little positive integers and
36
there are few data (see the example in lsquares.dem).
37
@code{mnewton} allows to adjust a nonlinear equation with respect to the
38
parameters, but a good set of initial approximations must be provided.
40
If possible, the adjusted equation is returned. If there exists more
41
than one solution, a list of equations is returned.
42
The Coefficient of Determination is displayed in order to inform about
43
the goodness of fit, from 0 (no correlation) to 1 (exact correlation).
44
This value is also stored in the global variable @var{DETCOEF}.
46
Examples using @code{solve}:
48
(%i1) load("lsquares")$
50
(%i2) lsquares(matrix([1,2,0],[3,5,4],[4,7,9],[5,8,10]),
51
[x,y,z], z=a*x*y+b*x+c*y+d, [a,b,c,d]);
52
Determination Coefficient = 1.0
53
x y + 23 y - 29 x - 19
54
(%o2) z = ----------------------
56
(%i3) lsquares(matrix([0,0],[1,0],[2,0],[3,8],[4,44]),
57
[n,p], p=a4*n^4+a3*n^3+a2*n^2+a1*n+a0,
59
Determination Coefficient = 1.0
61
3 n - 10 n + 9 n - 2 n
62
(%o3) p = -------------------------
64
(%i4) lsquares(matrix([1,7],[2,13],[3,25]),
65
[x,y], (y+c)^2=a*x+b, [a,b,c]);
66
Determination Coefficient = 1.0
67
(%o4) [y = 28 - sqrt(657 - 216 x),
68
y = sqrt(657 - 216 x) + 28]
69
(%i5) lsquares(matrix([1,7],[2,13],[3,25],[4,49]),
70
[x,y], y=a*b^x+c, [a,b,c]);
71
Determination Coefficient = 1.0
77
Examples using @code{mnewton}:
79
(%i6) load("lsquares")$
81
(%i7) lsquares(matrix([1.1,7.1],[2.1,13.1],[3.1,25.1],[4.1,49.1]),
82
[x,y], y=a*b^x+c, [a,b,c], [5,5,5]);
84
(%o7) y = 2.799098974610482 1.999999999999991
86
(%i8) lsquares(matrix([1.1,4.1],[4.1,7.1],[9.1,10.1],[16.1,13.1]),
87
[x,y], y=a*x^b+c, [a,b,c], [4,1,2]);
89
(%o8) y = 3.177315891123101 x
91
(%i9) lsquares(matrix([0,2,4],[3,3,5],[8,6,6]),
92
[m,n,y], y=(A*m+B*n)^(1/3)+C, [A,B,C], [3,3,3]);
94
(%o9) y = (3.999999999999862 n + 4.999999999999359 m)
98
To use this function write first @code{load("lsquares")}. See also @code{DETCOEF} and @code{mnewton}.
102
@deffn {Function} plsquares (@var{Mat},@var{VarList},@var{depvars})
103
@deffnx {Function} plsquares (@var{Mat},@var{VarList},@var{depvars},@var{maxexpon})
104
@deffnx {Function} plsquares (@var{Mat},@var{VarList},@var{depvars},@var{maxexpon},@var{maxdegree})
105
Multivariable polynomial adjustment of a data table by the "least squares"
106
method. @var{Mat} is a matrix containing the data, @var{VarList} is a list of variable names (one for each Mat column, but use "-" instead of varnames to ignore Mat columns), @var{depvars} is the name of a dependent variable or a list with one or more names of dependent variables (which names should be in @var{VarList}), @var{maxexpon} is the optional maximum exponent for each independent variable (1 by default), and @var{maxdegree} is the optional maximum polynomial degree (@var{maxexpon} by default); note that the sum of exponents of each term must be equal or smaller than @var{maxdegree}, and if @code{maxdgree = 0} then no limit is applied.
108
If @var{depvars} is the name of a dependent variable (not in a list), @code{plsquares} returns the adjusted polynomial. If @var{depvars} is a list of one or more dependent variables, @code{plsquares} returns a list with the adjusted polynomial(s). The Coefficients of Determination are displayed in order to inform about the goodness of fit, which ranges from 0 (no correlation) to 1 (exact correlation). These values are also stored in the global variable @var{DETCOEF} (a list if @var{depvars} is a list).
111
A simple example of multivariable linear adjustment:
113
(%i1) load("plsquares")$
115
(%i2) plsquares(matrix([1,2,0],[3,5,4],[4,7,9],[5,8,10]),
117
Determination Coefficient for z = .9897039897039897
119
(%o2) z = ---------------
123
The same example without degree restrictions:
125
(%i3) plsquares(matrix([1,2,0],[3,5,4],[4,7,9],[5,8,10]),
127
Determination Coefficient for z = 1.0
128
x y + 23 y - 29 x - 19
129
(%o3) z = ----------------------
133
How many diagonals does a N-sides polygon have? What polynomial degree should be used?
135
(%i4) plsquares(matrix([3,0],[4,2],[5,5],[6,9],[7,14],[8,20]),
136
[N,diagonals],diagonals,5);
137
Determination Coefficient for diagonals = 1.0
140
(%o4) diagonals = --------
142
(%i5) ev(%, N=9); /* Testing for a 9 sides polygon */
146
How many ways do we have to put two queens without they are threatened into a n x n chessboard?
148
(%i6) plsquares(matrix([0,0],[1,0],[2,0],[3,8],[4,44]),
149
[n,positions],[positions],4);
150
Determination Coefficient for [positions] = [1.0]
152
3 n - 10 n + 9 n - 2 n
153
(%o6) [positions = -------------------------]
155
(%i7) ev(%[1], n=8); /* Testing for a (8 x 8) chessboard */
156
(%o7) positions = 1288
159
An example with six dependent variables:
161
(%i8) mtrx:matrix([0,0,0,0,0,1,1,1],[0,1,0,1,1,1,0,0],
162
[1,0,0,1,1,1,0,0],[1,1,1,1,0,0,0,1])$
163
(%i8) plsquares(mtrx,[a,b,_And,_Or,_Xor,_Nand,_Nor,_Nxor],
164
[_And,_Or,_Xor,_Nand,_Nor,_Nxor],1,0);
165
Determination Coefficient for
166
[_And, _Or, _Xor, _Nand, _Nor, _Nxor] =
167
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
168
(%o2) [_And = a b, _Or = - a b + b + a,
169
_Xor = - 2 a b + b + a, _Nand = 1 - a b,
170
_Nor = a b - b - a + 1, _Nxor = 2 a b - b - a + 1]
173
To use this function write first @code{load("lsquares")}.