Message Boards Message Boards

GROUPS:

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

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?

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}]

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]

To clarify my question, suppose f is a function of n, where n is some number beyond the amount we want to write, 30 say, variables. The Hessian as I understanding is all possible second partial derivatives evaluated at a point with 30 coordinates in this example. It would in this case be a square matrix with 30 rows.

What I am looking for: the set of commands must accomplish the following task: define a polynomial function of 30 variables and find the Hessian of that.

I am not connecting the dots.

TIA

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract