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.

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

requires that x 2 − 1 ≥ 0. Solv<strong>in</strong>g for x, |x| ≥ 1, or equivalently, x ≤ −1 or<br />

x ≥ 1. For values <strong>of</strong> x between −1 and 1, <strong>the</strong> expression x 2 − 1 is negative, so<br />

<strong>the</strong> square root produces complex numbers. We can see <strong>the</strong> complex numbers <strong>in</strong><br />

y with logical <strong>in</strong>dex<strong>in</strong>g.<br />

>> y(k)<br />

ans =<br />

Columns 1 through 3<br />

0 + 0.2814i 0 + 0.5127i 0 + 0.6527i<br />

Columns 4 through 6<br />

0 + 0.7543i 0 + 0.8315i 0 + 0.8907i<br />

Columns 7 through 9<br />

0 + 0.9354i 0 + 0.9676i 0 + 0.9885i<br />

Columns 10 through 12<br />

0 + 0.9987i 0 + 0.9987i 0 + 0.9885i<br />

Columns 13 through 15<br />

0 + 0.9676i 0 + 0.9354i 0 + 0.8907i<br />

Columns 16 through 18<br />

0 + 0.8315i 0 + 0.7543i 0 + 0.6527i<br />

Columns 19 through 20<br />

0 + 0.5127i 0 + 0.2814i<br />

Note that <strong>the</strong> real part <strong>of</strong> each <strong>of</strong> <strong>the</strong>se complex numbers is zero.<br />

So, how does <strong>Matlab</strong>’s plot command handle complex numbers. The answer<br />

is found by typ<strong>in</strong>g help plot. The relevant part <strong>of</strong> <strong>the</strong> helpfile follows.<br />

PLOT(Y) plots <strong>the</strong> columns <strong>of</strong> Y versus <strong>the</strong>ir <strong>in</strong>dex.<br />

If Y is complex, PLOT(Y) is equivalent to<br />

PLOT(real(Y),imag(Y)). In all o<strong>the</strong>r uses <strong>of</strong> PLOT,<br />

<strong>the</strong> imag<strong>in</strong>ary part is ignored.<br />

In <strong>the</strong> case at hand, we used plot(x,y), so this paragraph <strong>of</strong> <strong>the</strong> help file for <strong>the</strong><br />

plot command would <strong>in</strong>dicate that <strong>Matlab</strong> simply ignores <strong>the</strong> imag<strong>in</strong>ary parts<br />

<strong>of</strong> <strong>the</strong> vector y when <strong>the</strong> entries are complex. For <strong>the</strong> output <strong>of</strong> y(k) above,<br />

when <strong>Matlab</strong> ignores <strong>the</strong> imag<strong>in</strong>ary part, that is equivalent to say<strong>in</strong>g that <strong>Matlab</strong><br />

considers all <strong>of</strong> <strong>the</strong>se complex numbers to equal <strong>the</strong>ir real part, or zero. This<br />

can be seen by add<strong>in</strong>g <strong>the</strong> command axis([-5,5,-5,5]) to produce <strong>the</strong> image <strong>in</strong><br />

Figure 4.2.

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

Saved successfully!

Ooh no, something went wrong!