Sunteți pe pagina 1din 8

MH2401

Algorithms & Computing III Lab 2: Solutions

AY 2013/14 S1

Tasks. 1. (a) Download the le lab01_qn3.m from the course website. Open it with MATLAB . (b) Publish the le by clicking on File > Publish lab01_qn3.m You should see a new window with the le and all outputs published in HTML format. (c) Add the following lines to the top of the le, and publish it again.
%% Lab 1, Question 3 % This is a question on the order of precedence of relational % operators. %%

You should see a title and some descriptive text before the function begins. (d) Change the line
% Part (a)

to
%% Part (a)

and publish the le again. You should see a section titled Part (a) with the comments following it in normal text. You should also see a list of contents. (e) Change the line
% We now try various values of a in the command 0 <= a <= 10.

to
% We now try various values of |a| in the command |0 <= a <= 10|.

and publish the le again. You should see the MATLAB variable a and command 0 <= a <= 10 in MATLAB font. (f) Replace the empty line immediately below the above line that you just changed with the following line, and publish the le again.
%

You should see the comments that follows immediately in normal text. (g) Add the following line immediately below the rst for loop, and publish the le again.
%%

You should see the comments immediately following the for loop in normal text, with the outputs of the loop appearing before these comments. (h) Update the rest of the le so that (i) every part begins as a new section listed in the list of contents; (ii) all comments are either section titles or in normal text;
1

(iii) all MATLAB variables and commands in normal text are in MATLAB font; and (iv) all outputs of each for loop appear before the comments immediately below it. Solution. (h) The updated le is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

%% Lab 1, Question 3 % This is a question on the order of precedence of relational % operators. %% funct ion l a b 0 1 _ q n 3 %% Part (a) % We now try various values of |a| in the command |0 <= a <= 10|. % % Since the command involves comparisons with the numbers 0 and % 10, we choose values of |a| that covers the range [0, 10]. f o r a = -5:15 b = 0 <= a <= 10; disp ([ The output for a = , num2str ( a ) , ... is , num2str ( b ) , . ]); end %% % We see that the command always output 1. % % This is because the first comparison gives either a 0 or 1, % which are both less than 10, and thus the second comparison % always give a 1. %% Part (b) % We now try various values of |a| in the command |a * (a > 0)|. % % Since the command involves comparison with the number 0, we % choose values of |a| that covers both negative and positive % numbers. f o r a = -5:5 b = a * ( a > 0); disp ([ The output for a = , num2str ( a ) , ... is , num2str ( b ) , . ]); end %% % We see that the command outputs 0 when |a| is negative or 0, % and outputs |a| itself when it is positive. % % This is because the comparison gives 1 when |a| is positive, so % that the product is |a|; and the comparison gives 0 otherwise, % so that the product is still 0. %% Part (c) % We now try various values of |a| in the command |(a > 5) (a < % 5)|.

3
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

% % Since the command involves comparison with the number 5, we % choose values of |a| that covers numbers that are less than 5 % and those greater than 5. f o r a = 0:10 b = ( a > 5) - ( a < 5); disp ([ The output for a = , num2str ( a ) , ... is , num2str ( b ) , . ]); end %% % We see that the command outputs 1 when |a| is less than 5, 0 % when |a| equals 5, and 1 when |a| is greater than 5. % % This is because when |a| > 5, the two comparisons result in 1 % 0; when |a| = 5, the two comparisons result in 0 0; and when % |a| < 5, the two comparisons result in 0 1. end

2. (a) Create a script by issuing the command edit test78.m (b) Type the following lines into the script le test78.m.
v = 1:100; v78 = []; for k = v i f ( mod (k ,7) == 0 || mod (k ,8) == 0) && mod (k ,56) ~= 0 v78 = [ v78 k ]; end end disp ([ The integers d i v i s i b l e by 7 or 8 but not both : , ... num2str ( v78 )]); disp ([ The number of such integers = , ... num2str ( length ( v78 ))]);

(c) Run the script le by issuing the command test78. (d) Write a MATLAB script that (i) prints an array of all integers between 1 and 100 that are divisible by 6 or by 9, but not both; and (ii) prints how many such integers are there which are divisible by 6 or 9, but not both. Solution. (d) The MATLAB script is as follows.
1 2 3 4 5 6 7

v = 1:100; v69 = []; for k = v i f ( mod (k ,6) == 0 || mod (k ,9) == 0) && mod (k ,18) ~= 0 v69 = [ v69 k ]; end end

4
8 9 10 11

disp ([ The integers d i v i s i b l e by 6 or 9 but not both : num2str ( v69 )]); disp ([ The number of such integers = , ... num2str ( length ( v69 ))]);

, ...

3. (a) Create a function by issuing the command edit fibonacci.m (b) Type the following lines into the function le bonacci.m.
funct ion out = f i b o n a c c i( n ) i f n == 0 % base case out = 0; e l s e i f n == 1 % base case out = 1; else out = f i b o n a c c i(n -1) + f i b o n a c c i(n -2); % recursive formula end end

(c) Run the function le by issuing the command fibonacci(n) with various nonnegative integer values of n. What value does the function return? (d) Write a MATLAB function called sequence that (i) takes a nonnegative integer value n as the input; and (ii) returns the value of the nth number in the sequence dened by a0 = 0, a1 = 1, a2 = 1, an = 2an1 + 2an2 an3 for n = 3, 4, . . . . (e) Compare the outputs of fibonacci(n) and sequence(n) for various choices of n. How is the sequence {an } related to the Fibonacci sequence? Solution. (c) The function returns the nth Fibonacci number fn dened by f0 = 0,
1 2 3 4 5 6 7 8 9 10 11 12

f1 = 1,

fn = fn1 + fn2 for n = 3, 4, . . . .

(d) The function is as follows


funct ion out = sequence ( n ) i f n == 0 % base case out = 0; e l s e i f n == 1 % base case out = 1; e l s e i f n == 2 % base case out = 1; else out = 2* sequence (n -1) + 2* sequence (n -2) ... - sequence (n -3); % recursive formula end end

(e) We compare the outputs with the following commands.

>> f o r n = 0:10 disp ([ n f i b o n a c c i( n ) sequence ( n )]) , end ; 0 0 0 1 2 3 4 5 6 7 8 9 10 1 1 2 3 5 8 13 21 1 1 4 9 25 64 169 441 34 55 1156 3025

We see from the output that an is the square of the nth Fibonacci number. 4. (a) Create a function by issuing the command edit sum_even.m (b) Type the following lines into the function le sum_even.m.
funct ion out = sum_even ( n ) v = 1: n ; v = 2* v ; out = sum ( v .^2); end

(c) Run the function le by issuing the command sum_even(n) with various positive integer values of n. What value does the function return? (d) Without using any loop or recursion, write a MATLAB function called partial_sum.m that (i) takes a positive integer value n as the input; and (ii) returns an array of values of the rst nth partial sums of the series 1 3 n=1 n

[Hint: explore the built-in cumsum function.] (e) Use the function partial_sum(n) to plot the nth partial sum against n from 1 to 100.

Do the partial sums appear to converge? If so, to what value (in 2 signicant digits)? Solution. (c) The function returns the sum of the squares of the rst nth positive even numbers. (d) The function is as follows
1 2 3 4 5

funct ion out = p a r t i a l _ s u m( n ) v = 1: n ; v = v .^3; out = cumsum (1./ v ); end

(e) The following command generates the plot.


>> p l o t (1:100 , p a r t i a l _ s u m (100))
1.25

1.2

1.15

1.1

1.05

20

40

60

80

100

The partial sums appear to converge to the value 1.2.

Extra Questions. 1. (a) Use a for loop, instead of recursion, to write a function that takes a positive integer value n as input, and returns the nth Fibonacci number. (b) Compare this function with the function fibonacci(n). Which function is faster? [Hint: use tic and toc to measure performance.] Solution. (a) The function is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

funct ion out = f i b o n a c c i _ l o o p ( n ) i f n == 0 % base case out = 0; e l s e i f n == 1 % base case out = 1; else f n m i n u s t w o = 0; f n m i n u s o n e = 1; f o r i = 2: n fn = f n m i n u s o n e + f n m i n u s t w o; % recursive formula f n m i n u s t w o = f n m i n u s o n e; f n m i n u s o n e = fn ; end out = fn ; end end

(b) We compare the two functions as follows.


>> t i c , f i b o n a c c i (24) , toc ans = 46368 Elapsed time is 1.229068 seconds . >> t i c , f i b o n a c c i _ l o o p (24) , toc ans = 46368 Elapsed time is 0.000430 seconds .

From the outputs, it is apparent that the loop implementation is much faster than the recursive implementation. 2. Without using any loop or recursion, write a function that takes as inputs a vector v of integers and an integer a, and returns the number of times a appears in v. [Hint: use a logical comparison to turn v into a vector of 0s and 1s.]

Solution.
1 2 3 4

funct ion out = find_a (v , a ) l = ( v == a ); out = sum ( l ); end

3. Write a function that takes a positive integer value n as input, and plots n uniformly spaced points on the unit circle. [Hints: 1) the points are uniformly spaced if the angle between any consecutive points is a constant. What is this constant? 2) what are the Cartesian coordinates of a point with polar coordinates (1, )?] Solution.
1 2 3 4 5

funct ion c i r c u l a r _ p o i n t s ( n ) twopi = 2* pi ; points = twopi / n : twopi / n : twopi ; p l o t ( cos ( points ) , s i n ( points ) , . ); end

S-ar putea să vă placă și