0

Suppose you want to create a uniform square grid of initial conditions on the (x,y) plane. This can be done using

s = 2;
step = 0.01;
data = Flatten[Table[{i, j}, {i, -s, s, step}, {j, -s, s, step}], 1];
ICs = Length[data]

Now my question is rather simple. How can I adjust the step so as the Length of the list to be equal to N, let's say N = 1000?

Many thanks in advance!

Vaggelis_Z
  • 8,740
  • 6
  • 34
  • 79
  • 2
    Not really a MMA question, to set the side length of your square grid to N, then use step = 2s/(N-1). – Quantum_Oli Dec 08 '15 at 11:15
  • 1
    But as Kuba says, you'll have a problem if you want to the the overall length of your flattened list to a non-square number. (As you'd have to use different step sizes in the i and j directions). – Quantum_Oli Dec 08 '15 at 11:16
  • 5
  • Since N@Array[ic, Sqrt[100] {1, 1}, {{-s, s}, {-s, s}}] // Flatten // Length works, then it is a duplicate. – Kuba Dec 08 '15 at 11:27
  • @Quantum_Oli I run several tests with the code proposed yesterday regarding the basins of attraction and the results seem free of obvious errors. I have in mind another type of plot and I really want your ideas on how to modify the code for this specific plot. – Vaggelis_Z Dec 08 '15 at 11:32

3 Answers3

1
In[55]:= stp[len_] := NSolve[(2./step1)*2 + 1 == Sqrt[len], step1]

In[56]:= stp[160801.]

Out[56]= {{step1 -> 0.01}}

In[57]:= stp[10000]

Out[57]= {{step1 -> 0.040404}}
Lotus
  • 2,671
  • 11
  • 10
1

We have Range and Table which require specifying the step size, and not the final list length (unlike MATLAB's linspace). Version 10.1 introduces the convenience function Subdivide which takes the number of intervals n into which the input interval will be subdivided. Thus it gives n+1 points.

You can generate an n by m grid using

Tuples[{Subdivide[xmin, xmax, n-1], Subdivide[ymin, ymax, m-1]}]

This is a sort of an analog of using linspace with meshgrid in MATLAB. I had the impression that you were looking for something like that.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
1
s = 2;
len = 100;

data =
 Table[{i, j}, {i, -s, s, (s - -s)/(len - 1)}, {j, -s, s, (s - -s)/(len - 1)}];

Dimensions@data

{100, 100, 2}

Generalization

MakeGrid[{x1_, x2_}, {y1_, y2_}, {xn_, yn_}] :=
 Table[{x, y}, {x, x1, x2, (x2 - x1)/(xn - 1)}, {y, y1, y2, (y2 - y1)/(yn - 1)}]

MakeGrid[{2, 4}, {1, 3}, {50, 100}] // Dimensions

{50, 100, 2}

eldo
  • 67,911
  • 5
  • 60
  • 168