How to save output from loop's eteration into a new matrix.
1 view (last 30 days)
Show older comments
Areeb Siraj
on 30 Mar 2019
Commented: Areeb Siraj
on 31 Mar 2019
% for I quad
%%co-ordinadets of n and e
n = [-0.557 0.577 0.577 -0.577]; e = [-0.577 -0.577 0.577 0.577];
%%co-ordinadets of x and y
for j=10:10:100 for i=10:10:100 x = [i-10 i i i-10]; y = [j-10 j-10 j j];
A = -0.25*(1-n); B = -0.25*(1-e); C = 0.25*(1-n); D = -0.25*(1+e); E = 0.25*(1+n); F = 0.25*(1+e); G = -0.25*(1+n); H = 0.25*(1-e);
Ia = (A(1,1)*x(1,1)) + (C(1,1)*x(1,2)) + (E(1,1)*x(1,3)) + (G(1,1)*x(1,4)); Ja = (B(1,1)*x(1,1)) + (D(1,1)*x(1,2)) + (F(1,1)*x(1,3)) + (H(1,1)*x(1,4)); Ka = (A(1,1)*y(1,1)) + (C(1,1)*y(1,2)) + (E(1,1)*y(1,3)) + (G(1,1)*y(1,4)); La = (B(1,1)*y(1,1)) + (D(1,1)*y(1,2)) + (F(1,1)*y(1,3)) + (H(1,1)*y(1,4));
% M is jacobian matrix
Ma = [Ia Ja; Ka La]; Na = inv(Ma'); %Mi' = transpose of Jacobian matrix P1a = Na*[A(1,1); B(1,1)]; P2a = Na*[C(1,1); D(1,1)]; P3a = Na*[E(1,1); F(1,1)]; P4a = Na*[G(1,1); H(1,1)];
Qa = [P1a(1,1) 0 P2a(1,1) 0 P3a(1,1) 0 P4a(1,1) 0; 0 P1a(2,1) 0 P2a(2,1) 0 P3a(2,1) 0 P4a(2,1); P1a(2,1) P1a(1,1) P2a(2,1) P2a(1,1) P3a(2,1) P3a(1,1) P4a(2,1) P4a(1,1)]; S = 180; %% value of E = 180 N/mm2 for steel T = 78; %% value of G = 78 N/mm2 for steel U = 34; %% value of poison's ratio V = [(1/S) (-U/S) 0; (-U/S) (1/S) 0; 0 0 (1/T)]; W = inv(V); X1 = 1; %% weighted fucntion w1 X2 = 1; %% weighted fucntion w2 t = 1; %% thickness X3a = det(Ma); %% determinant of jacobian matrix Ya = X1*X2*Qa'*W*Qa*t*X3a;
% for II quad
Ib = (A(1,2)*x(1,1)) + (C(1,2)*x(1,2)) + (E(1,2)*x(1,3)) + (G(1,2)*x(1,4)); Jb = (B(1,2)*x(1,1)) + (D(1,2)*x(1,2)) + (F(1,2)*x(1,3)) + (H(1,2)*x(1,4)); Kb = (A(1,2)*y(1,1)) + (C(1,2)*y(1,2)) + (E(1,2)*y(1,3)) + (G(1,2)*y(1,4)); Lb = (B(1,2)*y(1,1)) + (D(1,2)*y(1,2)) + (F(1,2)*y(1,3)) + (H(1,2)*y(1,4));
% M is jacobian matrix Mb = [Ib Jb; Kb Lb]; Nb = inv(Mb'); %Mi' = transpose of Jacobian matrix P1b = Nb*[A(1,2); B(1,2)]; P2b = Nb*[C(1,2); D(1,2)]; P3b = Nb*[E(1,2); F(1,2)]; P4b = Nb*[G(1,2); H(1,2)];
Qb = [P1b(1,1) 0 P2b(1,1) 0 P3b(1,1) 0 P4b(1,1) 0; 0 P1b(2,1) 0 P2b(2,1) 0 P3b(2,1) 0 P4b(2,1); P1b(2,1) P1b(1,1) P2b(2,1) P2b(1,1) P3b(2,1) P3b(1,1) P4b(2,1) P4b(1,1)]; X3b = det(Mb); %% determinant of jacobian matrix Yb = X1*X2*Qb'*W*Qb*t*X3b;
% for III quad
Ic = (A(1,3)*x(1,1)) + (C(1,3)*x(1,2)) + (E(1,3)*x(1,3)) + (G(1,3)*x(1,4)); Jc = (B(1,3)*x(1,1)) + (D(1,3)*x(1,2)) + (F(1,3)*x(1,3)) + (H(1,3)*x(1,4)); Kc = (A(1,3)*y(1,1)) + (C(1,3)*y(1,2)) + (E(1,3)*y(1,3)) + (G(1,3)*y(1,4)); Lc = (B(1,3)*y(1,1)) + (D(1,3)*y(1,2)) + (F(1,3)*y(1,3)) + (H(1,3)*y(1,4));
% M is jacobian matrix Mc = [Ic Jc; Kc Lc]; Nc = inv(Mc'); %Mi' = transpose of Jacobian matrix P1c = Nc*[A(1,3); B(1,3)]; P2c = Nc*[C(1,3); D(1,3)]; P3c = Nc*[E(1,3); F(1,3)]; P4c = Nc*[G(1,3); H(1,3)];
Qc = [P1c(1,1) 0 P2c(1,1) 0 P3c(1,1) 0 P4c(1,1) 0; 0 P1c(2,1) 0 P2c(2,1) 0 P3c(2,1) 0 P4c(2,1); P1c(2,1) P1c(1,1) P2c(2,1) P2c(1,1) P3c(2,1) P3c(1,1) P4c(2,1) P4c(1,1)]; X3c = det(Mc); %% determinant of jacobian matrix Yc = X1*X2*Qb'*W*Qb*t*X3c;
% for IV quad
Id = (A(1,4)*x(1,1)) + (C(1,4)*x(1,2)) + (E(1,4)*x(1,3)) + (G(1,4)*x(1,4)); Jd = (B(1,4)*x(1,1)) + (D(1,4)*x(1,2)) + (F(1,4)*x(1,3)) + (H(1,4)*x(1,4)); Kd = (A(1,4)*y(1,1)) + (C(1,4)*y(1,2)) + (E(1,4)*y(1,3)) + (G(1,4)*y(1,4)); Ld = (B(1,4)*y(1,1)) + (D(1,4)*y(1,2)) + (F(1,4)*y(1,3)) + (H(1,4)*y(1,4));
% M is jacobian matrix Md = [Id Jd; Kd Ld]; Nd = inv(Md'); %Mi' = transpose of Jacobian matrix P1d = Nd*[A(1,4); B(1,4)]; P2d = Nd*[C(1,4); D(1,4)]; P3d = Nd*[E(1,4); F(1,4)]; P4d = Nd*[G(1,4); H(1,4)];
Qd = [P1d(1,1) 0 P2d(1,1) 0 P3d(1,1) 0 P4d(1,1) 0; 0 P1d(2,1) 0 P2d(2,1) 0 P3d(2,1) 0 P4d(2,1); P1d(2,1) P1d(1,1) P2d(2,1) P2d(1,1) P3d(2,1) P3d(1,1) P4d(2,1) P4d(1,1)]; X3d = det(Md); %% determinant of jacobian matrix Yd = X1*X2*Qd'*W*Qd*t*X3d;
% equvalent matix Yeq = Ya + Yb + Yc + Yd; end end
This is my code and I want save output from loop's eteration in a matrix of size 80 by 80 for further formulation. Such that the first element of this new matrix is the first eteration of loop and so on.
3 Comments
Jan
on 30 Mar 2019
Edited: Jan
on 30 Mar 2019
Seriously?
Do not overestimate the willingness of the forum to fix unreadable code, to guess the meaning of vague questions and to follow badly formatted links to not existing web pages. Of course there is no server called "www-mathworks-com-...", but it must be "www.mathworks.com/...".
Accepted Answer
Rebecca Cleveland Stout
on 30 Mar 2019
Your code is quite hard to read, but generally, to save elements in a loop, you can do:
A = NaN*ones(80); % preallocate matrix where you want to save stuff
for k = 1:numel(A)
% do your operations
A(k) = result; % save your kth result to element k of the matrix
end
Check out https://www.mathworks.com/matlabcentral/answers/327317-how-to-save-output-of-for-loop if you're still confused.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!