Vous êtes sur la page 1sur 2

Introduction to Matlab & Complex Numbers

Peter Norgaard February 2nd , 2011

Like many programming languages, Matlab assigns values to variables using 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 y = 4*x + 1 % x is assigned the value 1 % the RHS is evalued as 5, which is assigned to y

Each line is executed independently, and is independent of previous lines, so if we change x, the value of y remains unchanged. 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, or clear the variable assignment, in which case it reverts to its original default. i = sqrt(-1) clear i % the imaginary number is assigned to i % i goes back to its default value x = 1 %

You can write any complex number using the sum of the real an imaginary parts, 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) y = imag(z) % returns 2 % returns 5 1

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) angle(z) % gives the modulus of the complex number % gives the phase angle of z in polar coordinates,

Note that angle() returns the answer in the range (, ]. So we can obtain the polar coordinates (r, ) of a complex number, and then use them to reconstruct z . r = abs(z) theta = angle(z) z = r*exp(i*theta)

% in the range (-pi, pi] % returns 2.0000 + 5.0000i

To get the complex conjugate of a complex number, use the function conj() z1 = 2 + 5i z2 = conj(z1)

% returns 2.0000 - 5.0000i

Standard operations like addition (+), subtraction(-), multiplication(*) and division (/) can all be performed on complex numbers: z1+z2 z1-z2 z1*z2 z1/z2 % % % % returns returns returns returns 4 10i 29 -0.7241 - 0.6897i

To nd the nth root of a number, we do something special. The function nthroot() is only intended for real roots. Instead, we cast the nth root as an algebraic equation. For example, quartic roots of 1, take the to nd the 4 fourth power of both sides of z = 41 to get z = 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 in order of increasing phase angle. In some cases the symbolic solution will be very long... use the function double() to recast it as a oating point number. Although you cant just use Matlab to do your homework (showing your work is denitely required), you can use it to quickly check your results. Good luck! 2

Vous aimerez peut-être aussi