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

조회 수: 4 (최근 30일)
Sung Yeob Lim
Sung Yeob Lim 2021년 4월 9일
댓글: Sung Yeob Lim 2021년 4월 9일
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