I want to store the value of matrix in each iteration and add it to the stored matrix in the next iteration. At the end I would get the sum of all matrices in the loop.

4 views (last 30 days)
Q=[ 171.1 3.622 0; 3.622 12.07 0 ; 0 0 4.5]
s=input('Total number of Layers:')
for n=1:s
x = input ('Enter the value of angle for the layer in Degrees:')
c=cos(degtorad(x));
s=sin(degtorad(x));
thetha=[c^2 s^2 -2*s*c; s^2 c^2 2*c*s; c*s -c*s c^2-s^2];
psi=[c^2 s^2 -s*c; s^2 c^2 c*s; 2*c*s -2*c*s c^2-s^2];
qbar=thetha*Q*inv(psi)
y=n;
disp(y)
A=qbar*input('Thickness of each layer:')*input('Number of layers:')
end
This is my code and I want to save the sum of A of all iterations in a separate variable (suppose B). How do I do that

Answers (2)

Andrei Bobrov
Andrei Bobrov on 6 Feb 2016
Edited: Andrei Bobrov on 6 Feb 2016
Q = [ 171.1 3.622 0; 3.622 12.07 0 ; 0 0 4.5];
s0 = input('Total number of Layers:');
A = zeros(3,3,s0);
for n=1:s0
x = input ('Enter the value of angle for the layer in Degrees:');
x = degtorad(x);
c = cos(x);
s = sin(x);
c2 = c^2;
s2 = s^2;
cs = c*s;
cs2 = 2*cs;
cs22 = c2-s2;
thetha = [c2 s2 -cs2; s2 c2 cs2; cs -cs cs22];
psi = [c2 s2 -cs; s2 c2 cs; cs2 -cs2 cs22];
qbar = thetha*Q/psi;
disp(n);
A(:,:,n) = qbar*input('Thickness of each layer:')*input('Number of layers:');
end
B = sum(A,3);

Walter Roberson
Walter Roberson on 6 Feb 2016
Before the loop,
B = 0;
At the end of the loop, after calculating A but before the "end", add
B = B + A;

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!