This example shows how to use a m-file to perform some complex number operations in matlab. The m-file can be created in any text editor (e.g., emacs or notepad).
MATLAB M-File example3.m:
% Filename: example3.m
%
% Description: M-file for demonstration of complex number operations.
%
clear; % clear matlab memory
z = -2 - j*3; % define complex number z=-2-j3
abs(z) % compute magnitude of complex number
angle(z) % compute angle (in radians) of complex number
angle(z)*180/pi % compute angle (in degrees) of complex number
[anglez, magz] = cart2pol(real(z), imag(z)) % recompute magnitude and phase
% of complex number
y = 3*exp(-j*pi/4) % define complex number y=3e^(-jpi/4)
[realy, imagy] = pol2cart(-pi/4, 3) % determine real and imaginary parts of a
% complex number 3e^(-jpi/4)
y*z % multiply complex numbers
Before running the m-file, make sure matlab looks in the
directory where the m-file is stored (C:\Temp in my case) by
using the change directory (cd) command:
>> cd C:\TempRun example3.m by typing example3 at the prompt:
>>example3Results shown in the matlab command window:
ans =
3.6056
ans =
-2.1588
ans =
-123.6901
anglez =
-2.1588
magz =
3.6056
y =
2.1213 - 2.1213i
realy =
2.1213
imagy =
-2.1213
ans =
-10.6066 - 2.1213i