1
$\begingroup$

I'm trying to built some random moving surface from a plane perturbed with random oscillators, and my current code isn't doing what I need yet, as seen from the Manipulate box. I'm building the surface from an answer found here :

How to insert variable height in this random bumps function?

Here's the code:

Clear["Global`*"] 

amplitude[x_, y_] := RandomReal[{0.5, 1.5}]
frequency[x_, y_] := RandomReal[{0.25, 0.75}]
phase[x_, y_] := RandomReal[{0, 2Pi}]

randomPoints[t_] := Table[{x, y, amplitude[x, y]Sin[frequency[x, y]t + phase[x, y]]}, {x, -30, 30, 5}, {y, -30, 30, 5}];

randomBumps[t_] := Interpolation[Flatten[randomPoints[t], 1], Method -> "Spline"];

Manipulate[
 Plot3D[
 Evaluate[randomBumps[t][x, y]],
  {x, -10, 10},
  {y, -10, 10},
  PlotPoints -> 30,
  PlotRange -> {{-10, 10}, {-10, 10}, {-3, 3}},
  Axes -> True,
  AxesOrigin -> {0, 0},
  AxesStyle -> Directive[GrayLevel[0.5]],
  ColorFunction -> "Rainbow",
  MeshFunctions -> {(#3&)},
  MeshStyle -> GrayLevel[0.25],
  ImageSize -> 700
  ],
 {
  t, 0, 20, 0.01,
  ImageSize -> Large,
  Appearance -> {"Labeled", "Closed"},
  AppearanceElements -> {"InputField", "Slider"}
  },
 ControlPlacement -> Bottom,
 FrameMargins -> None,
 FrameLabel ->  {None, None, 
   Style["Some Funny Title", Bold, 14, FontFamily -> "Helvetica"]}
 ]

Preview, with some of the issue shown in red:

enter image description here

When I move the time slider, the surface shown above is wildly jumping in random ways, and it's not what I'm trying to do.

Consider the surface as a set of oscillators, a single one at each location of the $x \, y$ grid. The oscillators have a fixed random frequency $f$, random amplitude $A$ and random phase $\phi$. Once the code is run, the amplitudes, frequencies and phases are randomly selected and an interpolation surface is drawn from the resulting positions at time $t = 0$. For the next time step, these numbers shouldn't change. Each point of the surface should be smoothly oscillating vertically (with their fixed amplitudes, frequencies and phases). Currently, the Manipulate box isn't showing this behavior: the numbers $A$, $f$ and $\phi$ appears to change randomly a each time step.

Or maybe it's the Spline Interpolation which creates a new surface at each time step?

How can I fix this issue?

Take note that I need the surface to be smooth and differentiable everywhere, for the three variables $x$, $y$ and $t$ (except at the edges).

$\endgroup$

1 Answer 1

3
$\begingroup$

The main bug is that you specify randomBumps[t_] := ... instead of randomBumps[t_] = ... This creates a new surface every time the time changes.

Further, you have some syntax errors. Here is a corrected version:

Clear["Global`*"]

amplitude[x_, y_] := RandomReal[{0.5, 1.5}]
frequency[x_, y_] := RandomReal[{0.25, 0.75}]
phase[x_, y_] := RandomReal[{0, 2 Pi}]

randomPoints[t_] = 
  Table[{x, y, 
    amplitude[x, y] Sin[frequency[x, y] t + phase[x, y]]}, {x, -30, 
    30, 5}, {y, -30, 30, 5}];

randomBumps[t_] = Interpolation[Flatten[randomPoints[t], 1]];

Manipulate[
 Plot3D[Evaluate[randomBumps[t][x, y]], {x, -10, 10}, {y, -10, 10}, 
  PlotPoints -> 30, PlotRange -> {{-10, 10}, {-10, 10}, {-3, 3}}, 
  Axes -> True, AxesOrigin -> {0, 0}, 
  AxesStyle -> Directive[GrayLevel[0.5]], ColorFunction -> "Rainbow", 
  MeshFunctions -> {(#3 &)}, MeshStyle -> GrayLevel[0.25], 
  ImageSize -> 700], {t, 0, 20, 0.01}, 
 AppearanceElements -> {"InputField", "Slider"}, 
 ControlPlacement -> Bottom, FrameMargins -> None, 
 FrameLabel -> {None, None, 
   Style["Some Funny Title", Bold, 14, FontFamily -> "Helvetica"]}]
$\endgroup$
4
  • $\begingroup$ What were the other syntax errors? $\endgroup$
    – Cham
    Commented Dec 14, 2020 at 14:14
  • $\begingroup$ I noticed that you removed Method -> "Spline". Why? Was this necessary to get a smooth and differentiable surface? $\endgroup$
    – Cham
    Commented Dec 14, 2020 at 14:17
  • $\begingroup$ I'm unable to make the derivative of the randomBumps function, relative to any of its three variables. $\endgroup$
    – Cham
    Commented Dec 14, 2020 at 15:10
  • 1
    $\begingroup$ I removed "Spline" because you can not do a spline interpolation with a unknown parameter t. But to precalculate the interpolation is crucial for a speedy calculation of the graphics. If you want derivatives, maybe you should reformulate the code with: randomBumps[t,x, y]. $\endgroup$ Commented Dec 14, 2020 at 16:21

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.