0% found this document useful (0 votes)
35 views4 pages

1 Infinite Series (30 Points) : HW1 p1.m

This document provides instructions for 5 problems in numerical methods homework. It includes instructions for writing MATLAB functions to compute series summations and represent floating point numbers, as well as hand calculations for Maclaurin series approximations and solving quadratic equations with rounding arithmetic. Students are asked to complete code with missing sections, run test scripts, analyze results, and provide calculations and explanations.

Uploaded by

aniket patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

1 Infinite Series (30 Points) : HW1 p1.m

This document provides instructions for 5 problems in numerical methods homework. It includes instructions for writing MATLAB functions to compute series summations and represent floating point numbers, as well as hand calculations for Maclaurin series approximations and solving quadratic equations with rounding arithmetic. Students are asked to complete code with missing sections, run test scripts, analyze results, and provide calculations and explanations.

Uploaded by

aniket patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EP501 Numerical Methods Homework 1 Due Sep 13, 2013 11:59 pm

1 Infinite Series (30 points)


The series
N
X 1
f (N ) = 2
(1)
n=1
n

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 ???.

1 %% EP501 HW1 problem 1


2 function [err fwd, err bkd, err appr, s1, s2] = HW1 p1(N)
3 % compare the forward and backward summation of a series
4
5 % INPUT:
6 % N − number of terms to sum
7 % OUTPUT:
8 % err fwd − true error with forward sum
9 % err bkd : true error with backward sum
10 % err appr : appxorimate error
11 % s1 : forward sum
12 % s2 : backward sum
13
14 % sum forward
15 s1=zeros(1,1,'single');
16 for i=???
17 s1 = s1 + ???;
18 end
19
20 % sum backward
21 s2=zeros(1,1,'single');
22 for i=???
23 s2 = s2 + ???;
24 end
25
26 % true value
27 s0=(piˆ2/6);
28
29 err fwd = ???; % error with forward sum
30 err bkd = ???; % errro worh backward sum
31 err appr = ???; % approximate error

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');

2) Which sum is more accurate, forward or reverse? Why?


3) Explain the results in the plot. Why the three errors vary differently with increasing N ?

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 Floating Point Number Set (30 points)


The following MATLAB function (file HW1 p2.m) calculates and lists all possible non-negative numbers that
can be represented by a floating-point variable, with some missing parts indicated by three question marks
???.

1 %% EP501 HW1 problem 2


2 function numlist = HW1 p2(E,M)
3 % Calculate all possible non−negative numbers that can be
4 % represented by a 7−bit floating point variable
5 %
6 % INPUT:
7 % E : number of bits for the exponent
8 % M : number of bits for the mantissa
9 %
10 % OUTPUT:
11 % numlist : a 1−d array that lists all possible non−negative numbers,
12 % sorted from smallest to largest values.
13 %
14
15 T=E+M+1; % number of total bits
16
17 b=1−2ˆ(E−1); % bias
18 emax=2ˆE−1; % maximum possible value of e
19
20 N=2ˆ(T−1); % total possible binary numbers to consider
21 numlist=zeros(N,1);
22
23 % loop through all possible numbers, excluding non−positive ones
24 for i=1:N
25

2
EP501 Numerical Methods Homework 1 Due Sep 13, 2013 11:59 pm

26 s = dec2bin(i−1); % binary string representing the number


27
28 % fill in 0's to make s a length−T string
29 for k=length(s)+1:T−1
30 s=['0' s];
31 end
32
33 % extract exponent part
34 e = bin2dec(s(1:E)); % exponent in decimal form
35
36 % extract mantissa part
37 f = bin2dec(s(E+1:T−1))/2ˆM;
38
39 if ( ??? ) % normal number
40 x = ???;
41 elseif (???) % subnormal number
42 x = ???;
43 else % infinity or NaN
44 if f==0
45 x = ???;
46 else
47 x = ???;
48 end
49 end
50 fprintf('#%02d 0 %s %s = %g\n',i,s(1:E),s(E+1:T−1),x);
51 numlist(i)=x;
52
53 end

1) Read and understand the program.


2) Replace the question marks ??? between lines 39 and 47 with appropriate code to make it work. Use the
following MATLAB script (file HW1 p2 test.m) to call the above function to test your work :

1 % plot all the numbers to see their distribution


2
3 E=2;
4 M=4;
5
6 numlist=HW1 p2(E,M);
7
8 figure(1);clf;
9 semilogy(numlist,'o'); % plot y−axis in logrithmic scale
10 title(sprintf('E=%d M=%d',E,M),'FontSize',18);
11 xlim([1 64]);
12 set(gca,'FontSize',14);
13
14 figure(2);clf;
15 plot(numlist,'o'); % plot y−axis in linear scale
16 title(sprintf('E=%d M=%d',E,M),'FontSize',18);
17 xlim([1 64]);
18 set(gca,'FontSize',14);

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

3 Maclaurin Series (15 points)


The first three nonzero terms of the Maclaurin series for the arctangent function are x − (1/3)x3 + (1/5)x5 .
Compute the absolute error and relative error in the following approximations of π using the polynomial in
place of the arctangent:
    
1 1
a. 4 arctan + arctan
2 3
   
1 1
b. 16 arctan − 4 arctan
5 239
Do not use MATLAB program to do the calculation. Calculate all terms using a calculator. Write down the
numbers obtained at every step.

4 Rounding Arithmetic) (15 points)


Use four-digit rounding arithmetic and the quadratic formulas (5), (13) and (15) in Lecture02.pdf to find
the most accurate approximations to the roots of the following quadratic equations. Computer the absolute
error and relative errors.
1 2 123 1
a. x − x+ =0
3 4 6
b. 1.002x2 + 11.01x + 0.01265 = 0

5 64-bit Floating-Point Numbers (10 points)


Find the decimal equivalent of the following 64-bit floating-point numbers:
1. 0 10000001010 1001001100000000000000000000000000000000000000000000
2. 0 01111111111 0101001100000000000000000000000000000000000000000001

You might also like