Obtain Hessian matrix from a sum of squares expression

Hi,
I have been trying to obtatin the Hessian matrix of a complex quadratic expression of 2-d variables so as to write the quadratic optimization problam I have in matrix form. However, no matter how hard I tried, I could not find a way to accomplish this.
The expression looks like
0.0010309*x(11, 7)*x(13, 9) + 0.0016748*x(15, 11)*x(13, 9) + 0.0025*x(12, 7)*x(14, 9) + 0.00049916*x(16, 11)*x(14, 9) + 9.6648e-06*x(13, 7)*x(15, 9) + 0.0024*x(14, 7)*x(16, 9) + 3.5338e-07*x(3, 12)*x(1, 10) + 0.0019432*x(4, 12)*x(2, 10) + 0.0025*x(1, 8)*x(3, 10) + 0.0017032*x(5, 12)*x(3, 10) + 3.4478e-07*x(2, 8)*x(4, 10)
but with variables indexed up to 64 in each dimension. This is stored in the problem.Objective field of an minimization problem I set up. I tried the coeff function but it needed a list of variable, which are too many to list. Any help would be greatly appreciated.
Thanks,
Yannis

 Réponse acceptée

Matt J
Matt J le 10 Mai 2023
Modifié(e) : Matt J le 10 Mai 2023
x=optimvar('x',[64,64]);
Objective=0.0010309*x(11, 7)*x(13, 9) + 0.0016748*x(15, 11)*x(13, 9) + 0.0025*x(12, 7)*x(14, 9) + 0.00049916*x(16, 11)*x(14, 9) + 9.6648e-06*x(13, 7)*x(15, 9) + 0.0024*x(14, 7)*x(16, 9) + 3.5338e-07*x(3, 12)*x(1, 10) + 0.0019432*x(4, 12)*x(2, 10) + 0.0025*x(1, 8)*x(3, 10) + 0.0017032*x(5, 12)*x(3, 10) + 3.4478e-07*x(2, 8)*x(4, 10);
prob=optimproblem('Objective',Objective);
Hessian=prob2struct(prob).H
Hessian =
(395,525) 0.0021 (655,525) 0.0033 (396,526) 0.0050 (656,526) 0.0010 (397,527) 0.0000 (398,528) 0.0048 (707,577) 0.0000 (708,578) 0.0039 (449,579) 0.0050 (709,579) 0.0034 (450,580) 0.0000

2 commentaires

Hi Matt,
this is very helpful, thanks! I will try it on the whole expression later and get back to this thread to tell the result.
With best wishes,
Yannis
Hi Matt,
thanks! This is what I needed!
With best wishes,
Yannis

Connectez-vous pour commenter.

Plus de réponses (1)

Torsten
Torsten le 10 Mai 2023
Modifié(e) : Torsten le 10 Mai 2023
Each optimizer expects the unknowns as a vector, not as a 2d-matrix.
So choose an arrangement of your unknowns in a vector of size (64^2,1). Then use MATLAB's "hessian" function to build the Hessian of your expression. Once you've created it with the symbolic toolbox, you can write it to file for use in a numerical computation. Since your expression seems to be quadratic, your Hessian will be constant, I guess.

4 commentaires

Hi Torsten,
thanks for the prompt reply!
I am, actually, seeking to minimize a sum of squares expression, that is a sum like (var(x) - sum of other var(k)'s)^2, which when expanded gives the 64^ expression part of which I showed above. I am trying to see if I can get solutions based on the matrix representation of quadratic optimization problems. I will try your suggestion to see if i can obtain the Hessian from the expression since if I increase 64 to 128 or more the system <<hangs>>, so I would like to avoid the expanted expression and work with the Hessian representation.
Thanks
With best wishes,
Yannis
Of course it's best if you can write your objective function as
min: 1/2*x'*A*x + b*x
because here, the Hessian is obviously A (assuming A is symmetric).
So I would see whether the unexpanded form of your objective as
min: (var(x) - sum of other var(k)'s)^2
does not offer such a matrix-vector representation of the objective.
Hi Torsten,
thanks again! This is exactly my issue. I believe that using Matlab to solve quadratic optimization problems works best with the matrix representation of the objective function instead of the explicit algebraic form. The 64x64 objective function takes minutes to solve. For the 128x128 problem I simply stop the computation after several minutes have passed with no result. I will try the Hesisian then and get back to this thread to tell you the result.
Thanks!
Yannis
I also thank you Torsten for this approach.
With best wishes,
Yannis

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by