1 @c -----------------------------------------------------------------------------
2 @c File : interpol.de.texi
3 @c License : GNU General Public License (GPL)
5 @c Author : Dr. Dieter Kaiser
8 @c This file is part of Maxima -- GPL CAS based on DOE-MACSYMA
9 @c -----------------------------------------------------------------------------
12 * Introduction to interpol::
13 * Functions and Variables for interpol::
16 @c -----------------------------------------------------------------------------
17 @node Introduction to interpol, Functions and Variables for interpol, interpol, interpol
18 @section Introduction to interpol
20 Package @code{interpol} defines the Lagrangian, the linear and the cubic
21 splines methods for polynomial interpolation.
23 For comments, bugs or suggestions, please contact me at @var{'mario AT edu DOT xunta DOT es'}.
26 @c @category{Numerical methods} @category{Share packages} @category{Package interpol}
29 @c -----------------------------------------------------------------------------
30 @node Functions and Variables for interpol, , Introduction to interpol, interpol
31 @section Functions and Variables for interpol
33 @c -----------------------------------------------------------------------------
34 @deffn {Function} lagrange (@var{points})
35 @deffnx {Function} lagrange (@var{points}, @var{option})
37 Computes the polynomial interpolation by the Lagrangian method. Argument @var{points} must be either:
41 a two column matrix, @code{p:matrix([2,4],[5,6],[9,3])},
43 a list of pairs, @code{p: [[2,4],[5,6],[9,3]]},
45 a list of numbers, @code{p: [4,6,3]}, in which case the abscissas will be assigned automatically to 1, 2, 3, etc.
48 In the first two cases the pairs are ordered with respect to the first coordinate before making computations.
50 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}.
52 Note that when working with high degree polynomials, floating point evaluations are unstable.
57 (%i1) load("interpol")$
58 (%i2) p:[[7,2],[8,2],[1,5],[3,2],[6,7]]$
60 (x - 7) (x - 6) (x - 3) (x - 1)
61 (%o3) -------------------------------
63 (x - 8) (x - 6) (x - 3) (x - 1)
64 - -------------------------------
66 7 (x - 8) (x - 7) (x - 3) (x - 1)
67 + ---------------------------------
69 (x - 8) (x - 7) (x - 6) (x - 1)
70 - -------------------------------
72 (x - 8) (x - 7) (x - 6) (x - 3)
73 + -------------------------------
76 (x - 7) (x - 6) (x - 3) (x - 1)
77 (%o4) f(x) := -------------------------------
79 (x - 8) (x - 6) (x - 3) (x - 1)
80 - -------------------------------
82 7 (x - 8) (x - 7) (x - 3) (x - 1)
83 + ---------------------------------
85 (x - 8) (x - 7) (x - 6) (x - 1)
86 - -------------------------------
88 (x - 8) (x - 7) (x - 6) (x - 3)
89 + -------------------------------
91 (%i5) /* Evaluate the polynomial at some points */
92 expand(map(f,[2.3,5/7,%pi]));
94 919062 73 %pi 701 %pi 8957 %pi
95 (%o5) [- 1.567535, ------, ------- - -------- + ---------
101 (%o6) [- 1.567535, 10.9366573451538, 2.89319655125692]
102 (%i7) load("draw")$ /* load draw package */
103 (%i8) /* Plot the polynomial together with points */
106 key = "Lagrange polynomial",
107 explicit(f(x),x,0,10),
110 key = "Sample points",
112 (%i9) /* Change variable name */
113 lagrange(p, varname=w);
114 (w - 7) (w - 6) (w - 3) (w - 1)
115 (%o9) -------------------------------
117 (w - 8) (w - 6) (w - 3) (w - 1)
118 - -------------------------------
120 7 (w - 8) (w - 7) (w - 3) (w - 1)
121 + ---------------------------------
123 (w - 8) (w - 7) (w - 6) (w - 1)
124 - -------------------------------
126 (w - 8) (w - 7) (w - 6) (w - 3)
127 + -------------------------------
132 @c @category{Package interpol}
136 @c -----------------------------------------------------------------------------
137 @deffn {Function} charfun2 (@var{x}, @var{a}, @var{b})
139 Returns @code{true} if number @var{x} belongs to the interval @math{[a, b)},
140 and @code{false} otherwise.
143 @c @category{Package interpol}
147 @c -----------------------------------------------------------------------------
148 @deffn {Function} linearinterpol (@var{points})
149 @deffnx {Function} linearinterpol (@var{points}, @var{option})
151 Computes the polynomial interpolation by the linear method. Argument
152 @var{points} must be either:
156 a two column matrix, @code{p:matrix([2,4],[5,6],[9,3])},
158 a list of pairs, @code{p: [[2,4],[5,6],[9,3]]},
160 a list of numbers, @code{p: [4,6,3]}, in which case the abscissas will be assigned automatically to 1, 2, 3, etc.
163 In the first two cases the pairs are ordered with respect to the first coordinate before making computations.
165 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}.
169 (%i1) load("interpol")$
170 (%i2) p: matrix([7,2],[8,3],[1,5],[3,2],[6,7])$
171 (%i3) linearinterpol(p);
173 (%o3) (-- - ---) charfun2(x, minf, 3)
175 + (x - 5) charfun2(x, 7, inf) + (37 - 5 x) charfun2(x, 6, 7)
177 + (--- - 3) charfun2(x, 3, 6)
182 (%o4) f(x) := (-- - ---) charfun2(x, minf, 3)
184 + (x - 5) charfun2(x, 7, inf) + (37 - 5 x) charfun2(x, 6, 7)
186 + (--- - 3) charfun2(x, 3, 6)
188 (%i5) /* Evaluate the polynomial at some points */
189 map(f,[7.3,25/7,%pi]);
191 (%o5) [2.3, --, ----- - 3]
194 (%o6) [2.3, 2.952380952380953, 2.235987755982989]
195 (%i7) load("draw")$ /* load draw package */
196 (%i8) /* Plot the polynomial together with points */
199 key = "Linear interpolator",
200 explicit(f(x),x,-5,20),
203 key = "Sample points",
205 (%i9) /* Change variable name */
206 linearinterpol(p, varname='s);
208 (%o9) (-- - ---) charfun2(s, minf, 3)
210 + (s - 5) charfun2(s, 7, inf) + (37 - 5 s) charfun2(s, 6, 7)
212 + (--- - 3) charfun2(s, 3, 6)
217 @c @category{Package interpol}
221 @c -----------------------------------------------------------------------------
222 @deffn {Function} cspline (@var{points})
223 @deffnx {Function} cspline (@var{points}, @var{option1}, @var{option2}, ...)
225 Computes the polynomial interpolation by the cubic splines method. Argument
226 @var{points} must be either:
230 a two column matrix, @code{p:matrix([2,4],[5,6],[9,3])},
232 a list of pairs, @code{p: [[2,4],[5,6],[9,3]]},
234 a list of numbers, @code{p: [4,6,3]}, in which case the abscissas will be assigned automatically to 1, 2, 3, etc.
237 In the first two cases the pairs are ordered with respect to the first coordinate before making computations.
239 There are three options to fit specific needs:
242 @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.
245 @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.
248 @code{'varname}, default @code{'x}, is the name of the independent variable.
253 (%i1) load("interpol")$
254 (%i2) p:[[7,2],[8,2],[1,5],[3,2],[6,7]]$
255 (%i3) /* Unknown first derivatives at the extremes
256 is equivalent to natural cubic splines */
259 1159 x 1159 x 6091 x 8283
260 (%o3) (------- - ------- - ------ + ----) charfun2(x, minf, 3)
263 2587 x 5174 x 494117 x 108928
264 + (- ------- + ------- - -------- + ------) charfun2(x, 7, inf)
267 4715 x 15209 x 579277 x 199575
268 + (------- - -------- + -------- - ------) charfun2(x, 6, 7)
271 3287 x 2223 x 48275 x 9609
272 + (- ------- + ------- - ------- + ----) charfun2(x, 3, 6)
276 (%i5) /* Some evaluations */
277 map(f,[2.3,5/7,%pi]), numer;
278 (%o5) [1.991460766423356, 5.823200187269903, 2.227405312429507]
279 (%i6) load("draw")$ /* load draw package */
280 (%i7) /* Plotting interpolating function */
283 key = "Cubic splines",
284 explicit(f(x),x,0,10),
287 key = "Sample points",
289 (%i8) /* New call, but giving values at the derivatives */
290 cspline(p,d1=0,dn=0);
292 1949 x 11437 x 17027 x 1247
293 (%o8) (------- - -------- + ------- + ----) charfun2(x, minf, 3)
296 1547 x 35581 x 68068 x 173546
297 + (- ------- + -------- - ------- + ------) charfun2(x, 7, inf)
300 607 x 35147 x 55706 x 38420
301 + (------ - -------- + ------- - -----) charfun2(x, 6, 7)
304 3895 x 1807 x 5146 x 2148
305 + (- ------- + ------- - ------ + ----) charfun2(x, 3, 6)
307 (%i8) /* Defining new interpolating function */
309 (%i9) /* Plotting both functions together */
312 key = "Cubic splines (default)",
313 explicit(f(x),x,0,10),
315 key = "Cubic splines (d1=0,dn=0)",
316 explicit(g(x),x,0,10),
319 key = "Sample points",
324 @c @category{Package interpol}
328 @c -----------------------------------------------------------------------------
329 @deffn {Function} ratinterpol (@var{points}, @var{numdeg})
330 @deffnx {Function} ratinterpol (@var{points}, @var{numdeg}, @var{option1})
332 Generates a rational interpolator for data given by @var{points} and the degree of the numerator
333 being equal to @var{numdeg}; the degree of the denominator is calculated
334 automatically. Argument @var{points} must be either:
338 a two column matrix, @code{p:matrix([2,4],[5,6],[9,3])},
340 a list of pairs, @code{p: [[2,4],[5,6],[9,3]]},
342 a list of numbers, @code{p: [4,6,3]}, in which case the abscissas will be assigned automatically to 1, 2, 3, etc.
345 In the first two cases the pairs are ordered with respect to the first coordinate before making computations.
347 There is one option to fit specific needs:
350 @code{'varname}, default @code{'x}, is the name of the independent variable.
356 (%i1) load("interpol")$
358 (%i3) p:[[7.2,2.5],[8.5,2.1],[1.6,5.1],[3.4,2.4],[6.7,7.9]]$
359 (%i4) for k:0 thru length(p)-1 do
361 explicit(ratinterpol(p,k),x,0,9),
364 title = concat("Degree of numerator = ",k),
369 @c @category{Package interpol}
373 @c --- End of file interpol.de.texi --------------------------------------------