Functions often can be approximated by infinite series. As more terms are added in the sequence, the approximation becomes better (usually). The exponential function ex can be approximated by the series
The notation n! represents the factorial of number n, n!=1*2*3...*n, 0!=1. The MATLAB function factorial performs this computation.
We will use the MATLAB function exp to calculate the "true" value of ex for a given value of x. The difference between the true and approximated values is the approximation error. The tolerance is the amount of error that we are willing to accept. Usually, we are willing to accept this error due to practical limitations on computing time or memory. The smaller the tolerance we choose, the more accurate the approximation becomes.
Part (a): Write a program (
eee.m) that uses the series shown above to approximate e0.5. The program should start by approximating e0.5 with just the first term of the series and add the additional terms one by one until a tolerance of 0.001 is satisfied. Use a loop! The program should show one line of output for each additional term used. This line of output should display the number of terms used, the approximated value of e0.5, and the approximation error. Below is an example of what the first few lines of output may look like:
No. of Terms Approximation Error 1 1.000000 0.648721 2 1.500000 0.148721
The above is an example of the output format, not the actual solution. Your output should have the same components but does not need to have exactly the same format.
Part (b):Now that you have a program to approximate ex, let's experiment with the tolerance! Use six values of tolerance: 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001. Do not submit M-files for Part (b). Instead, type a table (in plain text format) that shows how many terms of the series are needed for each tolerance value. Put this table at end of the program from Part (a) as a comment block.
loop.m) Use a for loop without using vectorized code. In this program, each iteration of the loop calculates one value of y for one value of x.
vectorized.m) Write vectorized code. No loops! Draw a plot of the function values for . The code should label the axes and give the plot a title. Read Section 10 in MATLAB Essentials (4/29 handout, p.4), and/or type
help plotin the command window, to learn about plotting. The code given in the 4/29 handout shows an example of using MATLAB function
Submit your files
vectorized.m on-line using CMS (Course Management System) before the project deadline. Make sure you are submitting the correct, up to date files. We will not accept any files after the deadline for any reason (except for documented medical reasons). See the CMS link on the web page for instructions on using CMS.