%Demonstrates the effect of an operator on a 2-dimensional vector
clear all;
%The following code gets user input for length and angle of a vector

options.WindowStyle='normal'; %Allows user to manipulate windows while ...
                                %waiting for user input
options.Interpreter='tex'; %Allows use of LaTeX notation in titles
prompt={'Length of Vector','Angle (deg) of Vector '}; %Prompts user 
title1='Vector Length and Angle'; %Title of user-input dialog box
dims=[1,70]; %Dimensions of user-input dialog box
definput={'4','45'}; %Default input values
params=inputdlg(prompt,title1,dims,definput,options);
    vecorig_length=str2num(char(params(1)));
    vecorig_angle=(pi/180)*str2num(char(params(2))); %Gets user input
    
% Sets the start and end points of the vector
vecorig_start = [0 0]; % Start point of original vector (before operator)
vecorigx=vecorig_length*cos(vecorig_angle); %x-value of orig. vector endpoint
vecorigy=vecorig_length*sin(vecorig_angle); %y-value of orig. vector endpoint
vecorig_end = [vecorigx vecorigy]; % Location of orig. vector endpoint
vecorig = vecorig_end-vecorig_start; % Makes original vector

% The following code draws the vector using the "quiver" command.
% The quiver command draws vectors as arrows, so quiver(x0,y0,xcomp,ycomp)
% draws an arrow starting at x0,y0 with horizontal component xcomp and
% vertical component ycomp
f1=figure(1); % Opens figure window
quiver(vecorig_start(1),vecorig_start(2),vecorig(1),vecorig(2),'AutoScale','off',...
    'MaxHeadSize',0.5,'Color','k'); % Set vector head size and color
hold on; % Allows multiple vectors on same plot
grid on; % Makes horizontal and vertical grid lines

%Setting the plot limits
plotlim=ceil(1.5*vecorig_length); % Makes sure vector fits on plot
axis([-plotlim  plotlim -plotlim  plotlim]); % Sets x and y axis limits
daspect([1 1 1]); % Makes sure plot remains square (so perpendicular lines
                    % appear with 90-deg angle to user)

% Labels vector, prints length on plot, and prints initial graph title
H1=text(vecorig_end(1),vecorig_end(2), '   V_{orig}'); %Labels orig. vector
H2=text(0.2*plotlim,0.9*plotlim,['V_{orig} length=',num2str(vecorig_length)]);
H3=text(0.2*plotlim,0.8*plotlim,['V_{orig} angle=',...
    num2str((180/pi)*vecorig_angle),' deg']); %Writes length & angle on plot
title(['Vector Before Operator Applied']);
pause(2); % Gives the user a few seconds to look at the two vectors

prompt={'M_{11}','M_{12}','M_{21}','M_{22}'}; %Promps user for matrix elements
title1='Operator Matrix Elements'; %Title of dialog box
dims=[1,70]; %Dimensions of dialog box
definput={'2','-1','1','3'}; %Default input values for matrix elements
params=inputdlg(prompt,title1,dims,definput,options); %Gets user inputs
    M11=str2num(char(params(1))); %User input value for matrix element (1,1)
    M12=str2num(char(params(2))); %User input value for matrix element (1,2)
    M21=str2num(char(params(3))); %User input value for matrix element (2,1)
    M22=str2num(char(params(4))); %User input value for matrix element (2,2)
opmat=[M11 M12;M21 M22]; %Makes matrix of four elements

vecmod=opmat*vecorig'; %Multiplies operator matrix by original vector to
                        %make a modified vector
vecmod_length=norm(vecmod); %Finds length of modified vector
vecmod_angle=atan2(vecmod(2),vecmod(1)); %Finds angle of modified vector
vecmod_start = [0 0]; %Sets start point of modified vector
vecmodx=vecmod_length*cos(vecmod_angle); %x-value of mod. vector endpoint
vecmody=vecmod_length*sin(vecmod_angle); %y-value of mod. vector endpoint
vecmod_end = [vecmodx vecmody]; %Location of modified vector endpoint

if vecorig_angle<0; %Converts negative angles of orig. vector to range of 0 to 2pi
        vecorig_angle=vecorig_angle+2*pi;
    end;
if vecmod_angle<0; %Convers negative angles of mod. vector to range of 0 to 2pi
        vecmod_angle=vecmod_angle+2*pi;
    end;    

biggervec=max([vecorig_length,vecmod_length]); %Find bigger of two vectors
plotlim=ceil(2*biggervec); %Sets plot limit to make sure vectors fit
axis([-plotlim  plotlim -plotlim  plotlim]); %Sets axis limits
daspect([1 1 1]); % Makes sure plot remains square (so perpendicular lines
                    % appear with 90-deg angle to user)
delete(H1); %Removes annotation of orig. vector to make room for
delete(H2); %annotation of modified vector
delete(H3);

%The following commands plot the modified vector as an arrow and
%annotate the name, length, and angle of both vectors
quiver(vecmod_start(1),vecmod_start(2),vecmod(1),vecmod(2),'AutoScale','off',...
    'MaxHeadSize',0.5*vecorig_length/biggervec,'Color','k');

H1=text(vecorig_end(1),vecorig_end(2), 'V_{orig}');
H4=text(vecmod_end(1),vecmod_end(2), 'V_{mod}');
H2=text(-0.8*plotlim,0.9*plotlim,['V_{orig} length=',num2str(vecorig_length)]);
H3=text(-0.8*plotlim,0.8*plotlim,['V_{orig} angle=',...
    num2str((180/pi)*vecorig_angle),' deg']);
H5=text(0.2*plotlim,0.9*plotlim,['V_{mod} length=',num2str(vecmod_length,3)]);
H6=text(0.2*plotlim,0.8*plotlim,['V_{mod} angle=',...
    num2str((180/pi)*vecmod_angle,3),' deg']);

length_change=vecmod_length-vecorig_length; %Finds change in length
angle_change=vecmod_angle-vecorig_angle; %Finds change in angle
if angle_change<=1e-15; %Sets threshold for angle change to equal zero
    angle_change=0;
end;

%The following commands explain the effect of applying the operator matrix
%to the original vector
H7=text(-0.8*plotlim,-0.7*plotlim,['For this vector, applying this operator changes']);
H8=text(-0.8*plotlim,-0.8*plotlim,['length by ',num2str(length_change,3),...
    ' units and angle by ',num2str((180/pi)*angle_change,3),' deg']);
if angle_change==0; %Tells user this is an eigenvector of this operator
    H9=text(-0.8*plotlim,-0.9*plotlim,'This is an eigenvector of this operator');
end;
title(['Vectors Before and After Operator Applied']);

%The following commands write the operator matrix on the plot
text(-0.95*plotlim,0.5*plotlim,'Operator Matrix');
text(-0.8*plotlim,0.3*plotlim,num2str(opmat));
