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'm afraid this is probably not going to be a "good" question, but I'd like to use Mathematica to learn about basic numerical schemes for solving pdes. For example, I'd like to compute the solution of a 2D heat equation initial value problem using finite differences and Euler timestepping, and then look at how big errors are or stability (not interested in using NDSolve directly).

My question is, are there any resources that already do this the "right" way? I'm just getting acquainted with Mathematica and don't know the language well, but I do know I want to use packed arrays and avoid loops. There's lots of language-agnostic references for the numerical methods themselves, but I can't find good clean examples of Mathematica implementations of these methods.


One of the books recommended in the answers suggest doing stuff like (1d heat equation IVP):

gridX = 10; (* num interior points *)
dx = 1./gridX;
dt = .005; (*cfl: dt/dx^2 <= .5 *)

T[i_, j_] := T[i, j] = T[i, j - 1] + 
                  (dt/dx^2) (T[i + 1, j - 1] - 2 T[i, j - 1] + T[i - 1, j - 1]);

T[i_, 0] := 1. (* initial value *)

T[0,         j_] := 0.; (* boundaries *)
T[gridX + 1, j_] := 0.; 

TFinal[t_] := Table[T[i, IntegerPart[t/ dt]], {i, 1, gridX}];

But I suspect that it's not the best way, because as soon as I crank up the grid size and shrink down the time step, I get a recursion limit error. The code above is storing the solution at all time steps, which I definitely don't want. I'm sure there are clean, non-recursive sample implementations of this method out there, just can't seem to find.

share|improve this question
trott's books are very good:Trott – Spawn1701D Apr 10 '13 at 4:35
a book is not a resource? in any case, i'm just interested in seeing some code, don't care if it's in a book, a website, or a stone tablet. what i'm definitely not interested in are comments like yours. just move along. – random dude Apr 10 '13 at 16:21

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.