0
$\begingroup$

I'm trying to define a random potential on the 2D unit square. For some fixed j, I want to decompose the unit square into $2^{2j}$ smaller squares with sides length $2^{-j}$. On each of the smaller squares I want to assign a value of $\{-1,+1\}$ randomly.

I think I want to define a function, e.g.

     V[x_,y_]:=Something

where the something takes in [x,y] (a point in the unit square) and returns the value {-1,+1} depending on which grid I landed in.

I'm pretty stuck because I'm not quite sure how to define my V function. I imagined using characteristic functions along with

RandomInteger[{-1, 1}] 

but I'm quite stuck and I think I'm missing something really simple.

Would someone care to enlighten me on what I'm missing?

EDIT: The below answer from Carl helps a lot but I run into issues now with defining my differential operator V. So what I want to now do is:

  L[x_, y_] := (-Laplacian[#, {x, y}]) + Evaluate[V[x, y]]*# &

But when I try to find the Eigensystem using NDEigensystem I get the error

NearestFunction::neard: The default distance function does not give a real numeric distance when applied to the point pair {x,y} and {1/30,1/30}.

$\endgroup$
  • $\begingroup$ for the the first part: j=2; lattice=Tuples[Subdivide[0,1,2^j],{2}]; Graphics[{Opacity[.5,Yellow],Rectangle[], Red, PointSize[Large],Point@lattice}]? $\endgroup$ – kglr May 28 '18 at 11:28
  • $\begingroup$ Welcome to our Mathematica site. As you are new, it might be interesting to you that you can format your question with inline code, block code, quotes, etc. Please refer to this tutorial to format your questions properly. $\endgroup$ – halirutan May 28 '18 at 13:01
2
$\begingroup$

Here is one possibility. FIrst generate a grid of points centered at each square:

pts[n_] := Tuples[Range[0,n-1]/n + 1/(2n), 2]

For example, create a grid of $5 \times 5$ by points:

Graphics[
    Point @ pts[5],
    GridLines->{Range[0,5]/5, Range[0,5]/5},
    PlotRange->{{0,1}, {0,1}}
]

enter image description here

Then, use Nearest to create your function:

makeV[n_] := With[{nf = Nearest[Thread[pts[n] -> RandomChoice[{-1,1}, n^2]]]},
    First @ nf[{##}]&
]

Here's a visualization:

V = makeV[15];
ContourPlot[V[x, y], {x, 0, 1}, {y, 0, 1}]

enter image description here

Addendum

There's a problem with this approach when V is fed non-numeric arguments:

V[x,y]

NearestFunction::neard: The default distance function does not give a real numeric distance when applied to the point pair {x,y} and {1/30,1/30}.

{x, y}

The following alternative works around this issue:

Clear[V]
With[{v=makeV[15]}, V[x__?NumericQ] := v[x]]

Then:

V[x, y]

V[x, y]

doesn't cause errors, and:

V[.6, .9]

1

still works.

$\endgroup$
  • $\begingroup$ Thanks @Carl this helps a lot. But I'm now running into issues when I am trying to define an operator with V I get the following error: NearestFunction::neard: The default distance function does not give a real numeric distance when applied to the point pair {x,y} and {1/30,1/30}. (see edit of original question for details) $\endgroup$ – istigatrice Jun 1 '18 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.