Below is my code for numerical solving of PDE with Crank Nicolson scheme.
ClearAll[L, M, \[CapitalDelta]t, \[CapitalDelta]x]
L = Pi // N; M = 25;
\[CapitalDelta]x = L/M;
\[CapitalDelta]t = \[CapitalDelta]x^2; k = 1;
U0[x_] = x (Pi - x);
U[0, n_] := 0.;
U[M, n_] := 0.;
sol[0] = Table[U[j, 0] -> U0[j \[CapitalDelta]x], {j, 1, M - 1}];
sol[n_] :=
Module[{vars, eqns}, vars = Table[U[j, n], {j, 1, M - 1}];
eqns = Table[
U[j, n] - U[j, n - 1] ==
1/2 ((k \[CapitalDelta]t )/\[CapitalDelta]x^2 (U[j + 1, n] -
2 U[j, n] + U[j - 1, n] + U[j + 1, n - 1] - 2 U[j, n - 1] +
U[j - 1, n - 1])), {j, 1, M - 1}] /. sol[n - 1];
Solve[eqns, vars][[1]]
]
The problem is, that I have to eveluate value of scheme for (Pi/4,2). That means i have to put 127 for n in code below. The problem is, that the program doesn't want to eveluate for such big numbers.
rez = Table[
Table[{\[CapitalDelta]x j, \[CapitalDelta]t n, U[j, n]}, {j, 0,
M}] /. sol[n], {n, 127}]
But if you go bigger for n, like 100, 400, and more it gives:
$RecursionLimit::reclim: Recursion depth of 256 exceeded. >>
$RecursionLimitto a bigger value, or evenInfinity. You will have to be careful if you do$RecursionLimit = Infinity, though. – J. M.'s missing motivation May 18 '13 at 17:14$RecursionLimit = Infinityis never appropriate. I realize that I may be biased by my software development (rather than problem - solving) perspective, but it is all too easy to lose unsaved data by using this setting. – Leonid Shifrin May 18 '13 at 17:22Block[{$RecursionLimit = Infinity}, (* stuff *)]instead of setting it globally. I did say that OP has to be careful; in particular, he should be sure that the recursion he has is guaranteed to terminate, but usually people are unable or unwilling to do such proofs. – J. M.'s missing motivation May 18 '13 at 17:26Block- this is better, but IMO not enough. Actually, presence of absence ofBlockis irrelevant for my main argument. My point is that even withBlock, setting$RecursionLimit = Infinitycan easily overflow the stack which leads to a kernel crash. Happened to me more than once :-). – Leonid Shifrin May 18 '13 at 17:30$IterationLimitrather than$RecursionLimit, and the former are safe (it is safe to set$IterationLimittoInfinity). – Leonid Shifrin May 18 '13 at 17:34sol[]; that is,sol[n_Integer] := Block[{$RecursionLimit = Infinity}, (* stuff *)]– J. M.'s missing motivation May 18 '13 at 17:42