Parker/Gambrel Truss Algorithm- undefined function or variable 'm'

1 view (last 30 days)
I kept getting a 'preallocating for speed error' on line 73 for r(m).
I solved that issue by setting:
r(m) = zeros; %(before the for loop function)
NOW I'm getting an 'undefined function or variable 'm' error' on line 65.
NEED to make this code plot a Parker/Gambrel truss like in the picture:
what is wrong with my code?
clear, clf, clc
choice1=menu('Truss Program would like you to enter truss dimensions manually and define its dimensions?'...
,'Enter Dimensions Manually');
switch choice1
case 1
J=input('Please enter the number of joints:\n');
M=(2*J)-3; %Joint-member relationship for a simple, planar truss.
C=zeros(J,M); %Preallocates the connection matrix
x=zeros(J,1); %Preallocates the joint position vectors
y=zeros(J,1); %Prompts user for coordinates for joint positions through a loop
for j=1:J
promptx=sprintf('Please enter the x coordinate of joint %d in FEET:\n',j);
x(j)=input(promptx);
prompty=sprintf('Now enter the y coordinate of joint %d in FEET:\n',j);
y(j)=input(prompty);
end
%Builds the connection matrix from a loop of queries asking the
%user which members are connected to each joint.
cnct{1,J}=[]; %Each cell in 'cnct' contains the indices of the corresponding row in connection matrix 'C' that are to be assigned the value of 1.
for j=1:J
str=sprintf('How many members are connected to joint %d?\n',j);
choice2=menu(str,'1','2','3','4','5','6');
cnct{j}=zeros(choice2,1);
for i=1:choice2
rem=choice2-(i-1);
str2=sprintf('Which members? (%d members remaining for joint %d.)\n',rem,j);
cnct{j}(i)=input(str2);
end
C(j,cnct{j})=1;
end
end
%--------------------DEFINE SUPPORT AND LOAD MATRICES---------------------%
tstart=tic;
%Creates support force connection matrix
Sx=zeros(J,3);
Sy=zeros(J,3);
Sx(1,1)=1;
Sy(1,2)=1;
Sy(J,3)=1;
%Creates a load vector describing the location and strength of the
%load on the truss
L=zeros(2*J,1);
choice3=menu('Would you like to set a load at a joint?','Set a Load');
switch choice3
case 1
Lj=input('What number joint will the load be placed at?\n');
F=input('Please enter the value of the load in Newtons: \n');
L(J+Lj)=F;
end
%---------------------CONSTRUCT EQUILIBRIUM EQUATIONS---------------------%
%Preallocation
deltax=zeros(M,1);
deltay=zeros(M,1);
ux=zeros(M,1);
uy=zeros(M,1);
Ax=zeros(J,M);
Ay=zeros(J,M);
r=zeros(1,M);
%Calculates coefficients for matrix A.
for m=1:M
vec=find(C(:,m));
deltax(m)=abs(x(vec(2))-x(vec(1)));
deltay(m)=abs(y(vec(2))-y(vec(1)));
r(m)= magnitude(deltax(m),deltay(m));
ux(m)=deltax(m)/r(m);
uy(m)=deltay(m)/r(m);
if x(vec(1))<x(vec(2))
Ax(vec(1),m)=ux(m);
Ax(vec(2),m)=-ux(m);
else
Ax(vec(1),m)=-ux(m);
Ax(vec(2),m)=ux(m);
end
if y(vec(1))<y(vec(2))
Ay(vec(1),m)=uy(m);
Ay(vec(2),m)=-uy(m);
else
Ay(vec(1),m)=-uy(m);
Ay(vec(2),m)=uy(m);
end
end
A=[Ax Sx;Ay Sy]; %Creates matrix A
T=A\L; %Solves equilibrium equations
%--------------------------ANALYSIS OF RESULTS----------------------------%
f=plottruss(x,y,C,critmember,Lj);
set(f,'Visible','on','Name','NameTest')
  11 Comments
Ruben Mora
Ruben Mora on 22 Oct 2019
Edited: Ruben Mora on 22 Oct 2019
I made another code and it's giving an error. Could you run it and check it, please?
NodeCoordinates={{0,0,0},{10,5,0},{10,0,0},{20,8,0},{20,0,0},{30,9,0},...
{30,0,0},{40,8,0},{40,0,0},{50,5,0},{50,0,0},{60,0,0}};
ElemNodes={{1,3},{3,5},{5,7},{7,9},{9,11},{11,12},...
{1,2},{2,4},{4,6},{6,8},{8,10},{10,12},...
{2,3},{4,5},{6,7},{8,9},{10,11},...
{2,5},{4,7},{7,8},{9,10}};
PrintSpaceTrussNodeCoordinates(NodeCoordinates,'Node coordinates:',[1 0.000000 0.000000 0.000000; 2 10.000000 5.000000 0.000000; 3 10.000000 0.000000 0.000000; 4 20.000000 8.000000 0.000000; 5 20.000000 0.000000 0.000000; 6 30.000000 9.000000 0.000000; 7 30.000000 0.000000 0.000000; 8 40.000000 8.000000 0.000000; 9 40.000000 0.000000 0.000000; 10 50.000000 5.000000 0.000000; 11 50.000000 0.000000 0.000000; 12 60.000000 0.000000 0.000000
]);
numnod=Length(NodeCoordinates); numele=Length(ElemNodes);
Em=1000; Abot=2; Atop=10; Abat=3; Adia=1;
ElemMaterials= Table(Em,{numele});
ElemFabrications={Abot,Abot,Abot,Abot,Abot,Abot,Atop,Atop,Atop,Atop,...
Atop,Atop,Abat,Abat,Abat,Abat,Abat,Adia,Adia,Adia,Adia};
PrintSpaceTrussElementData(ElemNodes,ElemMaterials,ElemFabrications,...
'Element data:',[1 {1, 3} 1000.00 2.00 2 {3, 5} 1000.00 2.00 3 {5, 7} 1000.00 2.00 4 {7, 9} 1000.00 2.00 5 {9, 11} 1000.00 2.00 6 {11, 12} 1000.00 2.00 7 {1, 2} 1000.00 10.00 8 {2, 4} 1000.00 10.00 9 {4, 6} 1000.00 10.00 10 {6, 8} 1000.00 10.00 11 {8, 10} 1000.00 10.00 12 {10, 12} 1000.00 10.00 13 {2, 3} 1000.00 3.00 14 {4, 5} 1000.00 3.00 15 {6, 7} 1000.00 3.00 16 {8, 9} 1000.00 3.00 17 {10, 11} 1000.00 3.00 18 {2, 5} 1000.00 1.00 19 {4, 7} 1000.00 1.00 20 {7, 8} 1000.00 1.00 21 {9, 10} 1000.00 1.00
]);
ProcessOptions= {True};
view ={ {0,1,0},{0,0,1} };
labels={{True,0.06,-1.5,1.5},{True,0.12},{"Times",11,"Roman"}};
PlotSpaceTrussElementsAndNodes(NodeCoordinates,ElemNodes,...
'bridge mesh',{view,-1,labels}) ;
NodeDOFTags= Table({0,0,1},{numnod});
NodeDOFValues=Table({0,0,0},{numnod});
NodeDOFValues(3)=({0,-10,0});
NodeDOFValues(5)= {0,-10,0};
NodeDOFValues(7)={0,-16,0};
NodeDOFValues(9)={0,-10,0};
NodeDOFValues(11)={0,-10,0};
NodeDOFTags(1)= {1,1,1}; %(* fixed node 1 *)
NodeDOFTags(numnod)={0,1,1}; %(* hroller @ node 12 *)
PrintSpaceTrussFreedomActivity(NodeDOFTags,NodeDOFValues,...
'DOF Activity:',[1 1 1 1 0 0 0 2 0 0 1 0 0 0 3 0 0 1 0 -10 0 4 0 0 1 0 0 0 5 0 0 1 0 -10 0 6 0 0 1 0 0 0 7 0 0 1 0 -16 0 8 0 0 1 0 0 0 9 0 0 1 0 -10 0 10 0 0 1 0 0 0 11 0 0 1 0 -10 0 12 0 1 1 0 0 0
]);
%-----------------------------------------------------------------%
NodeDisplacements,NodeForces,ElemForces,ElemStresses = SpaceTrussSolution( NodeCoordinates,ElemNodes,ElemMaterials,ElemFabrications,NodeDOFTags,NodeDOFValues,ProcessOptions );
PrintSpaceTrussNodeDisplacements(NodeDisplacements,...
'Computed node displacements:',[1 0.000000 0.000000 0.000000 2 0.809536 -1.775600 0.000000 3 0.280000 -1.792260 0.000000 4 0.899001 -2.291930 0.000000 5 0.560000 -2.316600 0.000000 6 0.847500 -2.385940 0.000000 7 0.847500 -2.421940 0.000000 8 0.795999 -2.291930 0.000000 9 1.135000 -2.316600 0.000000 10 0.885464 -1.775600 0.000000 11 1.415000 -1.792260 0.000000 12 1.695000 0.000000 0.000000
]);
PrintSpaceTrussNodeForces(NodeForces,...
'Node forces including reactions:',[1 0.0000 28.0000 0.0000 2 0.0000 0.0000 0.0000 3 0.0000 -10.0000 0.0000 4 0.0000 0.0000 0.0000 5 0.0000 -10.0000 0.0000 6 0.0000 0.0000 0.0000 7 0.0000 -16.0000 0.0000 8 0.0000 0.0000 0.0000 9 0.0000 -10.0000 0.0000 10 0.0000 0.0000 0.0000 11 0.0000 -10.0000 0.0000 12 0.0000 28.0000 0.0000]);
PrintSpaceTrussElemForcesAndStresses(ElemForces,ElemStresses,...
'Int Forces and Stresses:',[1 56.0000 28.0000 2 56.0000 28.0000 3 57.5000 28.7500 4 57.5000 28.7500 5 56.0000 28.0000 6 56.0000 28.0000 7 -62.6100 -6.2610 8 -60.0300 -6.0030 9 -60.3000 -6.0300 10 -60.3000 -6.0300 11 -60.0300 -6.0030 12 -62.6100 -6.2610 13 10.0000 3.3330 14 9.2500 3.0830 15 12.0000 4.0000 16 9.2500 3.0830 17 10.0000 3.3330 18 1.6770 1.6770 19 3.2020 3.2020 20 3.2020 3.2020 21 1.6770 1.6770
]);
view={{0,1,0},{0,0,1}};
box={{0,-4,0},{60,-4,0},{60,10,0},{0,10,0}};
PlotSpaceTrussDeformedShape(NodeCoordinates,ElemNodes,NodeDisplacements,...
{0,1},box,'deformed shape (unit magnif)',{view,-1,{'black','blue'}});
PlotSpaceTrussStresses(NodeCoordinates,ElemNodes,ElemStresses,1,box,...
"axial stresses in truss members",{view,0,labels});
Daniel M
Daniel M on 22 Oct 2019
You didn't post the functions that you're calling. So I get an error on line 7 for an undefined function PrintSpaceTrussNodeCoordinates.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!