# 2-D Truss Analysis

42 views (last 30 days)
Jillian Franke on 22 Oct 2020
Answered: KSSV on 22 Oct 2020
I keep getting this error message when trying to plot my code:
Index in position 2 exceeds array bounds (must not exceed 1).
Error in CEE210_CP3 (line 283)
p=plot(NodePoly(:,1),NodePoly(:,3));
Here is my original code
%.... Input problem data (radius, # of points, # of members, theta)
nNodesPoly = 6;
nMembersPoly = nNodesPoly - 1;
theta = pi/nMembersPoly; %a semi-circular arch
%% .. Use a for loop to compute the x, y and z coordinates of each point and
%.... create a matrix to store each point.
%.... You can use the variable name "nodePoly" for the nodal coordinates of
%.... the polygon.
for n= 1 : nNodesPoly
x(n,:)= [R*cos((n-1)*theta)]; %x as a function of R and theta
y(n,:)= [R*sin((n-1)*theta)]; %y as a function of R and theta
z(n,:)= [0]; %z as a function of R and theta
end
NodePoly=[x;y;z];
%.... Use a for loop to compute the members or connections and the unit
%.... vector for each member.
%.... You can use the variable name "UnVecPoly" for the unit vectors.
PosVecPoly= zeros(nMembersPoly,3); %initializing memory storage
UnitVecPoly= zeros(nMembersPoly,3);
OppPosVecPoly= zeros(nMembersPoly,3);
for n= 1 : nMembersPoly;
MemberPoly(n,:)=[n,n+1];
SN= MemberPoly(n,1);
EN= MemberPoly(n,2);
PosVecPoly(n,:)=NodePoly(EN,:)-NodePoly(SN,:);
Length(n,:)= norm(PosVecPoly(n,:));
UnVecPoly(n,:)= PosVecPoly(n,:)/Length(n,:);
end
%% .Create output for command window
fprintf('%s\n' , '---------------------------------------')
fprintf('%s\n' , '--------------Polygon------------------')
fprintf('%s\n' , '---------------------------------------')
for (i = 1 : nNodesPoly)
fprintf('%s%8i%8.3f%8.3f%8.3f\n',' Node: ', NodePoly(i,:)')
end
fprintf('\n')
for (i = 1 : nMembersPoly)
fprintf('%s%8i%8.3f%8.3f%8.3f\n',' Unit Vector: ',i, UnVecPoly(i,:)')
end
fprintf('\n\n\n')
%% ..Create the plot
fig3 = figure(3); clf; grid on; axis equal; hold on;
xlabel('X'); ylabel('Y'); title('Polygon Truss');
p=plot(NodePoly(:,2),NodePoly(:,3));
set(p,'Color','black','LineWidth',2);
scatter(NodePoly(:,2),NodePoly(:,3),'fill','green');
% End of Program

KSSV on 22 Oct 2020
Repalce this line:
NodePoly=[x ;y ;z];
with
NodePoly=[x y z];