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

In summary, the conversation discusses a problem involving solving a transient heat transfer conduction problem using MATLAB or C++ coding. The problem involves a 12x12 matrix with coefficients and a 12x1 answer matrix. A loop is needed to solve for the next time step using the previous answer matrix. The conversation also provides a suggested loop in MATLAB to solve the problem.
  • #1
tectactoe
39
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 [tex]T_{n}^{i+1}[/tex], from n = 1 to 12. Then we have the answer matrix, we'll call matrix [C] which is a 12x1 matrix with values [tex]T_{n}^{i}[/tex], also from n = 1 to 12. Initially, at t=0, [tex]T_{n}^{i} = 0[/tex] for all n.

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

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

Then matrix will hold all my answers for [tex]T_{n}^{i+1}[/tex] 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
  • #2
It seems you want a discrete time solution of an equation solvable by [tex] = [A]^{-1}[C][/tex].

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.
 

1. How can I use loops in MatLab/C++ to solve heat transfer problems?

Loops can be used in MatLab/C++ to solve heat transfer problems by creating a grid of points and iterating through each point to calculate the temperature at that point based on the surrounding points. This process can be repeated until the desired level of accuracy is achieved.

2. What is the difference between a for loop and a while loop in MatLab/C++?

A for loop in MatLab/C++ will iterate a specific number of times, while a while loop will continue to iterate until a certain condition is met. For loops are typically used when the number of iterations is known, while while loops are used when the number of iterations is dependent on a particular condition.

3. Can I use nested loops in MatLab/C++ for heat transfer calculations?

Yes, nested loops can be used in MatLab/C++ for heat transfer calculations. This involves using a loop within another loop to iterate through multiple dimensions of a matrix or grid. Nested loops are commonly used in heat transfer problems that involve 2D or 3D calculations.

4. How can I optimize my loop structure for faster computation in MatLab/C++?

To optimize your loop structure for faster computation in MatLab/C++, you can try to minimize the number of iterations or use vectorization techniques. Vectorization involves performing operations on entire arrays instead of individual elements, which can significantly improve performance.

5. Is it possible to exit a loop early in MatLab/C++?

Yes, it is possible to exit a loop early in MatLab/C++ using the break statement. This statement is commonly used within loops to check for a certain condition and exit the loop if that condition is met. This can be useful in situations where you want to stop the loop once a certain result is achieved.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
13
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Precalculus Mathematics Homework Help
Replies
1
Views
724
  • Engineering and Comp Sci Homework Help
Replies
23
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top