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

5 次查看(过去 30 天)
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 个评论
dpb
dpb 2021-4-9
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by