% DEMO:         PLOTTING COMPLEX NUMBERS ON THE COMPLEX PLANE
%
% Program:      complex_numbers_demo.m
% Written by:   Michael Buehner
% For:          EE 311
%               Demo For Computer Lab 1
% Date:         6-13-2001
% Purpose:      Program to take in real and imaginary parts
%               of a complex number and calculate its
%               magnitude and phase angle. The results are then 
%               printed on the complex plane
      
clear all, hold off, clf reset, close all

disp('Complex Number z=a+j*b')
a=input('Enter the real part of z      : ');
b=input('Enter the imaginary part of z : ');
disp(' ')

% Compute complex number
j=sqrt(-1);
z=a+j*b;

% Calculate the magnitude and phase of z
magz=sqrt(real(z)^2+imag(z)^2);
phz=atan2(imag(z), real(z));        % in radian
phasez=phz*180/pi;                  % in degrees

% Print results
s=['The magnitude of z is |z|= ' num2str(magz)];
disp(s)
s=['The phase of z is arg(z) = ' num2str(phasez) ' degrees'];
disp(s)

% graph results
j=sqrt(-1);
ang=0:phz/100:phz;
arc=(magz/3)*exp(j*ang);

hold on
maxz=ceil(magz);
axis([-maxz maxz -maxz maxz])
grid

plot(0,0,'ok')                      % label origin with a black o
plot(real(z),imag(z),'ob');
plot([0 real(z)],[0 imag(z)],'-r')
plot([-maxz maxz],[0 0],'-k')
plot([0 0],[-maxz maxz],'-k')
plot(real(arc),imag(arc),'m')

title('The complex number z plotted on the complex plane')
xlabel('real part'), ylabel('imag part')

figure
compass(z)                          % Plots an arrow from the origin pointing
hold on                             % to the complex number z on the complex plane
polar(angle(arc),abs(arc),'m')      % Illustrate phase angle
polar([0 0],[0 ceil(abs(z))],'k')   % Illustrate positive real axis
title('Polar plot of z on the complex plane')