I am having a problem with transferring the heat flux boundary conditions into a temperature to be able to put it into a matrix. I understand that deltat = deltax*q''/k but I do not know how to code it so that I can loop it into the matrix in MATLAB. I have not had heat transfer and it is a steady state problem, so it should be relatively simple.

How could you index code to be able to loop through since deltat = T(i,j) - T(I+1,J)?

code Schematic for Problem

Thank you for any help!

  • $\begingroup$ Your second for-loop has a semicolon in it which will lead to problems $\endgroup$
    – nluigi
    Nov 29, 2017 at 11:59
  • $\begingroup$ Could you please put the code in text form rather than as images? If you indent it with four spaces it will get syntax highlighting. $\endgroup$ Nov 29, 2017 at 14:59

1 Answer 1


You haven't specified which method you are using, so i am going to assume Finite Volume. In that case you have a staggered grid like so:

enter image description here

The vertical lines are the faces of you cells and the circles are the centers of you cells. Your left boundary is located at the face at $x=0$ and your right boundary is located at the face at $x=L$. The domain of size $L$ is divided into $N$ cells. To simplify the boundary treatment, I have included two ghost / virtual nodes $i=0$ and $i=N+1$. I assume a uniform gridspacing $\Delta x=L/N.$

Now how do you impose a flux condition at $x=0$ in terms of local temperatures? Well from a Taylor expansion around $x=0$ we find: $$ T_{i=0} = T_{x=0} - \left.\frac{dT}{dx}\right|_{x=0}\left(\frac{\Delta x}{2}\right) + \frac{1}{2}\left.\frac{d^2T}{dx^2}\right|_{x=0}\left(\frac{\Delta x}{2}\right)^2 + O\left(\Delta x^3\right) $$ and: $$ T_{i=1} = T_{x=0} + \left.\frac{dT}{dx}\right|_{x=0}\left(\frac{\Delta x}{2}\right) + \frac{1}{2}\left.\frac{d^2T}{dx^2}\right|_{x=0}\left(\frac{\Delta x}{2}\right)^2 + O\left(\Delta x^3\right) $$ Subtracting: $$T_{i=1} - T_{i=0} = \left.\frac{dT}{dx}\right|_{x=0}\Delta x + O\left(\Delta x^3\right)$$ If $\Delta x \ll 1$ we can neglect the $O\left(\Delta x^3\right)$ terms and we find the flux at $x=0$ in terms of the local temperatures at $i=0$ and $i=1$: $$\left.q"\right|_{x=0}=-\lambda\left.\frac{dT}{dx}\right|_{x=0} = -\lambda\frac{T_{i=1} - T_{i=0}}{\Delta x}$$

So to impose the flux at $x=0$ you need to impose the local temperature of the ghost node at $i=0$ according to above equation.

A similar analysis can be done for the boundary at $x=L$, I'll leave that for OP.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.