Numerical Tools in Matlab

GUIDE: Mathematics of the Discrete Fourier Transform (DFT) - Julius O. Smith III. Numerical Tools in Matlab

NOTE: THIS DOCUMENT IS OBSOLETE, PLEASE CHECK THE NEW VERSION: "Mathematics of the Discrete Fourier Transform (DFT), with Audio Applications --- Second Edition", by Julius O. Smith III, W3K Publishing, 2007, ISBN 978-0-9745607-4-8. - Copyright © 2017-09-28 by Julius O. Smith III - Center for Computer Research in Music and Acoustics (CCRMA), Stanford University

<< Previous page  TOC  INDEX  Next page >>

Numerical Tools in Matlab

In Matlab, root-finding is always numerical:2.3

>> % polynomial = array of coefficients in Matlab
>> p = [1 0 0 0 5 7]; %  p(x) = x^5 + 5*x + 7
>> format long;       %  print double-precision
>> roots(p)           %  print out the roots of p(x)

ans = 1.30051917307206 + 1.10944723819596i 1.30051917307206 - 1.10944723819596i -0.75504792501755 + 1.27501061923774i -0.75504792501755 - 1.27501061923774i -1.09094249610903

Matlab has the following primitives for complex numbers:

>> help j

J Imaginary unit. The variables i and j both initially have the value sqrt(-1) for use in forming complex quantities. For example, the expressions 3+2i, 3+2i, 3+2i, 3+2j and 3+2*sqrt(-1). all have the same value. However, both i and j may be assigned other values, often in FOR loops and as subscripts.

    See also I.
    Built-in function.
    Copyright (c) 1984-92 by The MathWorks, Inc.

>> sqrt(-1)

ans = 0 + 1.0000i

>> help real

REAL Complex real part. REAL(X) is the real part of X.

    See also IMAG, CONJ, ANGLE, ABS.

>> help imag

IMAG Complex imaginary part. IMAG(X) is the imaginary part of X. See I or J to enter complex numbers.

    See also REAL, CONJ, ANGLE, ABS.

>> help conj

CONJ Complex conjugate. CONJ(X) is the complex conjugate of X.

>> help abs

ABS Absolute value and string to numeric conversion. ABS(X) is the absolute value of the elements of X. When X is complex, ABS(X) is the complex modulus (magnitude) of the elements of X.

    See also ANGLE, UNWRAP.

    ABS(S), where S is a MATLAB string variable, returns the
    numeric values of the ASCII characters in the string.
    It does not change the internal representation, only the
    way it prints.
    See also SETSTR.

>> help angle

ANGLE Phase angle. ANGLE(H) returns the phase angles, in radians, of a matrix with complex elements.

    See also ABS, UNWRAP.</pre></p><p>Note how helpful the ''See also'' information is in Matlab.</p><p>Let's run through a few elementary manipulations of complex

numbers in Matlab:

>> x = 1; % Every symbol must have a value in Matlab
>> y = 2;
>> z = x + j * y

z = 1.0000 + 2.0000i

>> 1/z

ans = 0.2000 - 0.4000i

>> z^2

ans = -3.0000 + 4.0000i

>> conj(z)

ans = 1.0000 - 2.0000i

>> z*conj(z)

ans = 5

>> abs(z)^2

ans = 5.0000

>> norm(z)^2

ans = 5.0000

>> angle(z)

ans = 1.1071

Now let’s do polar form:

>> r = abs(z)

r = 2.2361

>> theta = angle(z)

theta = 1.1071

>> r * exp(j * theta)

ans = 1.0000 + 2.0000i

>> z

z = 1.0000 + 2.0000i

>> z/abs(z)

ans = 0.4472 + 0.8944i

>> exp(j*theta)

ans = 0.4472 + 0.8944i

>> z/conj(z)

ans = -0.6000 + 0.8000i

>> exp(2jtheta)

ans = -0.6000 + 0.8000i

>> imag(log(z/abs(z)))

ans = 1.1071

>> theta

theta = 1.1071

>>

Some manipulations involving two complex numbers:

>> x1 = 1;
>> x2 = 2;
>> y1 = 3;
>> y2 = 4;
>> z1 = x1 + j * y1;
>> z2 = x2 + j * y2;
>> z1

z1 = 1.0000 + 3.0000i

>> z2

z2 = 2.0000 + 4.0000i

>> z1*z2

ans = -10.0000 +10.0000i

>> z1/z2

ans = 0.7000 + 0.1000i

Another thing to note about Matlab is that the transpose operator ' (for vectors and matrices) conjugates as well as transposes. Use .' to transpose without conjugation:

>>x = [1:4]*j

x = 0 + 1.0000i 0 + 2.0000i 0 + 3.0000i 0 + 4.0000i

>> x'

ans = 0 - 1.0000i 0 - 2.0000i 0 - 3.0000i 0 - 4.0000i

>> x.'

ans = 0 + 1.0000i 0 + 2.0000i 0 + 3.0000i 0 + 4.0000i

>>

<< Previous page  TOC  INDEX  Next page >>