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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

324 <strong>Chapter</strong> 4 <strong>Programm<strong>in</strong>g</strong> <strong>in</strong> <strong>Matlab</strong><br />

All even <strong>in</strong>tegers greater than<br />

2 can be expressed as a sum <strong>of</strong><br />

two prime <strong>in</strong>tegers.<br />

Write a program that expresses 1202<br />

as <strong>the</strong> sum <strong>of</strong> two primes <strong>in</strong> ten different<br />

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

output.<br />

9. Here is a simple idea for generat<strong>in</strong>g<br />

a list <strong>of</strong> prime <strong>in</strong>tegers. Create<br />

a vector primes with a s<strong>in</strong>gle entry,<br />

<strong>the</strong> prime <strong>in</strong>teger 2. Write a program<br />

to test each <strong>in</strong>teger less than 100 to<br />

see if it is a prime us<strong>in</strong>g <strong>the</strong> follow<strong>in</strong>g<br />

procedure.<br />

i. If an <strong>in</strong>teger is divisible by any <strong>of</strong><br />

<strong>the</strong> <strong>in</strong>tegers <strong>in</strong> primes, skip it and<br />

go to <strong>the</strong> next <strong>in</strong>teger.<br />

ii. If an <strong>in</strong>teger is not divisible by any<br />

<strong>of</strong> <strong>the</strong> <strong>in</strong>tegers <strong>in</strong> primes, append<br />

<strong>the</strong> <strong>in</strong>teger to <strong>the</strong> vector primes<br />

and go to <strong>the</strong> next <strong>in</strong>teger.<br />

Use fpr<strong>in</strong>tf to output <strong>the</strong> vector primes<br />

<strong>in</strong> five columns, right justified.<br />

10. There is a famous stoy about Sir<br />

Thomas Hardy and Sr<strong>in</strong>ivasa Ramanujan,<br />

which Hardy relates <strong>in</strong> his famous<br />

work “A Ma<strong>the</strong>matician’s Apology,”<br />

a copy <strong>of</strong> which resides <strong>in</strong> <strong>the</strong> CR library<br />

along with <strong>the</strong> work “The Man<br />

Who Knew Inf<strong>in</strong>ity: A Life <strong>of</strong> <strong>the</strong> Genius<br />

Ramanujan.”<br />

I remember once go<strong>in</strong>g to see<br />

him when he was ill at Putney.<br />

I had ridden <strong>in</strong> taxi cab number<br />

1729 and remarked that <strong>the</strong><br />

number seemed to me ra<strong>the</strong>r a<br />

dull one, and that I hoped it<br />

was not an unfavorable omen.<br />

“No,” he replied, “it is a very<br />

<strong>in</strong>terest<strong>in</strong>g number; it is <strong>the</strong> smallest<br />

number expressible as <strong>the</strong><br />

sum <strong>of</strong> two cubes <strong>in</strong> two different<br />

ways.”<br />

Write a program with nested loops to<br />

f<strong>in</strong>d <strong>in</strong>tegers a and b (<strong>in</strong> two ways) so<br />

that a 3 + b 3 = 1729. Use fpr<strong>in</strong>tf to<br />

format <strong>the</strong> output <strong>of</strong> your program.<br />

11. Write a program to perform each<br />

<strong>of</strong> <strong>the</strong> follow<strong>in</strong>g tasks.<br />

i. Use <strong>Matlab</strong> to draw a circle <strong>of</strong> radius<br />

1 centered at <strong>the</strong> orig<strong>in</strong> and<br />

<strong>in</strong>scribed <strong>in</strong> a square hav<strong>in</strong>g vertices<br />

(1, 1), (−1, 1), (−1, −1), and<br />

(1, −1). The ratio <strong>of</strong> <strong>the</strong> area <strong>of</strong><br />

<strong>the</strong> circle to <strong>the</strong> area <strong>of</strong> <strong>the</strong> square<br />

is π : 4 or π/4. Hence, if we were<br />

to throw darts at <strong>the</strong> square <strong>in</strong> a<br />

rondom fashion, <strong>the</strong> ratio <strong>of</strong> darts<br />

<strong>in</strong>side <strong>the</strong> circle to <strong>the</strong> number <strong>of</strong><br />

darts thrown should be approximately<br />

equal to π/4.<br />

ii. Write a for loop that will plot 1000<br />

randomly generated po<strong>in</strong>ts <strong>in</strong>side<br />

<strong>the</strong> square. Use <strong>Matlab</strong>’s rand<br />

command for this task. Each time<br />

random po<strong>in</strong>t lands with<strong>in</strong> <strong>the</strong> unit<br />

circle, <strong>in</strong>crement a counter hits.<br />

When <strong>the</strong> for loop term<strong>in</strong>ates, use<br />

fpr<strong>in</strong>tf to output <strong>the</strong> ratio darts<br />

that land <strong>in</strong>side <strong>the</strong> circle to <strong>the</strong><br />

number <strong>of</strong> darts thrown. Calculate<br />

<strong>the</strong> relative error <strong>in</strong> approximat<strong>in</strong>g<br />

π/4 with this ratio.<br />

12. Write a program to perform each<br />

<strong>of</strong> <strong>the</strong> follow<strong>in</strong>g tasks.<br />

i. Prompt <strong>the</strong> user to enter a 3 × 4<br />

matrix. Store <strong>the</strong> result <strong>in</strong> <strong>the</strong> matrix<br />

A.

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

Saved successfully!

Ooh no, something went wrong!