# Find the Hessian of a function of 30 variables?

Posted 10 days ago
166 Views
|
6 Replies
|
3 Total Likes
|
 TIA!! I think main problems are defining such a function.
6 Replies
Sort By:
Posted 10 days ago
 Let's define a function for computing the Hesian of a scalar function of n variables in general. Thereafter, you can apply it to your function. hessian[f_, vars_] := Module[{n = Length[vars]}, Table[D[D[f @@ vars, vars[[i]]], vars[[j]]], {i, 1, n}, {j, 1, n}]] In the above function, the first input is the function whose hessian you want to compute. The second function is the list of variables of your function.In order to test the function, we will consider a very simple function $f(x,y) = x^2 + y^2$ . You call the function as follows hessian[#1^2 + #2^2 &, {x, y}] // MatrixForm or equivalently hessian[Function[{x,y}, x^2 + y^2], {x,y}]//MatrixForm 
Posted 9 days ago
 How would you specifically do this dozens of variables? I can see how to do it with two variables, perhaps 3 or maybe even 10. I tried hessian[Function[Table[x[[k]], {k, 1, 2}], x[[1]]^3 + x[[2]]^6], Table[x[[k]], {k, 1, 2}]] And the 2 is something I want to allow to be higher than 20. What am I doing wrong?
Posted 9 days ago
 Here is a way to generate a list of 30 variables, a random function of them, and its hessian: vars = Table[ToExpression[StringJoin["a", ToString[n]]], {n, 30}] function = Times @@ RandomChoice[vars, 8] + 3 Times @@ RandomChoice[vars, 3] hessian = D[function, {vars, 2}] 
Posted 9 days ago
 Thank you! Works perfectly!
 You need a function for creating as many variables as you need. createVar[name_, num_] := Table[Subscript[name, i], {i, 1, num}] Now, suppose that $f$ is a function of 30 variables and try this f@@createVar[x, 30]