0

This question arises from a matlab community add-on function "general simulated annealing algorithm". The function needs a 'loss function' which is required to be minimized. The loss function is a function handle, for example the camel function camel = @(x,y)(4-2.1*x.^2+x.^4/3).*x.^2+x.*y+4*(y.^2-1).*y.^2, then the loss function is expressed as loss = @(p)camel(p(1),p(2)).

Now, what I want to minimize is a matrix eigenvalue function, say goal=@(x,y)sum(abs(sort(real(eig([x,y;y,x])))-sort(real(eig(A))))) where A is a given matrix. But generally the matrix [x,y;y,x] in the goal is replaced by a much larger matrix H. For example, there 30 independent variables forming a vector x, and I have an algorithm to construct a 16x16 matrix using these variables, say for i=1:16;H(2*i-1:2*i,2*i-1:2*i)=[x(1),x(2);x(3),x(4)];end.

So it's quite inconvenient to directly write it down and insert it into the eqution. What I expect is a way to retain the construction of H in the function handle, so maybe I need something like nested function?

user835469
  • 11
  • 2
  • 1
    You mean `goal=@(x,y)sum(abs(sort(real(eig([x,y;y,x])))-sort(real(eig(A)))))`;? Why not just write that? – Ander Biguri Sep 27 '22 at 14:49
  • 1
    (1) `eig()` returns the eigenvalues already sorted, no need for `sort`. (2) Are you saying you want to minimize a symbolic function? Please [edit] your post to clarify. – Cris Luengo Sep 27 '22 at 15:03
  • @AnderBiguri Because I will need [x,y;y,x] to be a bigger matrix, say like 40x40, to meet my need,and there will be more symbolic variables in the matrix, and it's hard to just write the whole matrix directly into the equation. – user835469 Sep 27 '22 at 15:48
  • @user835469 note that simulated annealing is a numerical method, i.e. can't solve symbolic equations. – Ander Biguri Sep 27 '22 at 15:50
  • @CrisLuengo I see. I've edited a bit to make it clearer. – user835469 Sep 27 '22 at 15:57
  • @AnderBiguri Alright, but I've tried solving the 'goal' and it worked. My point here is that the matrix I want to put in the equation is not [x,y;y,x] but a much larger one with more variables thus hard to write it down, while the restriction, or the formation of the matrix is a key feature I want to retain. – user835469 Sep 27 '22 at 16:03
  • @user835469 can you gives us a description/example of the actual problem? – Ander Biguri Sep 27 '22 at 16:49
  • You still talk about a "symbolic matrix". And I still don't know what you mean by that. Do you really want to find a solution using symbolic math (rather than numeric)? Or did you mean to say your function is parameterized by a matrix instead of many individual variables? – Cris Luengo Sep 27 '22 at 16:54
  • In the latter case, and since your optimization function should have a single vector input, what you need to do is find a way to obtain the eigenvalues of your matrix given the unique matrix elements in a vector. You could do this once, using symbolic math, and converting the result into a numeric function, or you could just build the matrix H from the input vector p, and then compute eig. In no case does this involve having each parameter as a separate variable. – Cris Luengo Sep 27 '22 at 16:57
  • For example, in your 2x2 matrix case, the eigenvalues are `x + y` and `x - y`, which I obtained by declaring `x` and `y` as symbolic variables, then computing `eig([x,y;y,x])`. So you can say `eig_p = @(p)[p(1)+p(2),p(1)-p(2)]`, and `loss = @(p)sum(abs(sort(real(eig_p(p)))-sort(real(eig(A)))))`. This might be more elaborate for larger matrices, but it's not hard to do. – Cris Luengo Sep 27 '22 at 17:02
  • @CrisLuengo I don't quite understand the example you gave me, but I believe your 'latter case' is my case. What I considered is to first build my matrix in a function, say 'f', then call the function 'f' in the function handle 'goal', but I don't know if there's a way to achieve this. – user835469 Sep 28 '22 at 05:25
  • `it's hard to just write the whole matrix directly into the equation.` - is it clear that the anonymous function handle `@` syntax is just for convenience with short inline functions? You can write an entire standalone `.m` file containing your function and pass this in as your function handle instead if that makes the inputs clearer? – Wolfie Sep 28 '22 at 09:19

0 Answers0