UC SCI 111, January 28, 2004
Notebook 2

Taylor[args__] := Normal[Series[args]]

Mathematica Prerequisites.

Derivatives: D.

Suppose we have a function f (enter the cell below):

f[x_] := (x^10)

We calculate the derivative with the command

D[f[x], x]

Alternatively, in cases like this where there is only one variable present (x in this case), one may use the notation:

f'[x]

And for those who like the palette, one may also do this:

(∂_x) f[x]

We can calculate a second derivative in at least four ways:

D[D[f[x], x], x]

f''[x]

(∂_(x, x))f[x]

D[f[x], {x, 2}]

The last notation is best when higher derivatives are called for. For instance, the tenth derivative is

D[f[x], {x, 10}]

Note that this value can also be written 10! (x^0), as a hand-calculation will easily demonstrate, and as we verify below:

10! (x^0)

The Log function.

To calculate the natural logarithm of a number, do this:

Log[2.17]

Log[E]

Log[1]

D[Log[x], x]

To calculate logarithms in other bases, use the syntax: Log[base , x ].

Log[10, 100]

Immediate versus delayed assignment.

We saw last time that functions are defined with the following syntax:

f[x_] := (x^10)

Note the := symbol. It is technically referred to as the SetDelayed operator. This means that when the above cell is entered, the right hand side is memorized by Mathematica, but it is not evaluated yet. It is only evaluated when the function is called, like this:

f[3]

You have also seen the = symbol, technically refered to as the Set operator. When it is used,  Mathematica evaluates the right hand side of the equation immediately. For ordinary functions like f above, either := or = will work to define the function.

Clear[f];
f[x_] = (x^10);
f[3]

But there are instances when one is appropriate and the other is not. For instance, suppose you wanted to define h(x) to be the derivative of the function f. You would have to use = rather than :=, as follows:

h[x_] = D[f[x], x]

h[1]

What happens if you try :=? Very bad things....

Clear[h];
h[x_] := D[f[x], x]

h[1]

The problem is that calling h[1] causes Mathematica to try and calculate the expression

D[f[1], 1]

which makes no sense (we cannot differentiate with respect to the variable 1). Using the immediate assigment operator = causes the derivative to be calculated on the spot, so that subsequent calls to h can be executed properly.

Tables.

The Table command works just like the Plot command, but it produces a table of values instead of a picture.

Plot[f[x], {x, 1, 5}]

Table[f[x], {x, 1, 5}]

The argument x is incremented by units from 1 to 5, and the function is evaluated at each value. The output will look more like a table if you wrap it with the TableForm command.

TableForm[Table[f[x], {x, 1, 5}]]

An easier way to "wrap" the outermost command is with the following special syntax:

Table[f[x], {x, 1, 5}] // TableForm

This works for any command that accepts a single argument:

(1999/2002) // N

Lastly, you can have several values on each row of your table, if desired. Note that the "dummy" variable can be given any name you like. Here we call it n.

Table[{n, f[n]}, {n, 1, 5}] // TableForm

Exercise 1
Make a table of the first five derivatives of the function f(x) = (Sqrt[1 + x]).

Replacement Rules.

A convenient way to evaluate a symbolic expression in the variable x at a particular numerical value of x is via Replacement Rules. A simple example illustrates the idea:

2  x /. x -> 1

The structure /.x->1 reads "take the expression and replace all occurences of x by 1". It is really useful when the expression is rather large. Behold:

f[x_] := x^10;
Table[D[f[x], {x, n}], {n, 1, 5}] // TableForm

Table[D[f[x], {x, n}], {n, 1, 5}] /. x -> 1 // TableForm

We have evaluated f' (1),  
      f'' (1),   ...  ,  (f^((5)))(1) in one go! This will prove useful for finding Taylor series.

Exercise 2
Make a table of the first five derivatives of the function f(x) = (Sqrt[1 + x]) evaluated at x = 0.

Exact versus approximate numbers.

We saw last time that Mathematica makes a distinction between exact and approximate numbers. For instance, enter these cells and note the results:

π

N[π]

N[π, 30]

When we use approximation techniques such as Taylor series, or the Newton-Raphson method, we will seek decimal (ie approximate) output, to many decimal places. The last input cell above demonstrates how to ask for the decimal form of an exact number to a specified number of significant digits. The key is that the first argument to the N command must be an exact number. To work with 1.25 with 30 significant digits, for instance, enter it as

N[(125/100), 30]

The Sum function.

Sometimes you like to evaluate a series of numbers which have a certain pattern. For example the numbers 1 up to 100. In Mathematica there is a very quick way to do this using the command Sum.

Sum[k, {k, 1, 100}]

Here is the sum of the numbers 1/k for k=1 to 100, both precisely and numerically

Sum[1/k, {k, 1, 100}]
Sum[1/k, {k, 1, 100}] // N

To see how the sums of the first n inverted grows as n increases we can also make a table of a number of these sums.

Table[Sum[1/k, {k, 1, n}] // N, {n, 1, 100, 10}] // 
      TableForm

What is your opinion, will this list of sums go to a limit eventually, or not?

Taylor Approximations.

Recall Taylor's theorem for the series for the function f about the point x = a:

f(
        a + h) = f(a) + ((f' (a))/(1!)) 
              h + ((f'' (a))/(2!)) 
              (h^2) +  ...  + (((f^((n)))(a))/(n!)) (h^n) + 
        O((h^(n + 1)))

Frequently one is asked to use the theorem in the case when a = 0. In this special case the theorem takes the simpler form (here we use x instead of h):

f(
        x) = f(0) + ((f' (0))/(1!)) 
              x + ((f'' (0))/(2!)) 
              (x^2) +  ...  + (((f^((n)))(0))/(n!)) (x^n) + 
        O((x^(n + 1)))

Exercise 3
Consider the function f(x) = (Sqrt[1 + x]). Use the results of exercise 5 and the theorem above to find, by hand,
(a) the first order Taylor polynomial approximation to f at a = 0.
(b) the second order Taylor polynomial approximation to f at a = 0.
(c) the third order Taylor polynomial approximation to f at a = 0.
(d) the fourth order Taylor polynomial approximation to f at a = 0.
(e) the fifth order Taylor polynomial approximation to f at a = 0.

Of course, Mathematica can be harnessed to find Taylor polynomials in a much slicker way than the method used above. One creates the Taylor series for a function with the Series or Taylor commands. You must specify the function as the first argument, followed by an "iterator" of the form {x, a, n}, where x is the name of the variable, a is the point about which the approximation is made, and n is the highest degree to appear in the resulting polynomial. Here is the 9th order Taylor series for the inverse sine function, for points near x = 0.

Series[ArcSin[x], {x, 0, 9}]

The command Taylor will give the same output, but without the "big oh" term. Note that Taylor is not part of standard Mathematica but is defined in the first command of this Notebook. It should be used when you wish to make a subsequent plot or a numerical calculation using the Taylor series.

Taylor[ArcSin[x], {x, 0, 9}]

One could re-create a demonstration like the one given in class as follows:

ser = Taylor[ArcSin[x], {x, 0, 9}];
Plot[{ArcSin[x], ser}, {x, -1, 1}]

Exercise 4
For the function f(x) = (Sqrt[1 + x]), plot f on the domain -1 ≤ x ≤ 2 together with
(a) the first order Taylor polynomial approximation to f at a = 0.
(b) the second order Taylor polynomial approximation to f at a = 0.
(c) the third order Taylor polynomial approximation to f at a = 0.
(d) the fourth order Taylor polynomial approximation to f at a = 0.
(e) the fifth order Taylor polynomial approximation to f at a = 0.

Exercise 5
On the basis of the visual evidence accumulated in the previous exercise, make a conjecture regarding the "region of convergence" of the Taylor series for the function f(x) = (Sqrt[1 + x]) at a = 0. That is, for which domain does the Taylor series well-approximate the function f?

One can use replacement rules and tables to gain insight into the improvement in accuracy attained from higher degree Taylor polynomials. Here is a table of the first ten Taylor polynomials for the inverse sine function:

Table[Taylor[ArcSin[x], {x, 0, n}], {n, 1, 10}] // TableForm

Exercise 6
(a) Consider the first 10 Taylor polynomials to f(x) = (Sqrt[1 + x]) at a = 0. Make a table of each of these evaluated at the point x = 0.25. Report the values to 30 significant digits.
(b) Use the information in this table (and nothing else) to give an approximation to (Sqrt[1.25]). How many significant digits do you think you can report?
(c) Use Mathematica to find (Sqrt[1.25]) to 30 significant digits. Does your answer to part (b) appear valid? Actually, we are cheating a little bit.
Mathematica computes  (Sqrt[1.25]) using similar methods as the one we try to demonstrate here. 
(d) There is a better way to check the accuracy of your approximation.
Simply square the approximation you found and compare with 1.25.

Taylor Series

Suppose we have a function f(x) which we want to evaluate at some point (Subscript[x, 0]). Let us approximate f((Subscript[x, 0]))by a Taylor series of f(x) around the point x=0, say. Suppose also that the error term of the n-th Taylor approximation, evaluated at (Subscript[x, 0])goes to zero as we let n increase. Then we say that f((Subscript[x, 0])) equals the infinite sum

f(
          (Subscript[x, 0])) = f(0) + ((f' (0))/(1!)) 
                  (Subscript[x, 0]) + ((f'' (0))/(2!)) 
                  ((Subscript[x, 0])^2) +  ...  + (((f^((n)))(0))/(n!)) 
              ((Subscript[x, 0])^n) +  ... 

This infinite sum is called the Taylor series of f(x) around x=0.

To see how fast these Taylor Series converge we perform a little experiment. The Taylor series of (e^x) is given by 

1 + x + ((x^2)/(2!))+((x^3)/(3!))+((x^4)/(4!))+((x^5)/(5!))+....

We use this series with x=1 to approximate the number e.

Exercise 7.
Make a table of the sum of the first n terms of the Taylor series for (e^x), where we let n go from 1 to 10. Compare these sums with the actual value of e. (Note: n factorial can be written as input by n!)

Here is another experiment. The Taylor series of -log(1-x) is given by 

 x+((x^2)/(2 ))+((x^3)/(3 ))+((x^4)/(4 ))+((x^5)/(5 ))+ .... 

We use this series with x=-1 to approximate the number log(2). We expect that

log(2)=1-(1/2)+(1/3)-(1/4)+(1/5)- ....

Exercise 8.
Compute the first 100, 1000, 10000 terms of the summation and compare this with the actual value of log(2).

You probably noticed that convergence is much slower than the convergence of our series to e. There is a way to get a faster converging series to log(2) however. Simply take x=1/2 in our Taylor series for -log(1-x). We get 

log(2)=(1/2)+(((((1/2)))^2)/(2 ))+(((((1/2)))^3)/(3 ))+(((((1/2)))^4)/(4 ))+(((((1/2)))^5)/(5 ))+ ....

Exercise 9.
Compute the first 10, 50, 100 terms of the summation and compare this with the actual value of log(2).

Here is another exercise which gives you a famous (but very slowly converging) series for π.

Exercise 10.
What is the Taylor series for Arctan(x) around x=0? Using this series in the point x=1 you can deduce a nice series expansion for π . Do this.