MATLAB: Help! the variable entry is not displaying as entered.

basicerrorhelpscript

%loop that will have 2000 iterations (each iteration
%representing a rotation of the plane by 0.5 degrees)
for t= 1:2000
Output=zeros(1,2000);
format('long')
%imagine points in 2-dimensional euclidean space. These three vectors
%represent the three vertices of an equilateral triangle cetered at the
%origin
A = [-0.5; 0.5];
B = [-0.5; -0.5];
C = [0.5; -0.5];
D = [0.5; 0.5];
%T is designed to be a 0.5 degree counter-clockwise rotation matrix, sending each of the
%previous vectors to their 0.5 degree rotated counterparts
T= [ 0.99996 -0.00873;0.00873 0.99996];
%represent the rotated images of A,B,C, for some iteration 't'
tA=(T^t)*A;
tB=(T^t)*B;
tC=(T^t)*C;
tD=(T^t)*D;
%tE=(T^t)*E
%tF=(T^t)*F
%tG=(T^t)*G
%tH=(T^t)*H
%tI=(T^t)*I
%P extracts the x values of each vector above, discarding the rest
P= [1 0];
%represent the projection of tA,tB,tC onto the x-axis.
imA=P*tA;
imB=P*tB;
imC=P*tC;
imD=P*tD;
%imE=P*tE;
%imF=P*tF;
%imG=P*tG;
%imH=P*tH;
%imI=P*tI;
%imJ=P*tJ;
%distances between the respective projections
distAB=abs(imA-imB);
distBC=abs(imB-imC);
distCD=abs(imC-imD);
distDA=abs(imD-imA);
distAC=abs(imC-imA);
distBD=abs(imB-imD);
%making a vector V with elements 'distAB' 'distBC' 'distCA'
X1=[1 0 0 0 0 0];
X2=[0 1 0 0 0 0];
X3=[0 0 1 0 0 0];
X4=[0 0 0 1 0 0];
X5=[0 0 0 0 1 0];
X6=[0 0 0 0 0 1];
dX1=distAB.*X1;
dX2=distBC.*X2;
dX3=distCD.*X3;
dX4=distDA.*X4;
dX5=distAC.*X5;
dX6=distBD.*X6;
V= dX1+dX2+dX3+dX4+dX5+dX6;
% D = max element in V is the width of the 'shadow' of the triangle on the
%x-axis
D=max(V);
Output(t)=D;
end
When I run the script, my vector 'D', which, as far as I can tell is very clearly defined as a 2×1 vector, is assigned the value '1.1597'.. seemingly without my consent or permission!
Can anyone figure out where this anomalous value is coming from?

Best Answer

First to answer your question, in the line before last of your script D is very clearly defined as the max of V, overwriting whatever was stored previously in it. Morale: Use variable names that have more than one letter and more importantly have meaning, such as shadowwidth or some such and it'll be a lot more difficult to reuse them by mistake.
On another note, you certainly like to write the same code over and over. This would achieve the same in so much fewer lines:
vertices = [A, B, C, D, E, F, G, H, I]; %these variables should never have been created in the first place
rotation = [0.99996 -0.00873;0.00873 0.99996];
numrotations = 2000;
shadowwidth = zeros(1, numrotations);
for rotationstep = 1:numrotations
rotatedvertices = (rotation ^ rotationstep) * vertices; %all your tx = ... all at once!
xprojection = rotatedvertices(1, [1 4]); %hardly worth a matrix multiplication, but you could do it with P*rotatedvertices
distances = abs(xprojection - xprojection.'); %only works in R2016b and later
%in previous versions, replace with:
%distances = abs(bsxfun(@minus, xprojection, xprojection.');
distances = distances(triu(true(size(distances)), 1)); %only keep upper triangle of distance matrix
shadowwidth(t) = max(distances);
end