Lab 2

This lab will be mostly focused constructing more and more complicated functions to handle tasks that need to be repeated.

Contents

Evaluating the exponential function

Consider the Taylor series expansion for $e^x$ about $x = 1$:

$$e^x = \sum_{n=0}^\infty \frac{x^n}{n!}. $$

The absolute error for partial sums is given by

$$\left| e^x -  \sum_{n=1}^N \frac{x^n}{n!} \right| \leq \frac{e^{|x|} |x|^{N+1}}{N!}. $$

Exercise 1

Write a function exp_frac(x) to compute the exponential function to a relative accuracy of 10e-15 for $0 \leq x \leq 1$.

Create a plot of the absolute and relative error as $x$ increases. Use the semilogy() command to help with this visualzation.

Exercise 2

The exponential function has the important property that $e^{x/2} = (e^x)^2$. Construct a function exp_series(x) to compute the exponential function for any number $x$ using exp_frac(y) where $0 < y < 1$ and $x = y 2^m$ for some $m$.

Create a plot of the absolute and relative error as $x$ increases. Use the semilogy() command to help with this visualzation.

Exercise 3

Write a function bisection(f,a,b,TOL,Nmax) to find the root of a function f.

Exercise 4

Write a function log_bisect(x) to compute the natural logarithm of $x \geq 1$ using your exp_series() and bisection() functions. What challenges do you encounter?