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.2 Control Structures <strong>in</strong> <strong>Matlab</strong> 323<br />

4.2 Exercises<br />

1. Write a for loop that will output<br />

<strong>the</strong> cubes <strong>of</strong> <strong>the</strong> first 10 positive<br />

<strong>in</strong>tegers. Use fpr<strong>in</strong>tf to output <strong>the</strong> results,<br />

which should <strong>in</strong>clude <strong>the</strong> <strong>in</strong>teger<br />

and its cube. Write a second program<br />

that uses a while loop to produce an<br />

<strong>in</strong>dentical result.<br />

2. Without appeal<strong>in</strong>g to <strong>the</strong> <strong>Matlab</strong><br />

command factorial, write a for loop<br />

to output <strong>the</strong> factorial <strong>of</strong> <strong>the</strong> numbers<br />

10 through 15. Use fpr<strong>in</strong>tf to format<br />

<strong>the</strong> output. Write a second program<br />

that uses a while loop to produce an<br />

<strong>in</strong>dentical result. H<strong>in</strong>t: Consider <strong>the</strong><br />

prod command.<br />

3. Write a s<strong>in</strong>gle program that will<br />

count <strong>the</strong> number <strong>of</strong> divisors <strong>of</strong> each <strong>of</strong><br />

<strong>the</strong> follow<strong>in</strong>g <strong>in</strong>tegers: 20, 36, 84, and<br />

96. Use fpr<strong>in</strong>tf to output each result<br />

<strong>in</strong> a form similar to “The number <strong>of</strong><br />

divisors <strong>of</strong> 12 is 6.”<br />

4. Set A=magic(5). Write a program<br />

that uses nested for loops to<br />

f<strong>in</strong>d <strong>the</strong> sum <strong>of</strong> <strong>the</strong> squares <strong>of</strong> all entries<br />

<strong>of</strong> matrix A. Use fpr<strong>in</strong>tf to format<br />

<strong>the</strong> output. Write a second program<br />

that uses array operations and<br />

<strong>Matlab</strong>’s sum function to obta<strong>in</strong> <strong>the</strong><br />

same result.<br />

5. Write a program that uses nested<br />

for loops to produce Pythagorean Triples,<br />

positive <strong>in</strong>tegers a, b and c that satisfy<br />

a 2 +b 2 = c 2 . F<strong>in</strong>d all such triples such<br />

that 1 ≤ a, b, c ≤ 20 and use fpr<strong>in</strong>tf<br />

to produce nicely formatted results.<br />

6. Ano<strong>the</strong>r result proved by Leonhard<br />

Euler shows that<br />

π 4<br />

90 = 1 + 1 2 4 + 1 3 4 + 1 4 4 + · · · .<br />

Write a program that uses a for loop<br />

to sum <strong>the</strong> first 20 terms <strong>of</strong> this series.<br />

Compute <strong>the</strong> relative error when this<br />

sum is used as an approximation <strong>of</strong><br />

π 4 /90. Write a second program that<br />

uses a while loop to determ<strong>in</strong>e <strong>the</strong><br />

number <strong>of</strong> terms required so that <strong>the</strong><br />

sum approximates π 4 /90 to four significant<br />

digits. In both programs, use<br />

fpr<strong>in</strong>tf to format your output.<br />

7. Some attribute <strong>the</strong> follow<strong>in</strong>g series<br />

to Leibniz.<br />

π<br />

4 = 1 − 1 3 + 1 5 − 1 7 + 1 9 − · · ·<br />

Write a program that uses a for loop<br />

to sum <strong>the</strong> first 20 terms <strong>of</strong> this series.<br />

Compute <strong>the</strong> relative error when<br />

this sum is used as an approximation<br />

<strong>of</strong> π/4. Write a second program that<br />

uses a while loop to determ<strong>in</strong>e <strong>the</strong><br />

number <strong>of</strong> terms required so that <strong>the</strong><br />

sum approximates π/4 to four significant<br />

digits. In both programs, use<br />

fpr<strong>in</strong>tf to format your output.<br />

8. Goldbach’s Conjecture is one <strong>of</strong><br />

<strong>the</strong> most famous unproved conjectures<br />

<strong>in</strong> all <strong>of</strong> ma<strong>the</strong>matics, which is remarkable<br />

<strong>in</strong> light <strong>of</strong> its simplistic statement:<br />

6<br />

Copyrighted material. See: http://msenux.redwoods.edu/IntAlgText/

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

Saved successfully!

Ooh no, something went wrong!