# Display level sets of gradient-magnitude by Meshfunctions

## EDIT 2

Question refined to: How can I use this information to impose Meshlines which correspond to level sets of this graddata?

## Original

Suppose we have a data array of the form

{ {x1,y1,f(x1,y1)}, {x2,y2,f(x2,y2)}, ... , {xn,yn,f(xn,yn)} }


but we do not know the analytic form of the function f(x,y).

Here is a set of example data

Execution of that paste sets variable data. Then we make a ListPlot3D as

ListPlot3D[data, MeshFunctions -> {Sqrt[#1^2 + #2^2] &, #3 &},PlotStyle ->
Automatic, MeshStyle -> {Blue, Green}, Mesh -> 21,
ImageSize -> 600]


To obtain

In this way, we were able to apply different Meshfunctions to the ListPlot3D.

For example, #3 & makes mesh lines which are level sets of the 3rd coordinate, the height.

The other Meshfunction I used simply to illustrate that more sophisticated custom meshfunctions are possible.

However, this is the meshfunction I'd like to obtain: I want meshlines which represent level sets of the norm of the gradient vector of the surface.

Is there a way to achieve this? I only need for this to be approximate, clearly, as the data is discrete and not continuous. One way I could imagine to solve this might be to apply some complex array operations to the data set to obtain approximate slope magnitudes at every point, and apply some function to that to obtain the desired lines, but I don't know how to do that. Maybe there is a better way.

## ============EDIT============

Suppose I build a new dataset

graddata={ {x1,y1,g(x1,y1)}, {x2,y2,g(x2,y2)}, ... , {xn,yn,g(xn,yn)} }


where g(x,y) is the norm of the gradient vector of the surface at (x,y). I believe I can achieve this.

Given this new graddata, how can I implement this information into the ListPlot3D in order to obtain the desired levelsets?

For example, I could ListContourPlot[graddata] to obtain the level sets of the gradient-norm field, but how could I use implement to use this information to make such levelsets appear as Meshlines on the ListPlot3D of data?

## ============EDIT 2============

I have done as I described above an obtained the array of gradient-norm data: graddata which is here:

This array has the form described in my first edit.

How can I use this information to impose Meshlines which correspond to level sets of this graddata?

• I would estimate the magnitude of the derivative at each point $(x,y)$ by computing $\sqrt{(\Delta f/ \Delta x)^2 + (\Delta f/ \Delta y)^2}$, for neighboring sampled points in $x$ and $y$. – David G. Stork Feb 4 '17 at 2:53
• Yeah, this is pretty much the idea I had as well, but I was hoping there might be an easier way. Currently, the data points were created by MeshRegion etc., so the sampled points are not arranged nicely on a rectilinear grid but rather somewhat "randomly" based on what MeshCellCoordinates it chooses for the Region. I'll probably have to abandon this in favor of a predictable grid, though. – Steve Feb 4 '17 at 21:01
• Moreover, suppose I build a new dataset data2 which contains the slope values, how can I apply contours of level sets of that value? – Steve Feb 4 '17 at 21:03

Construct an interpolation function for graddata:

iF = Interpolation[graddata[[1]]]


Use this interpolating function as a mesh function:

ListPlot3D[data[[1]],
MeshFunctions -> {Function[{x, y, z}, iF[x, y]]}, Mesh -> {5},
MeshStyle -> Directive[Red, Thick]]


Note: posted data files contain one extra layer of {}s. Above we use data[[1]] and graddata[[1]] to get the appropriate lists of 3D coordinates.

Alternatively, work with data directly and construct a function for the norm of the gradient using a function that interpolates data:

intF = Interpolation[data[[1]]];
ngF[x_, y_] := Norm[Grad[intF[s, t], {s, t}]] /. {s -> x, t -> y};

ListPlot3D[data[[1]],
MeshFunctions -> {Function[{x, y, z}, Evaluate[ngF[x, y]]]},
Mesh -> {5}, MeshStyle -> Directive[Red, Thick]]


• I always get this kind of error with Interpolation i.imgur.com/QcW3LbI.png – Steve Feb 6 '17 at 19:56
• @Steve, i am using version 9 (windows 10) and i don;t get any error messages. – kglr Feb 6 '17 at 20:04
• Hmm. I'm in version 11.0 (windows 7). I always seem to have these problems with Interpolation solutions and I don't understand why. – Steve Feb 6 '17 at 20:28
• @Steve - I had the same problem as you, but I found a trick on this page to help. You can't get around the fact that your x,y gridpoints are not on a rectangular array, and therefore the interpolation isn't going to be as good as it would otherwise. Then, since you get that error, you can tweak the gridpoints just a little bit to get away from the error message. Here is the code I got to work. – Jason B. Mar 8 '17 at 21:35