1 Lab 1b: Iteration and error (due Sun Jan 31, 11:59 PM)
In this lab you will investigate the effect of rounding errors in computing some simple expressions. Take a close look at the submission requirements at the end of the lab script to ensure you know what is expected to be handed in. A helpful tip: If you keep a word-processor file open and paste in results, plots, and explanations as you go along it will save you a lot of time getting the report together. You will also investigate the effect of rounding errors in computing some simple expressions.
Working and submitting in Teams of 2: We will help find you a partner.
1.1 Taylor series and error
Recall that the truncated Taylor series of a function f(x) about a point x0 is given by:
Tn(x) = n f(k)(x0)(x − x0)k (1)
For f (x) = log x the task of evaluating (1) about x0 = 1 yields
Tn(x) =
k (x − 1)k (2)
k! k=0
n (−1)k−1
Problem 1
k=1
(a) Derive a Taylor series with n + 1 terms and the associated truncation error for the function f(x)= log(x)−x+1. (3)
(x−1)2
Your expression for the Taylor series should be general, similar to the series for log(x) (see Lab1A for examples of such computations). Note that this is actually much easier if you start with the Taylor series for log x and write out the first few terms explicitly starting with log(x) = (x − 1) + . . ., rather than starting from the definition above.
(b) Construct an expression that bounds the truncation error, assuming n > 2, for a given value of x. See especially the review material and examples related to Taylor’s series with remainder in Section 0.5 of the Text by Sauer.
Problem 2
(a) Modify an M-file for Taylor Series from Lab1A (e.g. for exp(x) without using Horner) to compute and plot the Taylor series and f(x) from problem 1 above (an M-file is expected to be handed in for this part with filename lab1bP2.m).
A couple of implementation hints that may be useful:
(i) Note that the loop does not have to start with 1.
(ii) Note that the first term in the series is now −1/2 rather than 0 so you will need to
1
change the initialization. A useful function to do this is the Matlab ones function which you can multiply by −0.5 to get the appropriate initialization.
(iii) When calculating f(x) with a vector argument x instead of a scalar, you need to use the vector operators for division ./ and powers .ˆ (i.e. with the . modifying the normal operator).
(b) Plot a graph of f(x) and its Taylor series with 4 terms (up to the cubic term) using 50 points over the range (0.999, 1.001). If you have done this correctly, the curves should be essentially indistinguishable.
Problem 3
(a) Change the range of your plot in problem 2b above to (1−10−7,1+10−7). The two curves should now look very different.
(b) Change the script so that it plots the absolute and relative errors (two different plots, one for absolute error, one for relative error)related to the difference between the Taylor series
and f (x) over the interval (1 − 10−6 , 1 + 10−6 ). Make sure you use the Matllab abs function to obtain the absolute value of the error. Use a log scale on the y−axis.
(c) To identify the source of the errors plotted in (b) it is useful to compare to a reasonable bound for the truncation error expected for the Taylor series. Come up with a such a bound and plot its magnitude on the same log-linear plot from (b). You should have a different bound for x < 1 and x > 1 (you can first look at the simpler case of log(x) to understand why this happens). For x > 1 you can use the result mentioned in class for error of an alternating series. The case x < 1 is much harder. You should consider what dominates f(x) as x → 0+.
(d) Consider the results from (c). From this you should be able to conclude that the problem is roundoff error rather than truncation error. Why? Explain what is making the roundoff errors so large and give an argument as to whether the Taylor series or the directly computed function is more accurate.
Problem 4 In this problem we want to create a matlab function that can evaluate f(x) to a given accuracy for any value of x.
(a) Create a new m-File, called myfofx.m. Unlike our previous script files, we would like this to be a function, just like log(x) or sin(x) that returns a number. i.e. we would like to be able to use the function in commands like:
or
To do this, we need to put the line
>> y=myfofx(1.001)
>> plot(x,myfofx(x))
function [fx]=myfofx(x)
2
as the first non-commented line in the m-file. Then, fx will be the variable that we will use in the m-file that, at the end of the calculation, contains our answer. Note that unlike previous cases where x and T were scalars you will not need to set the value of x as it will be passed to the function. In this case, just return the value of the n = 8 Taylor series (i.e. replace the fprintf statement with fx=T before the return). Test the function by evaluating the result on the command line for myfofx(1) and myfofx(2). Make sure you are getting the correct result before proceeding.
(b) If we are close to x = 1 our previous results suggest that it would be better to use the Taylor series. However, if we are far away from x = 1, the accuracy of our Taylor series starts to degrade due to truncation error. Use an if-else construct to modify your function to use the Taylor series when 1 − 10−6 < x < 1 + 10−6 and the explicit expression for f (x) outside of this range. If you are unsure of how to use the if-else construct described in the matlab tutorial, look it up in the matlab help. In particular, the examples given there often very helpful. Other matlab functions and statements that may, or may not, be useful is the absolute value function abs and the OR statement.
(c) Demonstrate that your function works by plotting its result over the same ranges of x used in problems 2 and 3.
Submission requirements
A section on submission requirements will be added later, together with format requirements for your lab.
3