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

12 vues (au cours des 30 derniers jours)
Sung Yeob Lim
Sung Yeob Lim le 9 Avr 2021
Commenté : Sung Yeob Lim le 9 Avr 2021
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 commentaires
dpb
dpb le 9 Avr 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.
Sung Yeob Lim
Sung Yeob Lim le 9 Avr 2021
Thank you for your help!

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by