%Two-dimensional vector plotter & graphical sum - This program draws two %vectors with lengths and angles specified by the user and then %demonstrates the process of graphically adding those two vectors. It does %this by displacing one of the vectors (without changing its length or %direction) so that its tail is at the head of the other vector. The sum %of the vectors is then drawn from the origin to the end of the displaced %vector. The user specifies which of the two vectors is displaced. clear all; %Clears old variables and data %The following code gets user input for length and angle of two vectors options.WindowStyle='normal'; %Allows user to manipulate windows while %waiting for user input prompt={'Length of First Vector','Angle (deg) of First Vector (V1)',... 'Length of Second Vector','Angle (deg) of Second Vector (V2)'}; %This specifies the text shown to the user in the dialog box requesting %inputs title1='Vector Lengths and Angles';%Title of user-interface dialog box dims=[1,70];%Sets dimensions (height and width) of user-input dialog box definput={'1','0','2','90'};%Default inputs for vector lengths and angles params=inputdlg(prompt,title1,dims,definput,options);%Generates dialog box vec1_length=str2num(char(params(1)));%Converts first user input from %character string to number and assigns variable for length %of vector 1 vec1_angle=(pi/180)*str2num(char(params(2)));%Converts second user %input from character string to number, then converts that %number from degrees to radians and assigns variable for %direction of vector 1 vec2_length=str2num(char(params(3)));%Converts third user input from %character string to number and assigns variable for length %of vector 2 vec2_angle=(pi/180)*str2num(char(params(4)));%Converts fourth user %input from character string to number, then converts that %number from degrees to radians and assigns variable for %direction of vector 2 % Finding the end points of the two vectors vec1start = [0 0];%Sets start point of vector 1 as origin vec1x=vec1_length*cos(vec1_angle);%Finds x-component of vector 1 vec1y=vec1_length*sin(vec1_angle);%Finds y-component of vector 2 vec1end = [vec1x vec1y];%Finds end point of vector 1 vec1 = vec1end-vec1start;%Generates 2-D vector 1 from start to end point vec2start = [0 0];%Sets start point of vector 2 as origin vec2x=vec2_length*cos(vec2_angle);%Finds x-component of vector 2 vec2y=vec2_length*sin(vec2_angle);%Finds y-component of vector 2 vec2end = [vec2x vec2y];%Finds end point of vector 2 vec2 = vec2end-vec2start;%Generates 2-D vector 2 from start to end point %Setting the start and end points of vec3 (the sum of vec1 and vec2) vec3start=[0,0];%Sets start point of vector 3 as origin vec3=vec1+vec2;%Adds vector1 to vector 2 vec3end=[vec1x+vec2x vec1y+vec2y];%Finds end point of vector 3 vec3_length=sqrt(vec3(1)^2+vec3(2)^2);%Finds length of vector 3 % The "quiver" command draws vectors - quiver(x0,y0,xcomp,ycomp) draws % an arrow starting at x0,y0 with horizontal component xcomp and % vertical component ycomp figure(1);%Initializes figure quiver(vec1start(1),vec1start(2),vec1(1),vec1(2),0,'k');%Plots vector 1 hold on;%Allows additional objects to be drawn on same plot quiver(vec2start(1),vec2start(2),vec2(1),vec2(2),0,'k');%Plots vector 2 grid on;%Draws grid lines on plot %Finding the longest vector to set the plot limits biggestvec=max([vec1_length,vec2_length,vec3_length]);%Finds biggest length plotlim=ceil(1.2*biggestvec);%Makes value 20 percent bigger than max length axis([-plotlim plotlim -plotlim plotlim]);%Sets plot axis limits %Labels for vector 1 and vector 2 and initial title for graph text(vec1end(1),vec1end(2), ' V1');%Labels end of vector 1 text(vec2end(1),vec2end(2), ' V2');%Labels end of vector 2 title(['Two Vectors to Add']);%Initial title of plot %Gives the user a few seconds to look at the two vectors pause(3);%Nothing happens for three seconds %Gets user input for which of the two vectors will be displaced prompt={'Displace Vector 1 or 2?'}; title1='Vector to Displace'; dims=[1,60]; definput={'1'}; params=inputdlg(prompt,title1,dims,definput,options); vectomove=str2num(char(params(1))); % Sets up movedvec to be displaced and statvec to remain stationary % and sets starting point of displaced vector to be the end of the % stationary vector if vectomove==1;%If user selects vector 1 to move, "vec to move" equals 1 vecnomove=2;%In this case, "vec no move" equals 2 statvec=vec2;%Sets stationary vector as vector 2 statvecstart=vec2start;%Sets start of stationary vector statvecend=vec2end;%Sets end of stationary vector movedvec=vec1;%Stores vector 1 as "moved vec" movedvecstart=vec1start;%Sets start of moved vector as start of vec1 movedvecend=vec1end;%Sets end of moved vector as end of vec1 vec4start=vec2end;%Sets start of displaced vector as end of vector 2 else vecnomove=1;%If user selects vector 2 to move, "vec no move" equals 1 statvec=vec1;%In this case, stationary vector is vec 1 statvecstart=vec1start;%Sets start of stationary vector statvecend=vec1end;%Sets end of stationary vector movedvec=vec2;%Stores vector 2 as "moved vec" movedvecstart=vec2start;%Sets start of moved vector as start of vec 2 movedvecend=vec2end;%Sets end of moved vector as end of vec 2 vec4start=vec1end;%Sets start of displaced vector as end of vector 1 end; %Moves displaced vector (as dashed arrow) from start to end % of stationary vector (while keeping the length and angle the same) % Each dashed arrow is deleted before a new one is drawn. title(['Displacing Vector V',num2str(vectomove)]);%Plot title during move for i=1:19;%Displacement will occur in 19 steps (final version of displaced %vector is drawn after this loop completes) movedvecstart=movedvecstart+(1/20)*statvec;%Moves start of vector which %is being displaced by one increment (1/20th of %distance to final displaced position) movedvecend=movedvecend+(1/20)*statvec;%Moves end of vector which is %being displaced by one increment H=quiver(movedvecstart(1),movedvecstart(2),movedvec(1),movedvec(2),0,... 'LineStyle','--','Color','k');%Draws vector being displaced (dashed) pause(0.1);%Short delay between positions for dashed moving vector delete(H);%Erases previous dashed moving vector end; %Draws displaced vector (not dashed) in final position vec4end=vec3end;%Sets end of displaced vector vec4=vec4end-vec4start;%Generates 2-D displaced vector as vector 4 quiver(vec4start(1),vec4start(2),vec4(1),vec4(2),0,'Color','k');%Draws %displaced vector pause(1)%Gives the user a second to see displaced vector in final position title(['Drawing Vector from Start of V',num2str(vecnomove),' to End of Displaced V'... num2str(vectomove)]);%Adds title during displacement % Draws an arrow of increasing length (in 19 steps) between start of % stationary vector and end of displaced vector for j=1:19;%Vector grows from origin in 19 steps vecgrow=(j/20)*vec3;%Sets length of growing vector H=quiver(statvecstart(1),statvecstart(2),vecgrow(1),vecgrow(2),0,'Color','k'); %Draws growing vector pause(0.1);%Short pause to show growing vector delete(H);%Clears previous version of growing vector end; %Draws thick arrow and labels it V1 + V2 quiver(vec3start(1),vec3start(2),vec3(1),vec3(2),0,'Color','k','Linewidth',2); text(vec3end(1),vec3end(2), ' V1+V2'); title(['Graphical Vector Addition with V',num2str(vectomove),' displaced']);