matlab complex 1

Upload: ds-harris

Post on 14-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Matlab Complex 1

    1/2

    Introduction to Matlab & Complex Numbers

    Peter Norgaard

    February 2nd, 2011

    Like many programming languages, Matlab assigns values to variablesusing the standard evaluate right-hand side, then assign to left-hand side

    format. For example, type the following, hitting return after each line,

    x = 1 % x is assigned the value 1

    y = 4*x + 1 % the RHS is evalued as 5, which is assigned to y

    Each line is executed independently, and is independent of previous lines, soif we change x, the value of y remains unchanged.

    x = 1 % y is still equal to 5

    By default, Matlab sets i =1. However, this can be (accidentally)

    overwritten by the user with a simple statement like

    i = 2

    In such cases, you can either assign it the imaginary number value again, orclear the variable assignment, in which case it reverts to its original default.

    i = sqrt(-1) % the imaginary number is assigned to i

    clear i % i goes back to its default value

    You can write any complex number using the sum of the real an imaginaryparts,

    z = 2 + 5i % z = 2 + 5*i also works

    To separate out the real and imaginary parts, use the real() and imag()functions,

    x = real(z) % returns 2

    y = imag(z) % returns 5

    1

  • 7/29/2019 Matlab Complex 1

    2/2

    Matlab has an incredible number of available functions, including a large set

    that operates on complex numbers. Weve already seen real() and imag().Here are some others that can be very useful:

    abs(z) % gives the modulus of the complex number

    angle(z) % gives the phase angle of z in polar coordinates,

    Note that angle() returns the answer in the range (, ]. So we can obtainthe polar coordinates (r, ) of a complex number, and then use them toreconstruct z.

    r = abs(z)

    theta = angle(z) % in the range (-pi, pi]

    z = r*exp(i*theta) % returns 2.0000 + 5.0000i

    To get the complex conjugate of a complex number, use the function conj()

    z 1 = 2 + 5 i

    z2 = conj(z1) % returns 2.0000 - 5.0000i

    Standard operations like addition (+), subtraction(-), multiplication(*) anddivision (/) can all be performed on complex numbers:

    z1+z2 % returns 4

    z1-z2 % returns 10i

    z1*z2 % returns 29

    z1/z2 % returns -0.7241 - 0.6897i

    To find the nth root of a number, we do something special. The functionnthroot() is only intended for real roots. Instead, we cast the nth root asan algebraic equation. For example, to find the quartic roots of 1, take thefourth power of both sides ofz =

    41 to get z4 = 1. Now use the symbolic

    math toolbox function solve() to obtain the solution for z,

    z_a = solve(z^4 = 1,z)

    which returns the four solutions, {1, 1 i, i}. Note that these are not inorder of increasing phase angle. In some cases the symbolic solution will be

    very long... use the function double() to recast it as a floating point number.

    Although you cant just use Matlab to do your homework (showing yourwork is definitely required), you can use it to quickly check your results.Good luck!

    2