Newsgroups: comp.sys.hp48
From: bd628@torfree.net (Bill Markwick)
Subject: Taylor Series and the HP48 (long)
Message-ID: <F6yCvH.JKn.0.queen@torfree.net>
Organization: Toronto Free-Net
X-Newsreader: TIN [version 1.2 PL2]
Date: Wed, 10 Feb 1999 19:02:04 GMT
Lines: 334
Xref: republic.btigate.com comp.sys.hp48:21669

  A brief discussion of Taylor polynomials, plus the TAYLOR program to
  simplify the HP48's approach, especially when translating away from
  zero.
                     Bill Markwick  (bd628@torfree.net)  Feb. 10, 1999


This is a whirlwind tour of the Taylor series expansions, with demos you
can try on the HP48. This is anything but an academic work - teachers
will be white-knuckled at my omissions and leaps of logic - but you might
come out the other end with some useful ideas about the series.

Back in pre-calculator days, some method was needed to simplify the
processing of complex functions, and Brook Taylor (1685-1731) came up
with a way of deriving simple approximations to most functions.  They
can be evaluated just by solving a straightforward power series.

Here's the power series that Taylor started with:

  x + x^2 + x^3 + x^4 + ...

By adding suitable coefficients, this series can take just about any
shape when plotted (you'll have to mentally insert "times" signs and
subscripts and so on):

  a0 + a1x + a2x^2 + a3x^3 + a4x^4 + ...

If you plot 'x^n' with even exponents, you get a sort of fat parabola.
If you plot with odd exponents, you get something like a stretched-out
backwards "S".  Between the two, it's possible to approximate most (but
not all) functions over a fairly wide range. (Note that if a series has
all-odd or all-even exponents, it just means that the coefficients of
the missing terms were zeros.)


A Quick Look
------------

Before plunging into theory, here's a graphical demonstration of how it
works.  Below is a list containing the sin(x) function to be approximated
and its 3rd-order Taylor polynomial:

  { 'SIN(X)' 'X-1/3!*X^3' }

Key it in, or if you're feeling particularly inert, snip it and transfer
it.  Store it in EQ and plot it with the default settings.  First you'll
see the familiar sinewave.  Then a line will descend from the upper left,
converge on the sinewave at about x=-1.5, and follow it along until
exiting for the bottom right at about x=1.5.


Accuracy
--------

The Taylor series does a remarkably good job of approximating the
function on either side of zero if you don't go too far (why that's so
is explained under the "Deriving" section).  Actually, the low screen
resolution creates the illusion that the two curves are coincident over
a wide range - even zooming in tightly seems to confirm this - but in
fact divergence starts as soon as you leave zero.  Here's a look at the
accuracy for the 3rd-order series:

      x        sin(x)     Taylor(x)
    ----     ---------    ---------
      0              0            0
    .15      .14943813    .14943750
     .5      .47942553    .47916666
      1      .84147098    .83333333
    1.5      .99749498    .93749999

As you can see, the accuracy strays more and more as you move away from
zero, although for practical applications, it's not bad to about 0.5.
(The program at the end will find the numerical Taylor values if you put
the desired x value into the Num: field.)

As you increase the number of terms, the approximation will converge to
the function more rapidly.  Try putting SIN(X) and order 15 into the
TAYLOR program and then plotting the result.  It looks exactly like a
plot of SIN(X); zoom out and you'll see the sudden divergence at about
+/- 8. Orders 11 and 13 show some divergence at about +/- 5 or
6 on the x-axis.

Textbooks with lots of detail on the Taylor Theorem will usually give a
method of finding the error term for deriving the upper bound of the
error.

Incidentally, the series about x=0 is known as the Maclaurin series.
Colin Maclaurin (1698-1746) pointed out that Taylor had already done all
the work and that the x=0 series is just a special case of the Taylor,
but the name Maclaurin stuck. (Just as Napier didn't devise the base e
logarithms that bear his name - that was Euler - but that's how it goes
in the accolade biz.  Taylor didn't get any applause for the Taylor
Theorem until after he was long gone.)


What Good Are They?
-------------------
The benefits for calculator owners are in approximating integrals that
RS-Symbolic can't do (and there are lots of those), plus taking a
function apart to learn about it.

Integration Example: The HP48 can't integrate sin(x)^2, so make a
7th-order Taylor series of it. It'll take about two minutes. Store it in
EXPR and run RS-Symbolic-Integrate with limits 0 and X and there's your
indefinite integral, or at least a workable approximation of it. Use
OBJ-> and then drop all but one object. COLCT will simplify it.


Deriving the sin(x) Series About Zero
-------------------------------------

If you're looking for an approximation to a function and you've got the
versatile power series listed at the beginning, derivatives are a really
good next step: the function and the derivative share a common point and
a common slope at that point. The coefficients can be derived by
differentiation.

Start with the power series:

  f(x) = a0 + a1x + a2x^2 + a3x^3 + a4x^4 + ...

Let x=0 and everything vanishes except A0, a constant. Its glimmer of
fame is extinguished because f(0) now equals only a0, and sin(0) = 0.
While we're at it, let's list the first few derivatives of sin(x) at
x=0:

         sin(0) =  0
  d1  =  cos(0) =  1
  d2  = -sin(0) =  0
  d3  = -cos(0) = -1
  d4  =  sin(0) =  0  and now it starts repeating if we keep going.


Now differentiate the series term by term and we get:

  f'(x) = a1 + 2a2x + 3a3x^2 + 4a4x^3 + ...

Set x=0 and the constant a1 is left; f'(0) = a1 and f'(0) = cos(0) = 1.


Differentiate the series again and set x=0:

  f''(x) = 2a2 + 6a3x + 12a4x^2 + ...

And the constant 2a2 is left; f''(0) = 2a2 = -sin(0) = 0, so a2 = 0/2 = 0.


Differentiate the series again and set x=0 and we're left with the
constant 6a3.  f'''(0) = 6a3 = -1, so a3 = -1/6.

And so on. Eventually we're left with the list of coefficients:

  a0  =  0
  a1  =  1
  a2  =  0
  a3  = -1/6
  a4  =  0
  a5  =  1/120

(These are called "Taylor coefficients", a handy thing to know at
parties.)

When deriving the coefficients, you'll notice that they have the form
3*2*1 and 5*4*3*2*1 - factorials. It's customary to use factorial
notation, so 6 becomes 3! and 120 becomes 5! Yes!

Now plug the coefficients into the original series:

  0 + 1x + 0x^2 - x^3/3! + 0x^4 - x^5/5! - ... and simplify <drumroll>

  sin(x) =  x - x^3/3! + x^5/5!

We have a 5th-order Taylor/Maclaurin series polynomial approximation
expansion (like, whatever). The equals sign should really be the
"approximately equal to" sign. Notice that the even-exponent terms have
dropped out.


    The general Taylor series expansion formula for f(x) moved
    away from zero by 'a' (more on this below) is:

      f(x) = f(a) + f'(a)(x-a) + (1/2!)f''(a)(x-a)^2 + ... +
      (1/n!)f[n](a)(x-a)^n-1

      where f[n] means the nth derivative.

    There's also an error term tacked on at the end, but that's
    for another day.


Functions You Can't Use
-----------------------

Not all functions work with the approximation method.  Obviously from
the above they have to be differentiable at zero; ln(x) isn't (ln(0) =
tilt! = "Infinite Result").  They have to be continuous on the interval
of interest; sin(x)/x isn't (plot it without the axes and you'll see the
one-pixel discontinuity at x=0). They can't have cusps (peaks or dips
that look like intersections of curves) where the slope suddenly goes
infinite (x^(2/3) has one at x=0).

But you can still get approximations for many functions by moving them
away from zero (all three of the above troublemakers, for instance).


Away From Zero:
---------------

If the discombobulations happen only at zero, you can still derive an
approximation by translating the function away from zero.  As you can
see from the general Taylor series listed previously, it has (x-a) where
'x' would appear in the Maclaurin series.  The 'a' is the value away
from zero where you want to approximate a function.  Here's the
3rd-order list from the start of the article, translated to x=1 by my
TAYLOR program and using the HP48's FIX 2 to condense it:

  { 'SIN(X)' '0.84+0.54*(X-1)-0.42*(X-1)^2-0.54/3!*(X-1)^3' }

As you can see, everything is a bit more complicated because terms that
conveniently departed when x equalled zero are now standing proud.  Some
signs have changed.  If you plot this list, you'll see the convergence
just to the left of the origin, diverging again about x=2; the function
and the approximation are coincident at x=1.  If you generate the series
with a=1 and order 7, the convergence is faster and so the interval is
wider.

Both RS-Symbolic-Taylor and the TAYLOR program use the same method
(although different means): replace f(x) with f(x+a), make the
series, and substitute (x-a) for x in the series.



The TAYLOR Program
------------------

The TAYLOR program offers the same features as the HP48's Taylor
Polynomials, and it allows you to translate away from zero with one
entry, as opposed to the 48's rather awkward side-trips to the stack.
UsrRPL is a tad slow, but it's still faster than going through
RS-Symbolic-Taylor, especially since the defaults are filled in.
  Let me know about any bugs and I'll fix them.

The opening screen:

  EXP: contains the contents of the global variable EXPR if it exists;
  if not, it will be blank. In either case you can key in another
  expression (you don't need to press Edit unless you want to).

    NOTE: the variable EXPR is *not* overwritten by the program.

    (Don't worry about messing up the keying in - if you lose hope,
    press NXT-Reset-Reset All and you'll be back where you started.)

  VAR: the variable of approximation; default X.

  ORD: the order of the highest-order term in the series; default 3.
  Higher orders give better accuracy, but take longer to process.

  A: the number used to translate the approximation away from zero; the
  default is a blank field. If you insert 2, for example, the
  approximation is derived using f(X+2) and all occurrences of X in the
  final approximation will be (X-2) as required by the Taylor series.

  NUM: a real number in this field will give numerical results for that
  number. The Taylor approximation is derived according to the other
  fields and then solved for NUM's value; the default is blank. Recall
  that accuracy decreases with increasing difference between NUM and
  zero (or A:).

Errors: error messages appear as a string on level one.

Other: - the program uses matching rather than saving and purging; no
         existing variables will be affected.
       - The program doesn't use COLCT - you might want to use it after
         for simplifying large expressions.
       - On the stack, the checksum is #2330h and the size is 672.5 bytes.


%%HP: T(3)A(R)F(.);
\<< DEPTH RCLF \-> DP             @ Save stack depth and flags
FL
  \<<
    IFERR                         @ Error-handler for TAYLR aborts
      IF                          @ Error-handler for CANCL/ON aborts
"Taylor Approximation"
{ { "Exp:"                        @ Lists for INFORM fields
"EXPR, or enter expression"
9 } { } { "Var:"
"Variable of approx."
9 } { "Ord:"
"Highest order term in series"
0 } { "a:"
"Value of translation from zero"
0 } { "Num:"
"Enter real for numerical result"
0 } } { 2 4 } {
NOVAL X 3 0 NOVAL }               @ Reset values list
        IFERR
1 'EXPR' RCL PUT                  @ Insert EXPR contents as element 1
        THEN 2                    @ or if no EXPR, tidy up the stack
DROPN
        END DUP                   @ Dupe reset list for initial values
INFORM                            @ Run INFORM
      THEN OBJ\->                 @ If it worked, THEN do the rest:
DROP \-> v o a n
        \<< -22 CF a              @ Clear infinite->err flag
          IF 0 ==                 @ If A: field is zero,
          THEN v o                @ Do a straightforward TAYLR
TAYLR
          ELSE v v                @ If A: is non-zero, make {var+a}
a + 2 \->LIST \|vMATCH            @ and sub in the function
DROP v o TAYLR v v                @ Do TAYLR
a - 2 \->LIST \|vMATCH            @ Make {var-a} and sub in series
DROP
          END n
          IF NOVAL                @ If n is empty, that's it: exit
\=/
          THEN v n                @ If there's a number, make {var n}
2 \->LIST |                       @ and solve series for n with "where"
          END
        \>>                       @ End main program
      ELSE DEPTH DP               @ Exit if INFORM cancelled - clean
- DROPN FL STOF                   @ up stack and reset flags
      END
    THEN DEPTH DP -               @ Exit on TAYLR error; reset stack, flags
DROPN ERRM FL STOF                @ and print error message on stack
    END FL STOF                   @ Normal exit - reset flags
  \>>
\>>

                              --- End ---


-- 
- Bill Markwick,  Toronto, Ontario, Canada  -  bd628@torfree.net