Re: Memory problem using NDSolve in For Loop

*To*: mathgroup at smc.vnet.net*Subject*: [mg83361] Re: Memory problem using NDSolve in For Loop*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>*Date*: Sun, 18 Nov 2007 04:50:10 -0500 (EST)*Organization*: The Open University, Milton Keynes, UK*References*: <fhmhrn$atl$1@smc.vnet.net>

DBK wrote: > I am running a number of simulations (sim) with different initial > conditions (ic). The code I have written runs fine until I increase > the number of simulations. In my "For" loop below, I thought that > previous solutions (sol[sim]) were being cleared but this obviously is > not happening. How do I clear sol and reduce my memory usage? Sorry if > this is pretty basic stuff. > > For[sim = 1, sim < 2500, sim = sim + 1, > sol[sim] = > NDSolve[{equations, a[0] == 10, b[0] == .45, c[0] == .30, > d[0] == .40, e[0] == ic[[sim, 1]], f[0] == ic[[sim, 2]]}, {a, b, > c, d, e, f}, {t, 0, time}, MaxSteps -> Infinity, > WorkingPrecision -> 5, MaxStepSize -> 0.10]; > AppendTo[outlist, {ic[[sim, 1]], ic[[sim, 2]], e[time] /. sol[sim], > f[time] /. sol[sim]}]]; As written, the for loop creates 2500 variables sol[1], sol[2], ..., sol[2500]. Apparently, you are just holding the result of NDSolve, sol[n], during the current cycle, say n, and then never touch it again. So why, in first instance, would you create so many symbols? You can just use one symbol, say sol, as in the following snippet: For[sim = 1, sim < 2500, sim = sim + 1, sol = NDSolve[{equations, a[0] == 10, b[0] == .45, c[0] == .30, d[0] == .40, e[0] == ic[[sim, 1]], f[0] == ic[[sim, 2]]}, {a, b, c, d, e, f}, {t, 0, time}, MaxSteps -> Infinity, WorkingPrecision -> 5, MaxStepSize -> 0.10]; AppendTo[ outlist, {ic[[sim, 1]], ic[[sim, 2]], e[time] /. sol, f[time] /. sol}]]; Regards, -- Jean-Marc