First off, it's apparent that k needs to take some (positive integer) value, since it's the end-value of the iterator later on. So I add:
k = 15;
The next bit of the code sets up a recursive function where inttstar[i] depends on periods i-1 and i+1. (This looks a lot like some economic model to be solved.) Notice the inttstar[i_]:= inttstar[i] = (* etc *) which ensures that existing results are saved and aren't recalculated every time.
inttstar[1] = (1/8)*(tstar[2] - tstar[0]);(*Eq 2.31*)
inttstar[i_] :=
inttstar[i] = (tstar[i + 1]/(i + 1) - tstar[i - 1]/(i - 1))/4 - (-1)^i*
tstar[0]/(2 (i^2 - 1));(*Eq 2.30*)
So for k=15, you get:
inttstar[k]
tstar[0]/448 + 1/4 (-(tstar[14]/14) + tstar[16]/16)
This is the bit that marks the original author out as someone who knows little about Mathematica programming. It's setting up some empty arrays to be filled in later.
Do[c[j] = {}, {j, 0, k}]
Then they fill in the empty arrays with coefficients from the above equations.
Do[Do[c[j] = Append[c[j], Coefficient[inttstar[j], tstar[i], 1]], {i, 0,
k}], {j, 0, k}]
For example:
c[14]
(* {-(1/390), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -(1/52), 0, 1/60} *0
And then (for goodness sake!) they do the exact same thing of setting up another empty vector to store all the c[index] results. Notice the hard-coded 15 there, versus k further up. Very sloppy.
r = {};
Do[r = Append[r, c[i]], {i, 0, 15}]
The end result is a coefficient matrix
r
{{1/2, 1/4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {-(1/8), 0, 1/8, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {-(1/6), -(1/4), 0, 1/12, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0}, (* etc *)
So how can we improve this? The main thing to do would be to get rid of the Matlab/Fortran style song and dance routine with the empty lists that get appended to in Do loops. Notice the order of the iterators in the Table command. You then wouldn't have the c[j] variables defined, but judging by the result, all that was wanted was a coefficient matrix to go into some solution technique anyway. Yes, this one line can replace everything starting from that first Do loop.
Clear[r];
r = Table[Coefficient[inttstar[j], tstar[i], 1], {j, 0, k}, {i, 0, k}]
Some links you might find useful:
Avoiding procedural loops (common pitfalls)
Alternatives to procedural loops
The point is that it's not just an issue of speed and efficiency of code or what is more "Mathematica-ish" in coding style. This is an issue of readability. When you load your code up with all those loops and intermediate temporary variables, you make it harder to follow what the code is doing - exactly why you have found it difficult to understand what this past student's code does. Mathematica is my first and only programming language, so I find it hard to understand why people put up with all that hard-to-read procedural code.
As an aside, doing all this in exact numbers rather than floating point numbers will make the solution slower than you might like. I'd suggest adding an N[] command around the definition of r, so it would be r= N[Table[....(* etc *). I'm willing to bet you'll need to solve this numerically anyway using NSolve. This looks like a rational-expectations model in economics, and most of those don't have closed-form solutions.
c) by appending a list of coefficients oftstartgenerated byintstar. Run it (assuming an appropriatekis set), then in a session evaluateintstar[somenumber], observe the result. Then evaluateCoefficient[%,tstar[somenumber]]and you'll see the coefficient associated with that (if it exists)tstarterm. As an aside, this is not a good example of MM code, it would be a good exercise to re-write it. – ciao Jan 22 '14 at 01:54