2
$\begingroup$

I'm having troubles in defining a function of three variables: $t$, $x$, $y$, from a moving bumpy surface that should be derivable relative to its three variables. Here's the animated surface code, which works very well in the Manipulate box:

Clear["Global`*"] 

size = 30;
Z0[x_, y_] := RandomReal[{-1, 1}]
amplitude[x_, y_] := RandomReal[{0.5, 1.5}]
frequency[x_, y_] := RandomReal[{0.25, 0.75}]
phase[x_, y_] := RandomReal[{0, 2Pi}]

(*Collection of random oscillators: *)

oscillators[t_] = Table[{x, y, Z0[x, y] + amplitude[x, y]Sin[frequency[x, y]t + phase[x, y]]}, {x, -size, size, 4}, {y, -size, size, 4}];

(* A bumpy surface fleshing the random oscillators: *)

bumpy[t_, x_, y_] := Interpolation[Flatten[oscillators[t], 1], Method -> "Spline"][x, y]

derivativeT[t_, x_, y_] := D[bumpy[t, x, y], t] (* Doesn't work!*)
derivativeX[t_, x_, y_] := D[bumpy[t, x, y], x] (* Works well! *)
derivativeY[t_, x_, y_] := D[bumpy[t, x, y], y] (* Works well! *)

Manipulate[
  Plot3D[
    Evaluate[bumpy[t, x, y]],
    {x, -10, 10}, {y, -10, 10},
    PlotPoints -> ControlActive[20, 60],
    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 -> 500
 ],
  {t, 0, 40, 0.01, 
  ImageSize -> Large,
  Appearance -> {"Labeled", "Closed"}, 
  AppearanceElements -> {"InputField", "Slider"}
  }, 
 ControlPlacement -> Bottom,
 FrameMargins -> None, 
 FrameLabel -> {None, None, 
   Style["Some Title Here!", Bold, 14, FontFamily -> "Helvetica"]}
   ]

Preview of what this code is doing (the animation is very smooth and pretty cool!): enter image description here

How can I turn bumpy[t_, x_, y_] into a function that could be derivable relative to its time $t$ parameter, and be plotted?

I'm able to plot the derivatives relative to the two other variables, $x$ and $y$:

derivativeX[t_, x_, y_] := D[bumpy[t, x, y], x]
derivativeY[t_, x_, y_] := D[bumpy[t, x, y], y]

But currently the following doesn't work:

derivativeT[t_, x_, y_] := D[bumpy[t, x, y], t]

In this case, I get the following error message:

General::ivar : 0 is not a valid variable.

Note: The solution should work with Mathematica 7, since I'm currently stuck with this version for some time, because of an old computer...

This question appears to be similar to this one (without any answers): How to derive a 3D interpolated surface?


EDIT: Here's a simple ListPointPlot3D to show the oscillators that define the "bones" of the bumpy surface:

Manipulate[
 ListPointPlot3D[
  Flatten[oscillators[t], 1],
    PlotRange -> {{-10, 10}, {-10, 10}, {-3, 3}},
    Axes -> True,
    AxesOrigin -> {0, 0},
    AxesStyle -> Directive[GrayLevel[0.5]],
    AxesLabel -> {Style["X",  Bold, 14],Style["Y",  Bold, 14]},
    ImageSize -> 700
  ],
 {t, 0, 40, 0.1, 
    ImageSize -> Large,
    Appearance -> {"Labeled", "Closed"}, 
    AppearanceElements -> {"InputField", "Slider"}
    }, 
  ControlPlacement -> Bottom,
  FrameMargins -> None, 
  FrameLabel -> {None, None, 
      Style["Filed of random oscillators", Bold, 14, 
    FontFamily -> "Helvetica"]}
    ]
$\endgroup$
7
  • 1
    $\begingroup$ rB[t_, x_, y_] := Interpolation[Flatten[randomPoints[t], 1], Method -> "Spline"][x, y]? $\endgroup$ – kglr Dec 14 '20 at 21:24
  • $\begingroup$ @kglr, this is what I tried, but I'm getting a warning message from Mathematica: Interpolation::maple: The Spline method could not be used because the data could not be coerced to machine real numbers. And the output is then all wrong... $\endgroup$ – Cham Dec 14 '20 at 21:27
  • $\begingroup$ It works in version 11.3.0 (windows 10 64b) and version 12.2.0 (wolfram cloud) with no warning message and the picture looks same as the picture from Evaluate[randomBumps[t][x, y]]. $\endgroup$ – kglr Dec 14 '20 at 21:46
  • $\begingroup$ I can confirm using kglrs code in v12 on OSX, it runs smoothly.... and can be plotted...not to say you need to upgrade...but version 12 does have nice features you might want to take advantage of since verison 7.... $\endgroup$ – morbo Dec 14 '20 at 22:30
  • 1
    $\begingroup$ Maybe try it on the free version of the Wolfram Cloud? That uses version 12. $\endgroup$ – Daniel Lichtblau Dec 15 '20 at 16:30
2
$\begingroup$

We need some small modification of the code to plot derivatives

size = 30;
Z0[x_, y_] := RandomReal[{-1, 1}];
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, 
    Z0[x, y] + 
     amplitude[x, y] Sin[frequency[x, y] t + phase[x, y]]}, {x, -size,
     size, 4}, {y, -size, size, 4}];
randomPoints1[t_] := 
  Table[{x, y, 
    Z0[x, y] + 
     amplitude[x, y] frequency[x, y] Cos[
       frequency[x, y] t + phase[x, y]]}, {x, -size, size, 
    4}, {y, -size, size, 4}];

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

Now we can define derivatives as follows

derivativeT[t_, x_, z_] := randomBumps1[t][x, z]; 
derivativeX[t_, x_, z_] := D[randomBumps[t][x, z], x];
derivativeY[t_, x_, z_] := D[randomBumps[t][x, z], z]; 

Visualization of derivativeT[]

Manipulate[
 Plot3D[Evaluate[
   derivativeT[t, x, 
    y]],{x, -10, 10}, {y, -10, 10}, 
  PlotPoints -> ControlActive[20, 60], 
  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, 40, 0.01, 
  ImageSize -> Large, Appearance -> {"Labeled", "Closed"}, 
  AppearanceElements -> {"InputField", "Slider"}}, 
 ControlPlacement -> Bottom, FrameMargins -> None, 
 FrameLabel -> {None, None, 
   Style["Some Title Here!", Bold, 14, FontFamily -> "Helvetica"]}]

Figure 1

$\endgroup$
17
  • $\begingroup$ randomPoints[t_] := and randomPoints1[t_] := shouldn't have a :=. The motion is continuous in Manipulate only with =, instead of :=. $\endgroup$ – Cham Dec 15 '20 at 0:09
  • $\begingroup$ It appears to work, but at the price of telling explicitly the time derivative. I'll have to study this solution, to see if it works as initial conditions in my code. I suspect that it will complicate my full code... $\endgroup$ – Cham Dec 15 '20 at 0:14
  • $\begingroup$ Oh, and in randomPoints1, there shouldn't be Z0[x, y], since it's supposed to be the time derivative of randomPoints. $\endgroup$ – Cham Dec 15 '20 at 0:19
  • $\begingroup$ @Cham I don't understand you remarks, since I only seen your code and try to make it working with version 7. Actually the code did smooth picture for derivatives. But it looks like it is not what do you expect? $\endgroup$ – Alex Trounev Dec 15 '20 at 0:35
  • $\begingroup$ The problem with your solution is that it defines another function. Please, see the new formulation of the question. I need an unique function bumpy[t, x, y] to get all three derivatives (relative to variables $t$, $x$, $y$) $\endgroup$ – Cham Dec 16 '20 at 23:35

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.