Help with MatLab/C++ Loops (Heat Transfer, Matrices)

AI Thread Summary
The discussion focuses on solving a transient heat transfer conduction problem using MATLAB or C++. The user has a 12x12 coefficient matrix [A] and a 12x1 answer matrix [C], with initial conditions set to zero. The goal is to iteratively compute a new matrix [B] for each time step by plugging the results from the previous time step into the calculation. A suggested MATLAB loop structure is provided, emphasizing the use of efficient matrix operations. Proper initialization of matrices is also highlighted as a crucial step for successful implementation.
tectactoe
Messages
38
Reaction score
0
Basically, I need to solve a transient heat transfer conduction problem. I've got most of the work done but I need to solve the problem using MATLAB or C++ or some other kind of coding. That's what I need help with.

The actual aspects of the problem aren't really that important, so I will try to explain the part I need help with, without getting into too much detail.

I have a 12x12 matrix, we will call it [A]. This matrix contains coefficients for a series of equations that contain variables T_{n}^{i+1}, from n = 1 to 12. Then we have the answer matrix, we'll call matrix [C] which is a 12x1 matrix with values T_{n}^{i}, also from n = 1 to 12. Initially, at t=0, T_{n}^{i} = 0 for all n.

We then solve for matrix which contains the variables of which [A] holds the coefficients of, as mentioned before, T_{n}^{i+1}.

This can be easily done by <b> = [A]^{-1}[C]</b>

Then matrix will hold all my answers for T_{n}^{i+1} from n = 1 to 12.

Now my problem is, for the next time step, say t = 1s, I need to use the answers held in matrix and essentially plug those values in for the previous values held in matrix [C], and repeat the calculation for a new matrix after 1s. Then, I take those new values and sub those into the [C] matrix again and get yet another new matrix for 2s. I need to do this for very many time steps, so I know a loop must be used.

The problem is, I am awful with coding and don't know how this can be done.

Any help would be appreciated. If you are having trouble understanding my question, please ask and I will clarify.

Thank you!
 
Physics news on Phys.org
It seems you want a discrete time solution of an equation solvable by <b> = [A]^{-1}[C]</b>.

The loop you would need in MATLAB would look as such

Code:
% hint: MATLAB allows inverse to be performed as either 
% inv(A)*C or A\C the latter performs much faster. I will use the latter

%start program
Bo = A\Co %initial conditions at t=0
Cn(1) = Bo %stores your old answer in the new C

for i = 1:t

    %solves for current B vector using the new C matrix from time step i
    B(i) = A\Cn(i)

   %stores your old answer in the new C matrix for time step i+1
   Cn(i+1) = B(i)

end %end iteration

You can try something like that. And of course you need to initialize your matrices properly.
 
Back
Top