3
$\begingroup$

I need to define a function h by the Hessian matrix:

f[x_, y_] := x^3 + y^2
h[x_, y_] := D[f[x, y], {{x, y}, 2}]

and evaluating it on a point, e.g. (0,0). But I obtained the following error:

General: 0 is not a valid variable.

Someone knows what should I do?

$\endgroup$
2
  • 5
    $\begingroup$ This is one of the rare occasions where I would suggest to use Set instead of SetDelayed. Try h[x_, y_] = D[f[x, y], {{x, y}, 2}]. It should work provided that both x and y have no values assigned to them. $\endgroup$ Feb 23 '19 at 15:00
  • 1
    $\begingroup$ Variant of @HenrikSchumacher suggestion is` h[x_, y_] := Evaluate @ D[f[x, y], {{x, y}, 2}]` $\endgroup$
    – m_goldberg
    Feb 23 '19 at 16:49
3
$\begingroup$
f[x_, y_] := x^3 + y^2

h[x1_, y1_] := D[f[x, y], {{x, y}, 2}] /. {x -> x1, y -> y1}

h[0, 0]

{{0, 0}, {0, 2}}

$\endgroup$
2
  • 6
    $\begingroup$ Using h[x1_, y1_] = Block[{x, y}, D[f[x, y], {{x, y}, 2}] /. {x -> x1, y -> y1}] provides robustness towards potential values for x and y as well as performance (since the symbolical computation for the derivatives has to be performed only once). $\endgroup$ Feb 23 '19 at 15:10
  • $\begingroup$ @HenrikSchumacher Thx I got it now $\endgroup$
    – zhk
    Feb 24 '19 at 3:32

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.