MATLAB Vector Analysis Program 
Purpose
After performing the force table lab and all the calculations and diagrams, this program is used by physics students to check their work. The program asks for the number of vectors and the magnitude and direction (absolute angle) of each vector. It then represents all the vectors from the origin and then in head to tail fashion. The resultant is shown in red on the diagram and the resultant's magnitude and direction are calculated. Several student groups were able to correct errors in their lab work through the use of this program.
% Program to add vectors clear; m = input('How many vectors ? '); mag(1) = 0; ang(1) = 0; for i=1:m; mag(i+1) = input('Enter magnitude of vector '); ang(i+1) = input('Enter angle of vector '); end n=length(mag); xcomp = mag .* cos(ang * pi/180); ycomp = mag .* sin(ang * pi/180); hold on; grid; for i = 1:n; xvect = [0 xcomp(i)]; yvect = [0 ycomp(i)]; plot(xvect,yvect); end; xplt(1) = 0; yplt(1) = 0; for i=2:n; xplt(i) = xplt(i1)+xcomp(i); yplt(i) = yplt(i1)+ycomp(i); end hold off; figure; plot(xplt,yplt,'y'); grid; xsum = sum(xcomp); ysum = sum(ycomp); xres = [0 xsum]; yres = [0 ysum]; hold on; plot(xres,yres,'r'); res = sqrt(xsum^2 + ysum^2); disp ('Resultant magnitude = '); disp (res); resang =180* (atan (ysum/xsum))/pi; if sign(xsum) == 1; ansang = 180 + resang; elseif sign(ysum)== 1 ; ansang = 360 + resang; else ansang = resang; end; disp ('Resultant angle = '); disp (ansang); hold off;
