Re: NMinimize
- To: mathgroup at smc.vnet.net
- Subject: [mg81897] Re: NMinimize
- From: Raj <rajanikanth at gmail.com>
- Date: Sat, 6 Oct 2007 04:36:45 -0400 (EDT)
- References: <28656799.1191337430978.JavaMail.root@m35>
Thanks for your suggestions. I was able to optimize the code using the Evaluate function. It reduced the memory consumption of the code from around 17 GB to around 1 GB. Thanks, Raj On Oct 3, 11:48 am, DrMajorBob <drmajor... at bigfoot.com> wrote: > It all depends on what the meaning of "do this" is. Without data, it's > hard to say. > > Offhand, calculating Select again for every value of Di, De, and i -- when > it depends only on i -- can't be a good idea. For that, you can define > > tiSelected[i_] := > tiSelected[i] = Select[ti, i - 2880 <= #[[2]] <= i - 1 &] > > or > > tiSelected[i_] := > tiSelected[i] = Pick[ti, ti[[All, 2]], x_ /; i - 2880 <= x <= i - 1] > > and use tiSelected[i] in theNMinimizestatement. > > You can do even better, since this ALSO depends only on i: > > Plus @@ ((func[15.2, Di, De, i, First@#, Last@#] &) /@ > Select[ti, i - floor <= #[[2]] <= i - 1 &]) > > if Di and De are left in symbolic form for now. One way to do this is > > Clear[di, de, func] > plus[i_] := > plus[i] = func[15.2, di, de, i, First@#, Last@#] & /@ tiSelected[i] > > (You can experiment with defining func before or after this. If before, > then don't Clear it. Evaluate plus[20001] and see what you get, each way.) > > Another optimization comes from realizing that Plus@@Table is the same as > Sum and, so that > > f[di_, de_] = Sum[data[[i - 20000]] - plus[i], {i, 22881, 25000}]; > > or -- better yet -- > > f[di_, de_] = Total@data[[881 ;; 3000]] - Sum[plus[i], {i, 22881, 25000}] > > (double-check those limits) allows the final result to be > > NMinimize[{f[di, de], 10^-8 <= di <= 10^-1, > 10^-8 <= de <= 10^-1}, {di, de}] > > f[di,de] will be a very large expression, so that level of optimization > may not work out... but "tiSelected" and "plus" should still be > worthwhile. In that case, defining func BEFORE plus may be best. (Or not. > Try it out.) > > The functions change when ti and "data" change, of course, but not within NMinimize. Without data I can't fully test it, but these are the kind of > "tricks" you'll need. > > Bobby > > > > On Tue, 02 Oct 2007 04:47:30 -0500, Raj <rajanika... at gmail.com> wrote: > > hi! > > Could somebody tell me if there is a better way to do this in > > Mathematica: > > > data = Flatten@Import["moistureAtDepth1.dat"]; > > data = data[[20000 ;; 30000]]; > > ti = Import["times.dat"]; > > func[x_, Di_, De_, t_, t1_, t2_] := > > (Erfc[x/Sqrt[2400 Di (t - t1)]] - > > Erfc[x/Sqrt[2400 Di (t - t2)]] + > > Erf[x/Sqrt[2400 De (t - t2) ]]) > > parameters = {Di,De}; > >NMinimize[ {Plus@@Table[ (data[[i-20000]] - > > Plus @@ ((func[15.2, Di, De, i, First@#, Last@#] &) /@ > > Select[ti, i - 2880 <= #[[2]] <= i-1 &]))^2,{i,22881,25000}], > > 10^-8<=Di<=10^-1,10^-8<=De<=10^-1},parameters] > > > I am interested in Optimizing theNMinimizeline. > > > Thanks, > > > Raj > > -- > DrMajor... at bigfoot.com