Taylor Series

Mathematical constants like #tex2html_wrap_inline73374# and e or functions like <#31335#>sin<#31335#>, <#31336#>cos<#31336#>, <#31337#>log<#31337#> are difficult to compute. Since these functions are important for many daily engineering applications, mathematicians have spent a lot of time and energy looking for better ways to compute these functions. One method is to replace a function with its Taylor series, which is, roughly speaking, an infinitely long polynomial. A Taylor series is the sum of a sequence of terms. In contrast to arithmetic or geometric sequences, the terms of a Taylor series depend on two unknowns: some variable <#65879#><#31338#>x<#31338#><#65879#> and the position <#65880#><#31339#>i<#31339#><#65880#> in the sequence. Here is the Taylor series for the exponential function:

#displaymath73378#

That is, if we wish to compute #tex2html_wrap_inline73380# for any specific <#65881#><#31346#>x<#31346#><#65881#>, we replace <#31347#>x<#31347#> with the number and determine the value of the series. In other words, for a specific value of <#65882#><#31348#>x<#31348#><#65882#>, say 1, the Taylor series becomes an ordinary series, <#31349#>i.e.<#31349#>, a sum of some sequence of numbers:

#displaymath73382#

While this series is the sum of an infinitely long sequence, it actually is a number, and it often suffices to add just a few of the first few terms to have an idea what the number is. The key to computing a Taylor series is to formulate each term in the underlying sequence as a function of <#65883#><#31356#>x<#31356#><#65883#> and its position <#65884#><#31357#>i<#31357#><#65884#>. In our running example, the Taylor sequence for the exponential function, has the shape

#displaymath73384#

Assuming a fixed <#65885#><#31360#>x<#31360#><#65885#>, here is an equivalent Scheme definition:

<#71426#>;; <#65886#><#31365#>e-taylor<#31365#> <#31366#>:<#31366#> <#31367#>N<#31367#> <#31368#><#31368#><#31369#>-;SPMgt;<#31369#><#31370#><#31370#> <#31371#>number<#31371#><#65886#><#71426#>
<#31372#>(d<#31372#><#31373#>efine<#31373#> <#31374#>(e-taylor<#31374#> <#31375#>i)<#31375#> 
  <#31376#>(/<#31376#> <#31377#>(expt<#31377#> <#31378#>x<#31378#> <#31379#>i)<#31379#> <#31380#>(!<#31380#> <#31381#>i)))<#31381#> 
<#71427#>;; <#65887#><#31382#>!<#31382#> <#31383#>:<#31383#> <#31384#>N<#31384#> <#31385#><#31385#><#31386#>-;SPMgt;<#31386#><#31387#><#31387#> <#31388#>number<#31388#><#65887#><#71427#> 
<#31389#>(d<#31389#><#31390#>efine<#31390#> <#31391#>(!<#31391#> <#31392#>n)<#31392#> 
  <#31393#>(c<#31393#><#31394#>ond<#31394#> 
    <#31395#>[<#31395#><#31396#>(=<#31396#> <#31397#>n<#31397#> <#31398#>0)<#31398#> <#31399#>1]<#31399#> 
    <#31400#>[<#31400#><#31401#>else<#31401#> <#31402#>(*<#31402#> <#31403#>n<#31403#> <#31404#>(!<#31404#> <#31405#>(sub1<#31405#> <#31406#>n)))]<#31406#><#31407#>))<#31407#> 
The first function computes the term; the second computes the factorial of a natural number. To compute the value of #tex2html_wrap_inline73386#, we now just need to ask for <#65888#><#31411#>(series<#31411#>\ <#31412#>10<#31412#>\ <#31413#>e-taylor)<#31413#><#65888#>, assuming we want the first <#65889#><#31414#>10<#31414#><#65889#> items of the sequence included. Putting everything together, we can define a function that computes the <#65890#><#31415#>x<#31415#><#65890#>th power of <#65891#><#31416#>e<#31416#><#65891#>. Since the function requires two auxiliaries, we use a <#65892#><#31417#>local<#31417#><#65892#>:
<#31422#>(d<#31422#><#31423#>efine<#31423#> <#31424#>(e-power<#31424#> <#31425#>x)<#31425#>
  <#31426#>(l<#31426#><#31427#>ocal<#31427#> <#31428#>(<#31428#><#31429#>(d<#31429#><#31430#>efine<#31430#> <#31431#>(e-taylor<#31431#> <#31432#>i)<#31432#> 
            <#31433#>(/<#31433#> <#31434#>(expt<#31434#> <#31435#>x<#31435#> <#31436#>i)<#31436#> <#31437#>(!<#31437#> <#31438#>i)))<#31438#> 
          <#31439#>(d<#31439#><#31440#>efine<#31440#> <#31441#>(!<#31441#> <#31442#>n)<#31442#> 
            <#31443#>(c<#31443#><#31444#>ond<#31444#> 
              <#31445#>[<#31445#><#31446#>(=<#31446#> <#31447#>n<#31447#> <#31448#>0)<#31448#> <#31449#>1]<#31449#> 
              <#31450#>[<#31450#><#31451#>else<#31451#> <#31452#>(*<#31452#> <#31453#>n<#31453#> <#31454#>(!<#31454#> <#31455#>(sub1<#31455#> <#31456#>n)))]<#31456#><#31457#>)))<#31457#> 
    <#31458#>(series<#31458#> <#31459#>10<#31459#> <#31460#>e-taylor)))<#31460#> 

<#31466#>Exercise 23.3.6<#31466#> Replace <#65893#><#31468#>10<#31468#><#65893#> by <#65894#><#31469#>3<#31469#><#65894#> in the definition of <#65895#><#31470#>e-power<#31470#><#65895#> and evaluate <#65896#><#31471#>(e-power<#31471#>\ <#31472#>1)<#31472#><#65896#> by hand. Show only those lines that contain new applications of <#65897#><#31473#>e-taylor<#31473#><#65897#> to a number. The results of <#65898#><#31474#>e-power<#31474#><#65898#> are fractions with large numerators and denominators. In contrast, Scheme's built-in scheme<#31475#>exp<#31475#> function produces an inexact number. We can turn exact fractions into inexact numbers with the following function
<#72344#>;; <#72244#><#71428#><#65899#><#31480#>exact<#31480#><#65899#><#65900#><#31481#><#31481#><#31482#>-;SPMgt;<#31482#><#31483#><#31483#><#65900#><#65901#><#31484#>inexact<#31484#><#65901#><#71428#> <#31485#>:<#31485#> <#31486#>number<#31486#> <#31487#>[<#31487#><#31488#>exact]<#31488#> <#31489#><#31489#><#31490#>-;SPMgt;<#31490#><#31491#><#31491#> <#31492#>number<#31492#> <#31493#>[<#31493#><#31494#>inexact]<#31494#> <#72244#><#72344#>
Test the function and add it to <#65902#><#31498#>e-power<#31498#><#65902#>'s body. Then compare the results of <#65903#><#31499#>exp<#31499#><#65903#> and <#65904#><#31500#>e-power<#31500#><#65904#>. Increase the number of items in the series until the difference between the results is small.~ external Solution<#65905#><#65905#> <#31506#>Exercise 23.3.7<#31506#> Develop the function <#65906#><#31508#>ln<#31508#><#65906#>, which computes the Taylor series for the natural logarithm. The mathematical definition of the series is

#displaymath73388#

This Taylor series has a value for all <#65907#><#31516#>x<#31516#><#65907#> that are greater than <#65908#><#31517#>0<#31517#><#65908#>. DrScheme also provides <#65909#><#31518#>log<#31518#><#65909#>, a primitive for computing the natural logarithm. Compare the results for <#65910#><#31519#>ln<#31519#><#65910#> and <#65911#><#31520#>log<#31520#><#65911#>. Then use <#72245#><#71429#><#65912#><#31521#>exact<#31521#><#65912#><#65913#><#31522#><#31522#><#31523#>-;SPMgt;<#31523#><#31524#><#31524#><#65913#><#65914#><#31525#>inexact<#31525#><#65914#><#71429#><#72245#> (see exercise~#exetaylor#31526>) to get results that are easier to compare.~ external Solution<#65915#><#65915#> <#31532#>Exercise 23.3.8<#31532#> Develop the function <#65916#><#31534#>my-sin<#31534#><#65916#>, which computes the Taylor series for <#65917#><#31535#>sin<#31535#><#65917#>, one of the trigonometric functions. The Taylor series is defined as follows:

#displaymath73390#

It is defined for all <#65918#><#31545#>x<#31545#><#65918#>. <#31546#>Hint:<#31546#> \ The sign of a term is positive if the index is even and negative otherwise. Programmers use <#65919#><#31547#>cond<#31547#><#65919#> to write this down; mathematicians use the following trick: #tex2html_wrap_inline73392# to compute the sign.~ external Solution<#65920#><#65920#> <#31553#>Exercise 23.3.9<#31553#> Mathematicians have used series to determine the value of #tex2html_wrap_inline73394# for many centuries. Here is the first such sequence, discovered by Gregory (1638--1675):

#displaymath73396#

Define the function <#65921#><#31557#>greg<#31557#><#65921#>, which maps a natural number to the corresponding term in this sequence. Then use <#65922#><#31558#>series<#31558#><#65922#> to determine approximations of the value of #tex2html_wrap_inline73398#.

<#31559#>Note on #tex2html_wrap_inline73400#<#31559#>:\ The approximation improves as we increase the number of items in the series. Unfortunately, it is not practical to compute #tex2html_wrap_inline73402# with this definition.~ external Solution<#65923#><#65923#>