3
$\begingroup$

I would like to define a simple function that gives random smooth bumps in a plane, a bit like the clumsy picture below, but it should be continuous:

Plot3D[RandomReal[{-1, 1}], {x, 0, 5}, {y, 0, 5}]

enter image description here

As a function of both $x$ and $y$, it should be smooth everywhere and be differentiable at least once. How can we define such a function RandomFun[x, y] (pun intended) with Mathematica?

Ideally, it should be as simple as possible and be valid for Mathematcia 7 (RandomFunction doesn't seem to be recognized with this version). Please, nothing fancy. Just the simplest way to get smooth bumps of height between -1 and 1.

$\endgroup$
1
  • $\begingroup$ What's wrong with tt = Table[RandomReal[{-1, 1}], {x, 0, 5}, {y, 0, 5}];ff = ListInterpolation[tt];Plot3D[ff[x, y], {x, 0, 6}, {y, 0, 6}] $\endgroup$
    – chris
    Commented Dec 9, 2020 at 18:11

1 Answer 1

6
$\begingroup$

Pick up the points

tt = Table[{x, y, RandomReal[{-1, 1}]}, {x, 0, 5}, {y, 0, 5}];

Interpolate

ff = Interpolation[Flatten[tt, 1], Method -> "Spline"];

Plot

Plot3D[ff[x, y], {x, 0, 5}, {y, 0, 5}]

enter image description here

More points

tt = Table[{x, y, RandomReal[{-1, 1}]}, {x, 0, 5, 0.25}, {y, 0, 5, 
   0.25}];    ff = Interpolation[Flatten[tt, 1], Method -> "Spline"];
Plot3D[ff[x, y], {x, 0, 5}, {y, 0, 5}, PlotPoints -> 50, Mesh -> None];

enter image description here

So if you want to wrap it up

Randfun:= 
 Module[{tt = 
    Table[{x, y, RandomReal[{-1, 1}]}, {x, 0, 5, 1}, {y, 0, 5, 1}]},
  Interpolation[Flatten[tt, 1], Method -> "Spline"]
  ]

Then

Table[Plot3D[Randfun[x, y] // Evaluate, {x, 0, 5}, {y, 0, 5}, 
  PlotPoints -> 50, PlotRange -> All], {2}, {2}]

enter image description here

Conversely, if you want to keep the same random bumps

ff1 = Randfun[x, y];
Table[ContourPlot[ff1, {x, 0, 5}, {y, 0, 5}], {2}]

enter image description here

If you want only a few point above sea level:

tt = Table[{x, y, z = RandomReal[{-1, 1}]; If[z > 0.75, z, 0]}, {x, 0, 5, 1/2}, {y, 0, 5, 1/2}];
ff = Interpolation[Flatten[tt, 1], Method -> "Spline"];
Plot3D[ff[x, y], {x, 0, 5}, {y, 0, 5}, PlotPoints -> 50, Mesh -> None,
  PlotRange -> All]

enter image description here

$\endgroup$
5
  • $\begingroup$ I think it's a proper solution! Thanks a lot! I'll mark your answer after a while, in case someone else find something prettier. $\endgroup$
    – Cham
    Commented Dec 9, 2020 at 18:30
  • $\begingroup$ What to add, if you need the random bumps to stay the same, after a first pass? $\endgroup$
    – Cham
    Commented Dec 9, 2020 at 19:00
  • 1
    $\begingroup$ Ok, it's trivial: SeedRandom[0]. Sorry for this. But your answer is awesome! $\endgroup$
    – Cham
    Commented Dec 9, 2020 at 19:08
  • $\begingroup$ I am wondering, your function is everywhere hilly. Can one use your method to have essentially a flat plane with some random bumps here and there? $\endgroup$
    – yarchik
    Commented Dec 11, 2020 at 11:55
  • $\begingroup$ @yarchik it seems that, to my understanding, what you’re asking for is dependent upon the domain of the plane you’re displaying relative to the range chosen for the RandomReal function being used. $\endgroup$ Commented Dec 11, 2020 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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