Previous: Введение в пакет interpol, Up: Пакет interpol   [Contents][Index]

54.2 Функции и переменные пакета interpol

Функция: lagrange (points)
Функция: lagrange (points, option)

Вычисляет полиномиальную интерполяцию методом Лагранжа. Аргумент points должен быть либо:

В первых двух случаях, перед проведением вычислений, пары упорядочиваются в соответствии со значением первой координаты.

Аргумент option позволяет выбрать имя независимой переменной, по умолчанию равной 'x. Чтобы задать другую переменную напишите varname='z.

Отметим, что при работе с многочленами высокого порядка, вычисления с плавающей точкой нестабильны.

Примеры:

(%i1) load("interpol")$
(%i2) p:[[7,2],[8,2],[1,5],[3,2],[6,7]]$
(%i3) lagrange(p);
                 4        3         2
             73 x    701 x    8957 x    5288 x   186
(%o3)        ----- - ------ + ------- - ------ + ---
              420     210       420      105      5
(%i4) f(x):=''%;
                     4        3         2
                 73 x    701 x    8957 x    5288 x   186
(%o4)    f(x) := ----- - ------ + ------- - ------ + ---
                  420     210       420      105      5
(%i5) /* Evaluate the polynomial at some points */
      map(f,[2.3,5/7,%pi]);
                             919062
(%o5)  [- 1.567534999999992, ------,
                             84035
                         4          3           2
                   73 %pi    701 %pi    8957 %pi    5288 %pi   186
                   ------- - -------- + --------- - -------- + ---]
                     420       210         420        105       5
(%i6) %,numer;
(%o6) [- 1.567534999999992, 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);
                 4        3         2
             73 w    701 w    8957 w    5288 w   186
(%o9)        ----- - ------ + ------- - ------ + ---
              420     210       420      105      5
Функция: charfun2 (x, a, b)

Возвращает true если число x принадлежит интервалу [a, b), и false в противном случае.

Функция: linearinterpol (points)
Функция: linearinterpol (points, option)

Вычисляет полиномиальную интерполяцию линейным методом. Аргумент points должен быть либо:

В первых двух случаях, перед проведением вычислений, пары упорядочиваются в соответствии со значением первой координаты.

Аргумент option позволяет выбрать имя независимой переменной, по умолчанию равной 'x. Чтобы задать другую переменную напишите varname='z.

Примеры:

(%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
Функция: cspline (points)
Функция: cspline (points, option1, option2, ...)

Вычисляет полиномиальную интерполяцию методом кубических сплайнов. Аргумент points должен быть либо:

В первых двух случаях, перед проведением вычислений, пары упорядочиваются в соответствии со значением первой координаты.

Существует три специальные опции:

Примеры:

(%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))$

Previous: Введение в пакет interpol, Up: Пакет interpol   [Contents][Index]