LogGrid

⇠ Back to Blog:Hacks

I put here a Mathematica module to generate a logarithmic grid of points between given lower and upper bounds.

LogGrid[lowerbound_, upperbound_, numpts_, accuracy_] := 
 Module[{grid, lb = Log[lowerbound], ub = Log[upperbound]}, 
  grid = Rationalize[#, 0] & /@ 
    Table[Exp[mPb], {mPb, lb, ub, (ub - lb)/(numpts - 1)}];
  If[accuracy != 0, Round[grid, accuracy], grid]]

The points of the grid are approximated by a rational number, to the given accuracy (last parameter), so that if you use the grid in arbitrary precision computation, you don't loose time (possibly an infinite amount) because of dealing with some irrelevant irrationality of the mesh.

In[1]:= LogGrid[1/10, 10, 5, .001]
Out[1]= {0.1, 0.316, 1., 3.162, 10.}