I would like to understand, how to obtain gradients of the PDE solution obtained with NDSolve. To be precise let us consider a Laplace equation from one of the examples:

    Clear[x, y, f];
emesh = ToElementMesh[Disk[]];

f = NDSolveValue[{Derivative[0, 2][u][x, y] + 
Derivative[2, 0][u][x, y] == 0, 
   DirichletCondition[u[x, y] == Sin[x y], True]}, 
  u, {x, y} ∈ emesh]

This returns the interpolation function which one can plot and integrate:

    NIntegrate[f[x, y], {x, y} ∈ emesh]

(*  1.52794*10^-8  *)

Plot3D[f[x, y], {x, y} ∈ emesh]

enter image description here

This, however, does not work:

  g[x_, y_] := D[f[x, y], x];
Plot3D[g[x, y], {x, y} ∈ emesh]

Since Integrate does not work on this result, only NIntegratedoes, the problem is probably that one needs to apply a numeric derivative. What and how?

  • $\begingroup$ Duplicate: 1301. Also related: 48383, 50791, 69188. $\endgroup$ – Michael E2 Jun 8 '15 at 15:36
  • $\begingroup$ @MichaelE2 but this is completely different: its on a mesh :-) $\endgroup$ – chris Jun 8 '15 at 16:05
  • $\begingroup$ @chris I'm a little slow to understand the ":-)" -- I thought you would object that it's a two-variable function (instead of single-variable). :-) $\endgroup$ – Michael E2 Jun 8 '15 at 16:14
  • $\begingroup$ @Michael E2 Thank you for the most useful references $\endgroup$ – Alexei Boulbitch Jun 10 '15 at 7:29

That's an easy one:

g[x_, y_] = D[f[x, y], x];
Plot3D[g[x, y], {x, y} \[Element] emesh]

Mathematica graphics

Note the : in the definition of g :-)

  • $\begingroup$ You beat me to it by a few seconds :-) $\endgroup$ – user21 Jun 8 '15 at 15:20
  • $\begingroup$ @user21 I think I learnt it from one of your posts! $\endgroup$ – chris Jun 8 '15 at 15:21
  • 1
    $\begingroup$ If one wanted it to be SetDelayed for some reason one could also do g[x_, y_] := Evaluate[D[f[x, y], x]]; $\endgroup$ – user21 Jun 8 '15 at 15:23
  • $\begingroup$ @user21 feel free to answer this question instead mathematica.stackexchange.com/q/84726/1089 :-) it is more in your league ! $\endgroup$ – chris Jun 8 '15 at 15:23
  • 2
    $\begingroup$ One can also use g = Derivative[1, 0][f]. $\endgroup$ – Michael E2 Jun 8 '15 at 16:09

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.