Parker/Gambrel Truss Algorithm- undefined function or variable 'm'
Show older comments
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
You haven't defined m at that point yet. Nor have you defined r, so there is no need to index into it anyways. You also did not choose the size of the variable using inputs to the function zeros.
Also you shouldn't name a variable rem because it is the name of a native function rem().
Ruben Mora
on 21 Oct 2019
Edited: Ruben Mora
on 21 Oct 2019
Daniel M
on 21 Oct 2019
I can't run your code because I don't know what inputs to enter. Can you post something with the inputs already written in?
Ruben Mora
on 21 Oct 2019
Edited: Ruben Mora
on 21 Oct 2019
Daniel M
on 21 Oct 2019
Please upload an .m file that contains a test case already programmed in. I do not know what inputs you want, and it makes it more annoying to debug. Make it easy for people to help you.
Ruben Mora
on 21 Oct 2019
Daniel M
on 21 Oct 2019
This seems to be completely different code. It also executes without error. Is your issue solved?
Ruben Mora
on 22 Oct 2019
Daniel M
on 22 Oct 2019
If you have a specific question, I can try to help.
Ruben Mora
on 22 Oct 2019
Edited: Ruben Mora
on 22 Oct 2019
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.
Answers (0)
Categories
Find more on Structural Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!