Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to calculate the cool down process of a regenerative heat exchanger. I solved the problem in Excel before, but now I want to do it with Mathematica. I want to create a table with the variables t and z. For t = 0, there is a function which gives the temperature for each layer z. For z = 0, there is another function which gives the temperature for the first layer for each t. the rest of the table should be calculated layer by layer, so that z is constant while t goes form t0 to tend, then z should increase 1 step and so on. I tried out some simple code, but it didn't work. What's worng? Are there any other ways to solve my problem?

  T[t + 1, z + 1] == T[t, z + 1] + T[t + 1, z], 
  T[t + 1, 0] == T[t, 0]/2, 
  T[0, z] == z + 2
  T, {t, 0, 6}, {z, 0, 4}] // Grid
share|improve this question
up vote 2 down vote accepted

This can be solved almost directly from your attempt by defining a recursive function. I've replaced your variable T with temp (for temperature), and added one more boundary condition. Something must be specified for temp[0,0] in order to have a well-defined function, temp[0,0] = 2 makes sense for consistency with the t=0 case.

temp[t_, z_] := temp[t - 1, z] + temp[t, z - 1];
temp[t_, 0] := temp[t - 1, 0]/2;
temp[0, z_] := z + 2;
temp[0, 0] := 2;

You can now ask for the values you want:

{temp[2,1], temp[5,6], temp[3,1]}

{9/2, 18017/16, 19/4}

or build a table of many

allTemps = Table[temp[i, j], {i, 0, 6}, {j, 0, 4}];

enter image description here

The OP noted that this runs slowly for large values. With luck, this can be sped up using the memoization trick, which is quite easy to program:

temp[t_, z_] := temp[t, z] = temp[t - 1, z] + temp[t, z - 1];
temp[t_, 0] := temp[t, z] = temp[t - 1, 0]/2;
temp[0, z_] := temp[0, z] = z + 2;
temp[0, 0] := temp[0, 0] = 2;

What this is doing is to cache the values that have already been computed (rather than recomputing them each time), hence trading off memory for execution speed.

share|improve this answer
Thanks a lot for your help. I tried it out for imax = 10 and jmax = 10 and it worked for my actual more complicated problem as well. The only problem is that there are 760 steps for z and 250 for t. So after ten minutes I had to abort the evaluation, because my laptop seems too slow for this calculation. I have to try it on my desktop. – Daniel Jun 18 '13 at 16:34
You can probably speed this up greatly if you wish. The technique called memoization is easy to do. I've added this to the answer. – bill s Jun 18 '13 at 16:44
It works:) Thanks again! – Daniel Jun 21 '13 at 11:13

Another way to solve this is using loops:

grid = {};
tab1 = Table[z + 2, {z, 0, 4}];
AppendTo[grid, tab1];
t = 1;
While[t <= 6,
tab = {};
z = 0;
While[z <= 4,
If[z == 0, AppendTo[tab, tab1[[z + 1]]/2], 
AppendTo[tab, tab1[[z + 1]] + tab[[-1]]]];
tab1 = tab;
AppendTo[grid, tab1];
Grid[grid // N]

output table

share|improve this answer
Thanks for your help. I also tried out your way to solve my problem. Now I have to check if it is faster than bills solution. – Daniel Jun 18 '13 at 16:39
You can optimize my code a bit more if you rewrite the inner loop so that you first create a new tab = {tab1[[z + 1]]/2]}, start with z = 1 and then you can get rid of the If[] test. – shrx Jun 18 '13 at 17:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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