1 Infinite Series (30 Points) : HW1 p1.m
1 Infinite Series (30 Points) : HW1 p1.m
converges to π 2 /6 as N approaches infinity. Complete the following MATLAB function (file HW1 p1.m) to
compute f (N ) for any arbitrary N by summing the series both forward and in reverse order, and calculate
the true fractional error for both results, as well as the approximate fractional error for the forward
sum (assuming that the backward sum is your best guess to the true solution). Fill in the missing parts
indicated by three question marks ???.
Note that the summing variables, s1 and s2 are defined to be single precision, i.e. 32-bit. This allows the
difference between the forward and backward summations to show. 64-bit precision is too accurate for the
two sums to deviate.
1) Try different values of N using this MATLAB function. Don’t try to go beyond 108 because the computing
time becomes very large. Use the provided script (file HW1 p1 test.m) below to test your function. The
script also plots the errors as a function of N .
1 %
2 % Test HW1 p1
3 %
4 clear all;
5
6 N=zeros(1,7);
7 err fwd=zeros(1,7);
1
EP501 Numerical Methods Homework 1 Due Sep 13, 2013 11:59 pm
8 err bkd=zeros(1,7);
9 err appr=zeros(1,7);
10 s1=zeros(1,7);
11 s2=zeros(1,7);
12
13 % calculate errors for 7 values of N
14 for i=1:7
15 N(i)=floor(10ˆi);
16 [err fwd(i), err bkd(i), err appr(i), s1(i), s2(i)] = HW1 p1(N(i));
17 fprintf(1,'err fwd=%e err bkd=%e err appr=%e s1=%e s2=%e\n', ...
18 err fwd(i), err bkd(i), err appr(i), s1(i), s2(i));
19 end
20
21 %%
22 figure(1);clf;
23 h=loglog(N,err fwd *100,'b−o',N,err bkd*100,'g',N,err appr*100,'r');
24 set(gca,'FontSize',18);
25 set(h,'LineWidth',2);
26 xlabel('N');
27 ylabel('Error (%)');
28 legend('err fwd','err bkd','err appr');
4) Compare the errors with the machine precision, which can be obtained with the MATLAB function
eps(’single’). Look up help of this function for more details.
2
EP501 Numerical Methods Homework 1 Due Sep 13, 2013 11:59 pm
3) Experiment with (a) E = 4 and M = 2, and (b) E = 2 and M = 4 for a 7-bit number (There is always
one bit for the sign). Make some intelligent comments about the trade-off between the range and the
precision of the two systems.
4) The script also plots the number distribution in both logarithmic and linear scales in y-axis. Describe
the distribution of the numbers based on the plots and explain why.
3
EP501 Numerical Methods Homework 1 Due Sep 13, 2013 11:59 pm