23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

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.

2 Data Types<br />

Complex Numbers<br />

Complex numbers consist of two separate parts: a real part and an imaginary<br />

part. The basic imaginary unit is equal to the square root of -1. Thisis<br />

represented in <strong>MATLAB</strong> by either of two letters: i or j.<br />

Creating Complex Numbers<br />

The following statement shows one way of creating a complex value in<br />

<strong>MATLAB</strong>. The variable x is assigned a complex number with a real part of 2<br />

and an imaginary partof3:<br />

x = 2 + 3i;<br />

Another way to create a complex number is using the complex function. This<br />

function combines two numeric inputs into a complex output, making the first<br />

input real and the second imaginary:<br />

x = rand(3) * 5;<br />

y = rand(3) * -8;<br />

z = complex(x, y)<br />

z =<br />

4.7842 -1.0921i 0.8648 -1.5931i 1.2616 -2.2753i<br />

2.6130 -0.0941i 4.8987 -2.3898i 4.3787 -3.7538i<br />

4.4007 -7.1512i 1.3572 -5.2915i 3.6865 -0.5182i<br />

You can separate a complex number into its real and imaginary parts using<br />

the real and imag functions:<br />

zr = real(z)<br />

zr =<br />

4.7842 0.8648 1.2616<br />

2.6130 4.8987 4.3787<br />

4.4007 1.3572 3.6865<br />

zi = imag(z)<br />

zi =<br />

-1.0921 -1.5931 -2.2753<br />

-0.0941 -2.3898 -3.7538<br />

-7.1512 -5.2915 -0.5182<br />

2-24

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

Saved successfully!

Ooh no, something went wrong!