Assignment 10

Assignment 10 - University of California Berkeley Fall...

This preview shows pages 1–4. Sign up to view the full content.

University of California, Berkeley Department of Mechanical Engineering Fall Semester 2008 Instructors: M. Frenklach, R. Horowitz E7, Assignment 10 Assigned: Thursday, November 6, 2008 Due: 12:00 pm (noon), Wednesday, November 19 , 2008 This assignment is an introduction to induction and recursive algorithms. As before, turn in the hard copy of your published file to the drop boxes in Etcheverry 1109 and upload the soft copies of your script and your functions (the M-files) to bspace. Do not forget to name your main M-file as lastname_firstname_SID_lab10.m NOTE: Do not forget to display the contents of your user-defined functions using the command type. MATLAB commands * introduced in this assignment: floor, rem 1. Prove the following using induction. Show that for any natural number n , 2 333 3 1 123 2 n(n+ ) ++++ n= ⎛⎞ ⎜⎟ ⎝⎠ Solve this problem by hand on a separate sheet of paper and attach it to your hard copy submission. 2. a) Suppose a and b are positive integers with a>b . In order to implement a recursive procedure to find the Greatest Common Divisor (GCD) of a and b , consider the following facts: If d is a divisor of a and b , then there exist integers u and v such that a = u*d and b = v*d . In such a case, the difference between a and b given by a-b = (u-v)*d is also divisible by d . So gcd(b,a-b) = gcd(a,b). Thus, by successively taking difference between a and b , where the roles of a and b are changed as above, a point is reached when a==b . This is the base case, where * Please refer to MATLAB help to learn how to use the functions introduced in this assignment. Assignment 10 E7 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
University of California, Berkeley Department of Mechanical Engineering Fall Semester 2008 Instructors: M. Frenklach, R. Horowitz gcd(a,b) = a(or b) . Using the hints above, write a recursive function gcd_rec that takes two positive integers a and b as inputs, and returns the gcd of a and b . Note: a>b is not necessary; hence modify your code appropriately using MATLAB functions min and abs . b) Write another function gcd_loop that works similar to gcd_rec but instead uses a while loop. Get the output for the following commands: >> gcd_rec(29,3) >> gcd_loop(29,3) >> gcd_rec(483,51) >> gcd_loop(483,51) >> gcd_rec(138346,11947) >> gcd_loop(138346,11947) c) In the following set of commands the times of execution for computing GCD using gcd_rec and gcd_loop is compared. Note that the number of loops/recursions using the above procedure for determining GCD of two numbers of the form (x,k*x) , where x and k are positive integers, is k. Hence the order of problem is increased with increased values of k. n = 100:50:450; m = 1; for j=n tic; gcd_rec(3*j,3); t_rec(m) = toc; m = m+1; end Assignment 10 E7 2
University of California, Berkeley Department of Mechanical Engineering Fall Semester 2008 Instructors: M. Frenklach, R. Horowitz m = 1; for j=n tic; gcd_loop(3*j,3); t_loop(m) = toc; m = m+1; end Use polyfit to fit curves of linear order, corresponding to the gcd_rec and gcd_loop execution times and plot these execution times vs n. Comment on the results.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 10

Assignment 10 - University of California Berkeley Fall...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online