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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
@menu
* Introduction to interpol::
* Functions and Variables for interpol::
@end menu
@node Introduction to interpol, Functions and Variables for interpol, interpol, interpol
@section Introduction to interpol
Package @code{interpol} defines the 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'}.
@opencatbox
@category{Numerical methods} @category{Share packages} @category{Package interpol}
@closecatbox
@node Functions and Variables for interpol, , Introduction to interpol, interpol
@section Functions and Variables 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}.
Note that when working with high degree polynomials, floating point evaluations are unstable.
Examples:
@example
(%i1) load(interpol)$
(%i2) p:[[7,2],[8,2],[1,5],[3,2],[6,7]]$
(%i3) lagrange(p);
(x - 7) (x - 6) (x - 3) (x - 1)
(%o3) -------------------------------
35
(x - 8) (x - 6) (x - 3) (x - 1)
- -------------------------------
12
7 (x - 8) (x - 7) (x - 3) (x - 1)
+ ---------------------------------
30
(x - 8) (x - 7) (x - 6) (x - 1)
- -------------------------------
60
(x - 8) (x - 7) (x - 6) (x - 3)
+ -------------------------------
84
(%i4) f(x):=''%;
(x - 7) (x - 6) (x - 3) (x - 1)
(%o4) f(x) := -------------------------------
35
(x - 8) (x - 6) (x - 3) (x - 1)
- -------------------------------
12
7 (x - 8) (x - 7) (x - 3) (x - 1)
+ ---------------------------------
30
(x - 8) (x - 7) (x - 6) (x - 1)
- -------------------------------
60
(x - 8) (x - 7) (x - 6) (x - 3)
+ -------------------------------
84
(%i5) /* Evaluate the polynomial at some points */
expand(map(f,[2.3,5/7,%pi]));
4 3 2
919062 73 %pi 701 %pi 8957 %pi
(%o5) [- 1.567535, ------, ------- - -------- + ---------
84035 420 210 420
5288 %pi 186
- -------- + ---]
105 5
(%i6) %,numer;
(%o6) [- 1.567535, 10.9366573451538, 2.89319655125692]
(%i7) load(draw)$ /* load draw package */
(%i8) /* Plot the polynomial together with points */
draw2d(
color = red,
key = "Lagrange polynomial",
explicit(f(x),x,0,10),
point_size = 3,
color = blue,
key = "Sample points",
points(p))$
(%i9) /* Change variable name */
lagrange(p, varname=w);
(w - 7) (w - 6) (w - 3) (w - 1)
(%o9) -------------------------------
35
(w - 8) (w - 6) (w - 3) (w - 1)
- -------------------------------
12
7 (w - 8) (w - 7) (w - 3) (w - 1)
+ ---------------------------------
30
(w - 8) (w - 7) (w - 6) (w - 1)
- -------------------------------
60
(w - 8) (w - 7) (w - 6) (w - 3)
+ -------------------------------
84
@end example
@opencatbox
@category{Package interpol}
@closecatbox
@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.
@opencatbox
@category{Package interpol}
@closecatbox
@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);
13 3 x
(%o3) (-- - ---) charfun2(x, minf, 3)
2 2
+ (x - 5) charfun2(x, 7, inf) + (37 - 5 x) charfun2(x, 6, 7)
5 x
+ (--- - 3) charfun2(x, 3, 6)
3
(%i4) f(x):=''%;
13 3 x
(%o4) f(x) := (-- - ---) charfun2(x, minf, 3)
2 2
+ (x - 5) charfun2(x, 7, inf) + (37 - 5 x) charfun2(x, 6, 7)
5 x
+ (--- - 3) charfun2(x, 3, 6)
3
(%i5) /* Evaluate the polynomial at some points */
map(f,[7.3,25/7,%pi]);
62 5 %pi
(%o5) [2.3, --, ----- - 3]
21 3
(%i6) %,numer;
(%o6) [2.3, 2.952380952380953, 2.235987755982989]
(%i7) load(draw)$ /* load draw package */
(%i8) /* Plot the polynomial together with points */
draw2d(
color = red,
key = "Linear interpolator",
explicit(f(x),x,-5,20),
point_size = 3,
color = blue,
key = "Sample points",
points(args(p)))$
(%i9) /* Change variable name */
linearinterpol(p, varname='s);
13 3 s
(%o9) (-- - ---) charfun2(s, minf, 3)
2 2
+ (s - 5) charfun2(s, 7, inf) + (37 - 5 s) charfun2(s, 6, 7)
5 s
+ (--- - 3) charfun2(s, 3, 6)
3
@end example
@opencatbox
@category{Package interpol}
@closecatbox
@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
1159 x 1159 x 6091 x 8283
(%o3) (------- - ------- - ------ + ----) charfun2(x, minf, 3)
3288 1096 3288 1096
3 2
2587 x 5174 x 494117 x 108928
+ (- ------- + ------- - -------- + ------) charfun2(x, 7, inf)
1644 137 1644 137
3 2
4715 x 15209 x 579277 x 199575
+ (------- - -------- + -------- - ------) charfun2(x, 6, 7)
1644 274 1644 274
3 2
3287 x 2223 x 48275 x 9609
+ (- ------- + ------- - ------- + ----) charfun2(x, 3, 6)
4932 274 1644 274
(%i4) f(x):=''%$
(%i5) /* Some evaluations */
map(f,[2.3,5/7,%pi]), numer;
(%o5) [1.991460766423356, 5.823200187269903, 2.227405312429507]
(%i6) load(draw)$ /* load draw package */
(%i7) /* Plotting interpolating function */
draw2d(
color = red,
key = "Cubic splines",
explicit(f(x),x,0,10),
point_size = 3,
color = blue,
key = "Sample points",
points(p))$
(%i8) /* New call, but giving values at the derivatives */
cspline(p,d1=0,dn=0);
3 2
1949 x 11437 x 17027 x 1247
(%o8) (------- - -------- + ------- + ----) charfun2(x, minf, 3)
2256 2256 2256 752
3 2
1547 x 35581 x 68068 x 173546
+ (- ------- + -------- - ------- + ------) charfun2(x, 7, inf)
564 564 141 141
3 2
607 x 35147 x 55706 x 38420
+ (------ - -------- + ------- - -----) charfun2(x, 6, 7)
188 564 141 47
3 2
3895 x 1807 x 5146 x 2148
+ (- ------- + ------- - ------ + ----) charfun2(x, 3, 6)
5076 188 141 47
(%i8) /* Defining new interpolating function */
g(x):=''%$
(%i9) /* Plotting both functions together */
draw2d(
color = black,
key = "Cubic splines (default)",
explicit(f(x),x,0,10),
color = red,
key = "Cubic splines (d1=0,dn=0)",
explicit(g(x),x,0,10),
point_size = 3,
color = blue,
key = "Sample points",
points(p))$
@end example
@opencatbox
@category{Package interpol}
@closecatbox
@end deffn
@deffn {Function} ratinterpol (@var{points}, @var{numdeg})
@deffnx {Function} ratinterpol (@var{points}, @var{numdeg}, @var{option1}, @var{option2}, ...)
Generates a rational interpolator for data given by @var{points} and the degree of the numerator
being equal to @var{numdeg}; the degree of the denominator is calculated
automatically. 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 two options to fit specific needs:
@itemize @bullet
@item
@code{'denterm}, default @code{1}, is the independent term of the polynomial in the denominator.
@item
@code{'varname}, default @code{'x}, is the name of the independent variable.
@end itemize
Examples:
@example
(%i1) load(interpol)$
(%i2) load(draw)$
(%i3) p:[[7.2,2.5],[8.5,2.1],[1.6,5.1],[3.4,2.4],[6.7,7.9]]$
(%i4) for k:0 thru length(p)-1 do
draw2d(
explicit(ratinterpol(p,k),x,0,9),
point_size = 3,
points(p),
title = concat("Degree of numerator = ",k),
yrange=[0,10])$
@end example
@opencatbox
@category{Package interpol}
@closecatbox
@end deffn
|