% EXERCISE:     Perpendicular Complex Numbers
%
% Program:      perpendicular_cn.m
% Written by:   
% For:          EE 311
%               Exercise For Computer Lab 1
% Date:         5-31-2001
% Purpose:      You enter the real and imaginary parts of three
%               complex numbers. The program returns the magnitude
%               and angle of the numbers. It also gives the
%               cartesian coordinates of perp(z). After all three
%               numbers have been entered, the program plots these
%               numbers and their perpendiculars on the screen.
clear all, close all, clf reset
j=sqrt(-1);

for n=1:3
   ns=int2str(n);			% The number n converted to a string
   s=['Enter complex number z' ns ' (cartesian format)'];
   disp(s)
   s=['Use inputs from -5 to +5'];
   disp(s)
   disp(' ')
   s=['  Re[z' ns '] = '];
   x=input(s);              % Get real part of z
   s=['  Im[z' ns '] = '];
   y=input(s);              % Get imaginary part of z
   z(n)=x+j*y;
   z1(3*n-2:3*n)=[0 z(n) 0];
   disp('')
   s=['    |z' ns '| = ' num2str(abs(z(n)))];
   disp(s)                  % Display magnitude of z
   if imag(j*z(n))>=0
       perps=[num2str(real(j*z(n))) ' + j*' num2str(imag(j*z(n)))];
   else
       perps=[num2str(real(j*z(n))) ' - j*' num2str(-imag(j*z(n)))];
   end
   s=['perp(z' ns ') = ' perps];
   disp(s)                  % Display perpendicular complex number
   disp(' ')
end
disp('Press any key to continue')
pause

% Cartesian Plot
subplot(121)                % Plot in left half of screen
axis([-5 5 -5 5]);          % limit plot to range (-5,5)
axis('square')
hold on
title('Input Complex Numbers')
xlabel('Real Part'),ylabel('Imag Part')
plot(real(z),imag(z),'x',real(z1),imag(z1),'-')
grid

subplot(122)                % Plot in right half of screen
hold on
axis([-5 5 -5 5]);          % limit plot to range (-5,5)
axis('square')
title('Perpendicular Complex Numbers')
xlabel('Real Part'),ylabel('Imag Part')
plot(real(j*z),imag(j*z),'*',real(j*z1),imag(j*z1),':')
grid
figure(gcf)

% Compass plot
figure
subplot(121)                % Plot in left half of screen
axis([-5 5 -5 5]);          % limit plot to range (-5,5)
axis('square')
hold on
title('Input Complex Numbers')
xlabel('Real Part'),ylabel('Imag Part')
compass(z(1),'r')
compass(z(2),'b')
compass(z(3),'k')
grid

subplot(122)                % Plot in right half of screen
hold on
axis([-5 5 -5 5]);          % limit plot to range (-5,5)
axis('square')
title('Perpendicular Complex Numbers')
xlabel('Real Part'),ylabel('Imag Part')
compass(j*z(1),'r')
compass(j*z(2),'b')
compass(j*z(3),'k')
grid
figure(gcf)

%************************************************************************
% Exercises:                                                           *
% 1.) Notice in the second plot, the perpendicular complex numbers      *
%     are just the original complex numbers rotated 90 degrees          *
%     counter-clockwise on the complex plane. Figure out why this       *
%     happens and modify the program so that the perpendicular complex  *
%     number shown is rotated 90 degrees clockwise on the complex       *
%     plane.                                                            *
%                                                                       *
% 2.) Once you have your new program running, figure out which three    *
%     complex numbers where used to create the graph seen at the link   *
%     'Perpendicular Numbers Reversed' on the webpage where you         *
%     downloaded this file.                                             *
%************************************************************************