File: interpol.texi

package info (click to toggle)
maxima 5.10.0-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 44,268 kB
  • ctags: 17,987
  • sloc: lisp: 152,894; fortran: 14,667; perl: 14,204; tcl: 10,103; sh: 3,376; makefile: 2,202; ansic: 471; awk: 7
file content (213 lines) | stat: -rw-r--r-- 7,942 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
@menu
* Introduction to interpol::
* Definitions for interpol::
@end menu

@node Introduction to interpol, Definitions for interpol, interpol, interpol
@section Introduction to interpol

Package @code{interpol} defines de Lagrangian, the linear and the cubic 
splines methods for polynomial interpolation.



For comments, bugs or suggestions, please contact me at @var{'mario AT edu DOT xunta DOT es'}.



@node Definitions for interpol,  , Introduction to interpol, interpol
@section Definitions for interpol


@deffn {Function} lagrange (@var{points})
@deffnx {Function} lagrange (@var{points}, @var{option})
Computes the polynomial interpolation by the Lagrangian method. Argument @var{points} must be either:

@itemize @bullet
@item
a two column matrix, @code{p:matrix([2,4],[5,6],[9,3])},
@item
a list of pairs, @code{p: [[2,4],[5,6],[9,3]]},
@item
a list of numbers, @code{p: [4,6,3]}, in which case the abscissas will be assigned automatically to 1, 2, 3, etc.
@end itemize

In the first two cases the pairs are ordered with respect to the first coordinate before making computations.

With the @var{option} argument it is possible to select the name for the independent variable, which is @code{'x} by default; to define another one, write something like @code{varname='z}. 

Examples:

@example
(%i1) load("interpol")$
(%i2) p:[[7,2],[8,2],[1,5],[3,2],[6,7]]$
(%i3) lagrange(p);
            4         3         2
        73 x  - 1402 x  + 8957 x  - 21152 x + 15624
(%o3)   -------------------------------------------
                            420
(%i4) f(x):=''%;
                  4         3         2
              73 x  - 1402 x  + 8957 x  - 21152 x + 15624
(%o4) f(x) := -------------------------------------------
                                  420
(%i5) /* Evaluate the polynomial at some points */
      map(f,[2.3,5/7,%pi]);
                            919062
(%o5) [- 1.567535000000005, ------,
                            84035
              4           3           2
        73 %pi  - 1402 %pi  + 8957 %pi  - 21152 %pi + 15624
        ---------------------------------------------------]
                                420
(%i6) %,numer;
(%o6) [- 1.567535000000005, 10.9366573451538,
                                           2.89319655125692]
(%i7) /* Plot the polynomial together with points */
      plot2d([f(x),[discrete,p]],[x,0,10],
           [gnuplot_curve_styles,
                 ["with lines","with points pointsize 3"]])$
(%i8) /* Change variable name */
      lagrange(p, varname=w);
            4         3         2
        73 w  - 1402 w  + 8957 w  - 21152 w + 15624
(%o8)  -------------------------------------------
                            420
@end example

@end deffn


@deffn {Function} charfun2 (@var{x}, @var{a}, @var{b})
Returns @code{true} if number @var{x} belongs to the interval @math{[a, b)}, and @code{false} otherwise.
@end deffn


@deffn {Function} linearinterpol (@var{points})
@deffnx {Function} linearinterpol (@var{points}, @var{option})
Computes the polynomial interpolation by the linear method. Argument @var{points} must be either:

@itemize @bullet
@item
a two column matrix, @code{p:matrix([2,4],[5,6],[9,3])},
@item
a list of pairs, @code{p: [[2,4],[5,6],[9,3]]},
@item
a list of numbers, @code{p: [4,6,3]}, in which case the abscissas will be assigned automatically to 1, 2, 3, etc.
@end itemize

In the first two cases the pairs are ordered with respect to the first coordinate before making computations.

With the @var{option} argument it is possible to select the name for the independent variable, which is @code{'x} by default; to define another one, write something like @code{varname='z}. 

Examples:
@example
(%i1) load("interpol")$
(%i2) p: matrix([7,2],[8,3],[1,5],[3,2],[6,7])$
(%i3) linearinterpol(p);
(%o3) - ((9 x - 39) charfun2(x, minf, 3)
 + (30 - 6 x) charfun2(x, 7, inf)
 + (30 x - 222) charfun2(x, 6, 7)
 + (18 - 10 x) charfun2(x, 3, 6))/6
(%i4) f(x):=''%;
(%o4) f(x) := - ((9 x - 39) charfun2(x, minf, 3)
 + (30 - 6 x) charfun2(x, 7, inf)
 + (30 x - 222) charfun2(x, 6, 7)
 + (18 - 10 x) charfun2(x, 3, 6))/6
(%i5)  /* Evaluate the polynomial at some points */
       map(f,[7.3,25/7,%pi]);
                        62    18 - 10 %pi
(%o5)            [2.3, --, - -----------]
                        21         6
(%i6) %,numer;
(%o6)  [2.3, 2.952380952380953, 2.235987755982988]
(%i7)  /* Plot the polynomial together with points */
       plot2d(['(f(x)),[discrete,args(p)]],[x,-5,20],
           [gnuplot_curve_styles,
                 ["with lines","with points pointsize 3"]])$
(%i8)  /* Change variable name */
       linearinterpol(p, varname='s);
(%o8) - ((9 s - 39) charfun2(s, minf, 3)
 + (30 - 6 s) charfun2(s, 7, inf)
 + (30 s - 222) charfun2(s, 6, 7)
 + (18 - 10 s) charfun2(s, 3, 6))/6
@end example

@end deffn



@deffn {Function} cspline (@var{points})
@deffnx {Function} cspline (@var{points}, @var{option1}, @var{option2}, ...)
Computes the polynomial interpolation by the cubic splines method. Argument @var{points} must be either:

@itemize @bullet
@item
a two column matrix, @code{p:matrix([2,4],[5,6],[9,3])},
@item
a list of pairs, @code{p: [[2,4],[5,6],[9,3]]},
@item
a list of numbers, @code{p: [4,6,3]}, in which case the abscissas will be assigned automatically to 1, 2, 3, etc.
@end itemize

In the first two cases the pairs are ordered with respect to the first coordinate before making computations.

There are three options to fit specific needs:
@itemize @bullet
@item
@code{'d1}, default @code{'unknown}, is the first derivative at @math{x_1}; if it is @code{'unknown}, the second derivative at @math{x_1} is made equal to 0 (natural cubic spline); if it is equal to a number, the second derivative is calculated based on this number.

@item
@code{'dn}, default @code{'unknown}, is the first derivative at @math{x_n}; if it is @code{'unknown}, the second derivative at @math{x_n} is made equal to 0 (natural cubic spline); if it is equal to a number, the second derivative is calculated based on this number.

@item
@code{'varname}, default @code{'x}, is the name of the independent variable.
@end itemize

Examples:
@example
(%i1) load("interpol")$
(%i2) p:[[7,2],[8,2],[1,5],[3,2],[6,7]]$
(%i3) /* Unknown first derivatives at the extremes
         is equivalent to natural cubic splines */
      cspline(p);
              3          2
(%o3) ((3477 x  - 10431 x  - 18273 x + 74547)
                                  3           2
 charfun2(x, minf, 3) + (- 15522 x  + 372528 x  - 2964702 x
 + 7842816) charfun2(x, 7, inf)
           3           2
 + (28290 x  - 547524 x  + 3475662 x - 7184700)
                              3          2
 charfun2(x, 6, 7) + (- 6574 x  + 80028 x  - 289650 x
 + 345924) charfun2(x, 3, 6))/9864
(%i4) f(x):=''%$
(%i5) /* Some evaluations */
      map(f,[2.3,5/7,%pi]), numer;
(%o5) [1.991460766423358, 5.823200187269904,
                                          2.227405312429501]
(%i6) /* Plotting interpolating function */
      plot2d(['(f(x)),[discrete,p]],[x,0,10],
          [gnuplot_curve_styles,
               ["with lines","with points pointsize 3"]])$
(%i7) /* New call, but giving values at the derivatives */
      cspline(p,d1=0,dn=0);
               3           2
(%o7) ((17541 x  - 102933 x  + 153243 x + 33669)
                                  3            2
 charfun2(x, minf, 3) + (- 55692 x  + 1280916 x  - 9801792 x
 + 24990624) charfun2(x, 7, inf)
           3            2
 + (65556 x  - 1265292 x  + 8021664 x - 16597440)
                               3           2
 charfun2(x, 6, 7) + (- 15580 x  + 195156 x  - 741024 x
 + 927936) charfun2(x, 3, 6))/20304
(%i8) /* Defining new interpolating function */
      g(x):=''%$
(%i9) /* Plotting both functions together */
      plot2d(['(f(x)),'(g(x)),[discrete,p]],[x,0,10],
           [gnuplot_curve_styles,
              ["with lines","with lines","with points pointsize 3"]])$
@end example

@end deffn