15.11.2014 Views

Chapter 4: Programming in Matlab - College of the Redwoods

Chapter 4: Programming in Matlab - College of the Redwoods

Chapter 4: Programming in Matlab - College of the Redwoods

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Section 4.3 Functions <strong>in</strong> <strong>Matlab</strong> 351<br />

23. The first few rows <strong>of</strong> Pascal’s Triangle<br />

look like <strong>the</strong> follow<strong>in</strong>g.<br />

1<br />

1 1<br />

1 2 1<br />

1 3 3 1<br />

1 4 6 4 1<br />

The nth row holds <strong>the</strong> b<strong>in</strong>omial<br />

coefficients<br />

( n<br />

k)<br />

, k = 0, 1, . . . , n.<br />

Write a function to produce Pascal’s<br />

Triangle for an arbitrary number r <strong>of</strong><br />

rows. Your first l<strong>in</strong>e should read as<br />

follows.<br />

function pascalTriangle(r)<br />

This function should use nested for<br />

loops to produce <strong>the</strong> rows <strong>of</strong> Pascal’s<br />

Triangle. The function should also use<br />

<strong>the</strong> fpr<strong>in</strong>tf command to pr<strong>in</strong>t each<br />

row to <strong>the</strong> screen. Use cell mode to<br />

test your function by enter<strong>in</strong>g <strong>the</strong> follow<strong>in</strong>g<br />

l<strong>in</strong>e <strong>in</strong> your cell enabled editor.<br />

pascalTriangle(8)<br />

Your function should employ <strong>the</strong> b<strong>in</strong>om<br />

function written <strong>in</strong> Exercise 20. H<strong>in</strong>t:<br />

The format to use for <strong>the</strong> fpr<strong>in</strong>tf command<br />

is <strong>the</strong> tricky part <strong>of</strong> this exercise.<br />

Before enter<strong>in</strong>g <strong>the</strong> <strong>in</strong>ner loop to<br />

compute <strong>the</strong> coefficients for a particular<br />

row, set format=’’ (<strong>the</strong> empty<br />

str<strong>in</strong>g), <strong>the</strong>n build <strong>the</strong> str<strong>in</strong>g <strong>in</strong> <strong>the</strong> <strong>in</strong>ner<br />

loop with format=[format,’%6d’].<br />

Once <strong>the</strong> <strong>in</strong>ner loop completes, you’ll<br />

need to add a l<strong>in</strong>e return character before<br />

execut<strong>in</strong>g fpr<strong>in</strong>tf(format,row).<br />

24. Rewrite <strong>the</strong> quadratic function<br />

m-file <strong>of</strong> Example 8 by replac<strong>in</strong>g <strong>the</strong><br />

if..elseif..else conditional with <strong>Matlab</strong>’s<br />

switch..case..o<strong>the</strong>rwise conditional.<br />

25. Rewrite <strong>the</strong> quadratic function<br />

m-file <strong>of</strong> Example 8 to produce two<br />

complex solutions <strong>of</strong> <strong>the</strong> quadratic equation<br />

ax 2 +bx+c = 0 <strong>in</strong> <strong>the</strong> case where<br />

<strong>the</strong> discrim<strong>in</strong>ant D = b 2 − 4ac < 0.<br />

26. Write a function called mysort<br />

that will take a column vector <strong>of</strong> numbers<br />

as <strong>in</strong>put, sort <strong>the</strong>m <strong>in</strong> ascend<strong>in</strong>g<br />

order, <strong>the</strong>n return <strong>the</strong> sorted list as a<br />

column vector. Test <strong>the</strong> speed <strong>of</strong> your<br />

rout<strong>in</strong>e with <strong>the</strong> follow<strong>in</strong>g commands.<br />

<strong>in</strong>=rand(10000,1);<br />

>> tic, mysort(<strong>in</strong>); toc<br />

Compare <strong>the</strong> speed <strong>of</strong> your sort<strong>in</strong>g<br />

rout<strong>in</strong>e with <strong>Matlab</strong>’s.<br />

>> tic, sort(<strong>in</strong>); toc<br />

Note: This is a classic problem<br />

<strong>in</strong> computer science, a ritual <strong>of</strong> passage,<br />

like cross<strong>in</strong>g <strong>the</strong> equator for <strong>the</strong><br />

first time. There are very sophisticated<br />

algorithms for attack<strong>in</strong>g this problem,<br />

but try to see what you can do<br />

with it on your own <strong>in</strong>stead <strong>of</strong> search<strong>in</strong>g<br />

for a sort rout<strong>in</strong>e on <strong>the</strong> <strong>in</strong>ternet.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!