EE 212 Example 2: Complex Numbers Calculations and Plots in Matlab (M-file)


M-file saved as example1.m:
% m-file name:  example1.m
% description:  performs complex number calculations and plots
%               for class example

A = -1 + j*2;
fprintf('Re{A} = %f, Im{A} = %f, |A| = %f, /_A = %f deg \n', ...
    real(A), imag(A), abs(A), angle(A)*180/pi);

B = 2*exp(pi*j/3);
fprintf('Re{B} = %f, Im{B} = %f, |B| = %f, /_B = %f deg \n', ...
    real(B), imag(B), abs(B), angle(B)*180/pi);

A + B
B - A

C = A*B
D = B/A

plot(real(A),imag(A),'ro')
hold
plot(real(B),imag(B),'gx')
plot(real(C),imag(C),'b*')
plot(real(D),imag(D),'c+')
hold
gtext('A')
gtext('B')
gtext('C')
gtext('D')
xlabel('Re')
ylabel('Im')
title('Plot of Complex Numbers in Complex Plane');
After changing Matlab's Current Directory to where m-file is saved:
>> example1
Re{A} = -1.000000, Im{A} = 2.000000, |A| = 2.236068, /_A = 116.565051 deg 
Re{B} = 1.000000, Im{B} = 1.732051, |B| = 2.000000, /_B = 60.000000 deg 

ans =
   0.0000 + 3.7321i

ans =
   2.0000 - 0.2679i

C =
  -4.4641 + 0.2679i

D =
   0.4928 - 0.7464i

Current plot held
Current plot released

Figure/Plot Generated (Note had to click on figure four times for gtext):