I cannot get the partial derivative of a set of data. I managed to get the accurate interpolation, but I cannot evaluate its derivatives.

Here are my details:

data = Import["data.dat"];

The data set looks like this:


{2, 38, -0.000307893533656}, 
{2, 39, -0.000307922246488},
{2, 40, -0.000307952337061}, 
{3, 0, -0.000461750353095},
{3, 1, -0.000462600240141}, 
{3, 2, -0.000463450127186}, 
{3, 3, -0.000464294338731},
{3, 4, -0.000464294338731}

The 3D Interpolation looks good:

 pot = Interpolation[data]

enter image description here

And the Plot3D does too:

ListPlot3D[data, ColorFunction -> "SouthwestColors"]

enter image description here

There is a slight change along y (unnoticeable from Plot3D), I want to get the derivative for this function (which is along y):

Plot[ pot[50, y], {y, 0, 40} ]

enter image description here

When I use Derivative I get this, unable to even extract a value:

 pot'[50, y]

enter image description here enter image description here

Not getting a function at all...

 Plot[pot'[50, y], {y, 0, 40}]

enter image description here

What is the smart way to get the gradient of the 3D interpolated function? I also tried 'Gradient' without success:

Gradient[pot[x, y]]
VectorPlot[Gradient[pot[x, y]], {x, 0, 100}, {y, 0, 40}]

enter image description here

What must I do?


You can use Derivative[0, 1][int] to get the first derivative of int with respect to your second variable $y$.

To illustrate, let's create some data and interpolate:

data = Table[{x, y, Sin[x] + 2 y}, {x, 0., 10.}, {y, 0., 10.}] ~ Flatten ~ 1;
int = Interpolation[data];

Plot3D[int[x, y], {x, 0, 10}, {y, 0, 10}]

3D plot of function

Then obtain the first derivative with respect to x vs. y respectively, and compare them with he expected values (i.e. Cos[x] and $2$, respectively):

    Derivative[1, 0][int][x, 3],
  {x, 0, 10}

plot of 1st derivative wrt x compared to cos(x)

Note that the jumps in the interpolation above are due to the relatively coarse grid on which the interpolation was carried out (i.e. with spacings of only 1 unit). This doesn't matter much here since it's just a made-up example; indeed, in a way it helps to show the two functions separately.

    Derivative[0, 1][int][Pi, y],
  {y, 0, 10}

plot of 1st derivative wrt y compared to the constant 2

  • $\begingroup$ Incredible, very illustrative example. Appreciate your time! $\endgroup$ – Joshua Salazar Jan 5 at 17:31
  • $\begingroup$ @Joshua Glad to help! $\endgroup$ – MarcoB Jan 5 at 17:34

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.