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.

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

>> M(~B)<br />

ans =<br />

4<br />

10<br />

24<br />

6<br />

12<br />

18<br />

1<br />

25<br />

8<br />

14<br />

20<br />

21<br />

15<br />

16<br />

22<br />

9<br />

Even more succ<strong>in</strong>ctly, try <strong>the</strong> command M(~isprime(M)) to achieve <strong>the</strong> same<br />

result.<br />

Let’s look at ano<strong>the</strong>r example.<br />

◮ Example 4. Sketch <strong>the</strong> surface z = x 2 + y 2 on <strong>the</strong> rectangular doma<strong>in</strong><br />

d = {(x, y) : −1 ≤ x, y ≤ 1}, but only for those pairs (x, y) that satisfy y < x<br />

and y > −x.<br />

Let’s aga<strong>in</strong> use an anonymous function, mak<strong>in</strong>g it “array smart.”<br />

>> f=@(x,y) x.^2+y.^2<br />

f =<br />

@(x,y) x.^2+y.^2<br />

Test <strong>the</strong> function. Note that f(3, 4) = 3 2 + 4 2 = 25.<br />

>> f(3,4)<br />

ans =<br />

25

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

Saved successfully!

Ooh no, something went wrong!