Plotting solutions of a for loop involving matrices.. Need to plot at each time step.

I'll show the code first and explain it afterwards....
T1i = 30;
T2i = 30;
T3i = 30;
T4i = 30;
T5i = 30;
T6i = 30;
T7i = 30;
T8i = 30;
T9i = 30;
T10i = 30;
T11i = 30;
T12i = 30;
A = (A is a 12 x 12 matrix with coefficients from a system of equations, no need to list it out here)
B = [T1i; T2i; T3i; T4i; T5i; T6i; T7i; T8i; T9i; T10i; T11i; T12i];
D = [2.25; 2.25; 2.25; 0; 0; 0; 3.125; 0; 0; 0; 0; 0];
fprintf('Here are the temperatures of each node after 360 seconds\n\n');
for r = 1:360
E = B + D;
C = A^-1*E;
B = C;
end
Basically when you solve for C in the loop, you're solving for a system of 12 unknowns and equations using matrices. The answer matrix, C, then gets thrown back into the original equation during the for loop in place of the old B matrix (by saying B = C at the end) and the calculations are done again, with the new B matrix. Then the C matrix produced that time gets tossed back in and this is repeated 360 times.
This is a heat transfer problem and there are 12 nodes and thus 12 temperatures. The initial temp of each node is 30 degrees Cel. This explains the initial B matrix. The equations for the 12 x 12 matrix are already calculated to one time step equal to 1 second. So basically, each iteration of the loop yields a matrix (matrix C, a 12x1 matrix) which holds the temperature of each node after said number of iterations, where 1 iteration = 1 second.
Bottom Line: I need to plot each node temperature over the time (from 0 seconds to 360 seconds). One plot with twelve curves, all starting at 30 degrees on one axis, t = 0 on the other. Due to heating elements, the curves will all end up differently but obviously start at the same spot.
Does anyone have any idea how I would go about this? I've racked my brain with Google and can't figure it out.
Thanks.

 Accepted Answer

Make B a 12x360 matrix where you store the result at each time step:
% Don't mind this, just making a random A matrix...
A = rand(12); [U,S,V] = svd(A); A = U*V';
nmax = 360;
B = zeros(12,nmax);
B(:,1) = 30;
D = [2.25; 2.25; 2.25; 0; 0; 0; 3.125; 0; 0; 0; 0; 0];
[L,U] = lu(A);
for n = 2:nmax
E = B(:,n-1)+D;
B(:,n) = U\(L\E);
end
plot(B');
You don't need to calculate the inverse of a matrix to solve a linear system. Instead of inv(A)*b, use A\b or the LU factorization.

More Answers (1)

Before the loop,
B{i} = [T1i; T2i; T3i; T4i; T5i; T6i; T7i; T8i; T9i; T10i; T11i; T12i];
Then change the loop to
for r = 1:360
E = B{i} + D;
C = A^-1*E;
B{i+1} = C;
end
Then at the end, B will be a cell array from 1 to 361 each member of which holds a copy of the heat array as of the beginning of a time step.
Hint: cat(3,B{:})

Categories

Community Treasure Hunt

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

Start Hunting!