4
$\begingroup$

I have a data set of the type $\{x,y,z\}$ where $(x,y)$ is a point and $z$ is the "value" or magnitude at that point. This gives me a triangular sort of shape since $(x,y,z)$ is not defined along the whole possibility set considering it is real data. Is there any way I can get a gradient using the points I have (using them if possible) and using the interpolation from Mathematica otherwise?

$\endgroup$
5
$\begingroup$

I'll try to give you an answer complementary to Szabolcs’, because I understood your question in another way. If by “get a gradient using the points I have” you mean having a continuous color gradient in your plot of the data (and not calculating a gradient, as in “derivative”), then you can simply use DensityPlot with an Interpolation.

Let's get some data, taken at random points from the function $z=\sin x\ \sin y$:

points = RandomReal[{-5, 5}, {500, 2}];
data = {#1, #2, Sin[#1]*Sin[#2]} & @@@ points;

then plot it:

DensityPlot[
 Interpolation[data, InterpolationOrder -> 1][x, y], {x, -5, 
  5}, {y, -5, 5}, PlotRange -> {Full, Full, {-1, 1}}]

enter image description here

which you can compare to the original function I drew points from:

DensityPlot[Sin[x]*Sin[y], {x, -5, 5}, {y, -5, 5}]

enter image description here

$\endgroup$
5
$\begingroup$

Your question is not very clearly formulated, but if I understand it correctly:

  • you have the value of a function in a set of points on the plane

  • the points do not form a rectangular lattice

  • you need to estimate the gradient of the function

You can do this by interpolating linearly, then taking the gradient of the interpolated function.

Example:

Let's generate some data:

points = RandomReal[{-2, 2}, {20, 2}];
data = {points, Exp[-#.# & /@ points]}\[Transpose];

Let's interpolate:

if = Interpolation[data, InterpolationOrder -> 1]

You can get the gradient by

D[if[x, y], {{x, y}}]

The interpolated function looks like this:

ListPlot3D[Flatten /@ data, Mesh -> All]

Mathematica graphics

(Note: here I used ListPlot3D, which does the interpolation internally. It uses the same method as Interpolation. I did this because it made it easy to only plot the function inside the convex hull of the points, and not extrapolate outside of that.)

$\endgroup$

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.