How to do you use a loop in a sub-function to extract values obtained in your main code?

I am trying to extract the values from the displacement matrix that correspond to the x and y values that were imported from a text file. Extraction of X and Y work, but the ux and uy matrices remain empty. Can someone help me please?
Thanks in advance.
%
displacement=stiffness\force
%
%plots undeformed and deformed structure
function PlotMeshonMesh(coordinates,nodes,displacement)
nel=length(nodes); %number of elements
nnel=size(nodes,2); %number of nodes per element
nnode=length(coordinates); %number of nodes
%
% empty required matrices
X=zeros(nnel,nel);
Y=zeros(nnel,nel);
ux=zeros(nnel,nel);
uy=zeros(nnel,nel);
scalefactor=10^6;
%
for iel=1:nel
for i=1:nnel
nd(i)=nodes(iel,i); %extract connected node for (iel)-th element
X(i,iel)=coordinates(nd(i),1); %extract x value of the node
Y(i,iel)=coordinates(nd(i),2); %extract y value of the node
for p=1:nnode
ux(i,iel)=displacement(2*p-1,1)*scalefactor %extract x value of the node with displacement
uy(i,iel)=displacement(2*p,1)*scalefactor %extract y value of the node with displacement
end
end
end
hold on;
%Plotting the FEM mesh and profile of the given component
f4 = figure ;
plot(ux,uy,'r')
hold on
plot(X,Y,'k')
hold off
end

7 Comments

Monique the line of code
displacement=stiffness\force
must be a copy and paste error else you wouldn't be able to run the above function and would observe some kind of error. Can you confirm that?
As for your ux and uy, your code is
for p=1:nnode
ux(i,iel)=displacement(2*p-1,1)*scalefactor %extract x value of the node with displacement
uy(i,iel)=displacement(2*p,1)*scalefactor %extract y value of the node with displacement
end
Is nnode nonzero? if so, note how you replace the values in the ux and uy matrices on each iteration. Is this what you want to be doing?
I have roughly 300 lines of code in this program whihc find everything I need to calculate the displacement and plot the contours. I did not post all of this.
nnode is the number of nodes in the system, in this case there are 28. My displacement vector is a 56x1 matrix where the odd rows have the x displacements and the even rows have the y displacements. I would like to extract these values and eventually add them to the corresponding values of x and y.
ok but you do see how you are overwriting ux and uy values on subsequent iterations of your loop:
for p=1:nnode
ux(i,iel)=displacement(2*p-1,1)*scalefactor %extract x value of the node with displacement
uy(i,iel)=displacement(2*p,1)*scalefactor %extract y value of the node with displacement
end
Is this intended? Have you stepped through the code with the debugger to see what values are being assigned to these matrices?
I didn't realize that I was overwriting ux and uy. It pulls out the correct values from the matrix but overwrites them. How can I fix this and how can I get it to save them in a matrix such that I can add the ux matrix to the X matrix and plot the deformed structure?
Perhaps create a third dimension?
for p=1:nnode
ux(i,iel,p)=displacement(2*p-1,1)*scalefactor %extract x value of the node with displacement
uy(i,iel,p)=displacement(2*p,1)*scalefactor %extract y value of the node with displacement
end
Or maybe displacement doesn't depend upon i and iel? (Since displacement is an input to the function...)
for iel=1:nel
for i=1:nnel
nd(i)=nodes(iel,i); %extract connected node for (iel)-th element
X(i,iel)=coordinates(nd(i),1); %extract x value of the node
Y(i,iel)=coordinates(nd(i),2); %extract y value of the node
ux(i,iel)=displacement(2*nd(i)-1,1)*scalefactor; %extract x value of the node with displacement
uy(i,iel)=displacement(2*nd(i),1)*scalefactor; %extract y value of the node with displacement
uX=X-ux;
uY=Y-uy;
end
end
This is what I did and it worked. Thanks for your suggestions.

Sign in to comment.

Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 9 Apr 2019

Commented:

on 11 Apr 2019

Community Treasure Hunt

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

Start Hunting!