4
$\begingroup$

I cannot find a way to obtain the partial derivatives of a 2D Interpolating Function obtained with ListInterpolation. I didn't find anything useful on the web. Any clue?

$\endgroup$
0

3 Answers 3

9
$\begingroup$

Another form I have found useful is:

f = ListInterpolation[
  Table[Sin[x y], {x, 0, 1, .25}, {y, 0, 2, .25}], {{0, 1}, {0, 2}}]

Then one can make derivative functions that can be treated as normal function via

dfdx[x_, y_] := Evaluate[D[f[x, y], {x, 1}]]
dfdy[x_, y_] := Evaluate[D[f[x, y], {y, 1}]]

and the second derivatives

d2fdx[x_, y_] := Evaluate[D[f[x, y], {x, 2}]]
d2fdy[x_, y_] := Evaluate[D[f[x, y], {y, 2}]]

the partial with respect to x and then y

dfdydx[x_, y_] := Evaluate[D[f[x, y], x, y]]

or as pointed out by J.M. a more compact and very readable form:

dfdx = Derivative[1, 0][f]
dfdy = Derivative[0, 1][f]
dfdydx = Derivative[1, 1][f]
$\endgroup$
2
  • 2
    $\begingroup$ More compact: dfdx = Derivative[1, 0][f]; dfdy = Derivative[0, 1][f]; dfdydx = Derivative[1, 1][f];, and so forth. $\endgroup$ Jun 29, 2016 at 17:23
  • 1
    $\begingroup$ @J.M. Yes, this form is very readable and compact. I added it. $\endgroup$ Jun 29, 2016 at 17:35
3
$\begingroup$

Using an example from the documentation of ListInterpolation:

f = ListInterpolation[
  Table[Sin[x y], {x, 0, 1, .25}, {y, 0, 2, .25}], {{0, 1}, {0, 2}}]
dfdx[u_, v_] := D[f[x, y], x] /. {x -> u, y -> v}
dfdy[u_, v_] := D[f[x, y], y] /. {x -> u, y -> v}
Manipulate[
 Show[{Plot3D[f[x, y], {x, 0, 1}, {y, 0, 2}], 
   Graphics3D[{Red, PointSize[0.03], 
     Table[Point[{x, y, Sin[x y]}], {x, 0, 1, .25}, {y, 0, 2, .25}], 
     Blue, Thick, 
     Arrow[{{p, q, f[p, q]}, {p, q, f[p, q]} - 
        Normalize[{dfdx[p, q], dfdy[p, q], -1}]}]}]}, 
  BoxRatios -> Automatic, 
  PlotRange -> {{-1, 2}, {-1, 2}, {0, 2}}], {p, 0, 1}, {q, 0, 2}]

enter image description here

$\endgroup$
2
$\begingroup$

Please make your question clear.

But I think you're simply using f'[x,y] and hope that you can get a result?

Try the following code:

f = ListInterpolation[
  Table[Sin[x y], {x, 0, 1, .25}, {y, 0, 2, .25}], {{0, 1}, {0, 2}}]

D[f[x, y], x]

Plot3D[Evaluate@D[f[x, y], x], {x, 0, 1}, {y, 0, 2}]

for higher order:

D[f[x,y],{x,2}]

Will this code help?

Also, you may want a derivatives on a certain direction:

Ddir[f_, {x0_, y0_}] := Normalize[{x0, y0}].{f[x, y]~D~x, f[x, y]~D~y}

Plot3D[Evaluate@Ddir[f, {1, 1}], {x, 0, 1}, {y, 0, 2}]

I hate these simple copy work of documentation. Please check the documentation up next time you ask.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.