Performing the for loop written in one function in another function.

5 views (last 30 days)
Hi everyone,
I'm working on a project for school where we're given several "skeleton" functions which we need to fill in and make all those functions work together in the end.
I have a for loop in one function called "shapeFunctions" that I actually want to run inside another function called "elemStiff".
The for loop in "shapeFunctions" looks like this:
for i = 1:length(xi)
% TODO
N = [(1 - xi(1,i)) * (1 - xi(2,i));
(1 + xi(1,i)) * (1 - xi(2,i));
(1 + xi(1,i)) * (1 + xi(2,i));
(1 - xi(1,i)) * (1 + xi(2,i))] .* 1/4;
dNdxi = [(-1 + xi(2,i)) (-1 + xi(1,i));
(1 - xi(2,i)) (-1 - xi(1,i)) ;
(1 + xi(2,i)) (1 + xi(1,i)) ;
(-1 - xi(2,i)) (1 - xi(1,i))] .* 1/4;
%
end
where xi for the above case is:
xi = [ -1/sqrt(3) 1/sqrt(3) -1/sqrt(3) 1/sqrt(3);
-1/sqrt(3) -1/sqrt(3) 1/sqrt(3) 1/sqrt(3)] ;
And I want to run the following in "elemStiff":
B(1,[1,3,5,7]) = dNdxi(:,1)';
B(2,[2,4,6,8]) = dNdxi(:,2)';
B(3,[1,3,5,7]) = dNdxi(:,2)';
B(3,[2,4,6,8]) = dNdxi(:,1)';
dxdxi = [dNdxi(:,1)' * elemCoord(:,1); dNdxi(:,2)' * elemCoord(:,1)]; % 1st column of Jacobian
dydxi = [dNdxi(:,1)' * elemCoord(:,2); dNdxi(:,2)' * elemCoord(:,2)]; % 2nd column of Jacobian
J = [dxdxi dydxi];
k = k + B' * D * B * det(J) * wList(1) * wList(2);
I could just write the for loop for N and dNdxi in the "elemStiff" function, but that would defeat the purpose of writing the "shapeFunction" function.
Any guidance is appreciated.
Thank you!
  4 Comments
dpb
dpb on 9 Apr 2021
Preallocate and assign subscripts...see the first example in the documentation for for...NB: it sets a 2D array; see the doc for zeros function on why.

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!