matlab

profilealiaqi
csis1040_hw10_1.pdf

CSIS 1040 Exercises for Week 10

Write m-files (either functions or scripts) to do the following tasks. Save the m-files in your hw subdirectory. Name them as hw10 1.m through hw10 4.m Once you get your code to work, strive for good style by eliminating unnecessary steps and inserting comments.

1.) Create a function that will output 1 when run using Matlab, but will output 0 when run using Octave. (Hint: recall that Matlab and Octave are not identical. We have seen several instances where one has a built-in feature that the other lacks. Exploit one of these differences to create your function.)

2.) The Catalan numbers form an infinite sequence

1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, · · ·

with the (n + 1)st term given by the recursion Cn+1 = 2(2n+1)

n+2 Cn valid for all integers

n ≥ 0. Use this recursion to write a Matlab/Octave function that inputs an integer n ≥ 0 and outputs Cn. Call the function hw9 2a . There is also a direct definition for Cn on Wikipedia (top of the article). Write another function (hw9 2b )that generates Cn based on the direct definition (hint: you will probably need the built-in “nchoosek” of Matlab/Octave).

3.) The Ackermann function A(m, n) is described and recursively defined on the Wikipedia website. Write a Matlab/Octave function, called hw9 3 that computes A(m, n) WARNING: don’t run this function with inputs larger than 3 or 4 !!

4.) Using Matlab/Octave “tic.......toc” (or the “cputime” operator), compare/plot the run times for your two Catalan functions using various inputs n. Also find run times for your Ackermann function with various inputs of m, n. Do all this in a script called hw9 4.m. Use pauses between showing each plot. Include a summary of your findings in the comments of the script.

Extra.) If you are ambitious and need some extra points, write yet another function (called hw9 extra) to compute the nth Catalan number, based on the formula

Cn+1 = n!

i=0

CiCn−i .