~ubuntu-branches/debian/squeeze/maxima/squeeze

« back to all changes in this revision

Viewing changes to doc/info/lsquares.texi

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2006-10-18 14:52:42 UTC
  • mto: (1.1.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20061018145242-vzyrm5hmxr8kiosf
ImportĀ upstreamĀ versionĀ 5.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
@menu
 
2
* Definitions for lsquares::
 
3
@end menu
 
4
 
 
5
@node Definitions for lsquares,  , lsquares, lsquares
 
6
@section Definitions for lsquares
 
7
 
 
8
 
 
9
@defvr {Global variable} DETCOEF
 
10
 
 
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). 
 
12
 
 
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.
 
14
 
 
15
See also @code{lsquares}.
 
16
@end defvr
 
17
 
 
18
 
 
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.
 
30
 
 
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.
 
39
 
 
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}.
 
45
 
 
46
Examples using @code{solve}:
 
47
@example
 
48
(%i1) load("lsquares")$
 
49
 
 
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 = ----------------------
 
55
                              6
 
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,
 
58
         [a0,a1,a2,a3,a4]);
 
59
      Determination Coefficient = 1.0
 
60
                     4       3      2
 
61
                  3 n  - 10 n  + 9 n  - 2 n
 
62
(%o3)         p = -------------------------
 
63
                              6
 
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
 
72
                              x
 
73
(%o5)                  y = 3 2  + 1
 
74
@end example
 
75
 
 
76
 
 
77
Examples using @code{mnewton}:
 
78
@example
 
79
(%i6) load("lsquares")$
 
80
 
 
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]);
 
83
                                             x
 
84
(%o7) y = 2.799098974610482 1.999999999999991
 
85
                                        + 1.099999999999874
 
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]);
 
88
                             .4878659755898127
 
89
(%o8) y = 3.177315891123101 x
 
90
                                        + .7723843491402264
 
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]);
 
93
                                                     1/3
 
94
(%o9) y = (3.999999999999862 n + 4.999999999999359 m)
 
95
                                         + 2.00000000000012
 
96
@end example
 
97
 
 
98
To use this function write first @code{load("lsquares")}. See also @code{DETCOEF} and @code{mnewton}.
 
99
@end deffn
 
100
 
 
101
 
 
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.
 
107
 
 
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).
 
109
 
 
110
 
 
111
A simple example of multivariable linear adjustment:
 
112
@example
 
113
(%i1) load("plsquares")$
 
114
 
 
115
(%i2) plsquares(matrix([1,2,0],[3,5,4],[4,7,9],[5,8,10]),
 
116
                [x,y,z],z);
 
117
     Determination Coefficient for z = .9897039897039897
 
118
                       11 y - 9 x - 14
 
119
(%o2)              z = ---------------
 
120
                              3
 
121
@end example
 
122
 
 
123
The same example without degree restrictions:
 
124
@example
 
125
(%i3) plsquares(matrix([1,2,0],[3,5,4],[4,7,9],[5,8,10]),
 
126
                [x,y,z],z,1,0);
 
127
     Determination Coefficient for z = 1.0
 
128
                    x y + 23 y - 29 x - 19
 
129
(%o3)           z = ----------------------
 
130
                              6
 
131
@end example
 
132
 
 
133
How many diagonals does a N-sides polygon have? What polynomial degree should be used?
 
134
@example
 
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
 
138
                                2
 
139
                               N  - 3 N
 
140
(%o4)              diagonals = --------
 
141
                                  2
 
142
(%i5) ev(%, N=9);   /* Testing for a 9 sides polygon */
 
143
(%o5)                 diagonals = 27
 
144
@end example
 
145
 
 
146
How many ways do we have to put two queens without they are threatened into a n x n chessboard?
 
147
@example
 
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]
 
151
                         4       3      2
 
152
                      3 n  - 10 n  + 9 n  - 2 n
 
153
(%o6)    [positions = -------------------------]
 
154
                                  6
 
155
(%i7) ev(%[1], n=8); /* Testing for a (8 x 8) chessboard */
 
156
(%o7)                positions = 1288
 
157
@end example
 
158
 
 
159
An example with six dependent variables:
 
160
@example
 
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]
 
171
@end example
 
172
 
 
173
To use this function write first @code{load("lsquares")}.
 
174
@end deffn
 
175