Matrix inverse in objective function
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi,
I am trying to find the matrix X, which minimizes the objective function. However, I see the following error:
Error using inv
Invalid data type. Input matrix must be double or single.
Error in t2 (line 11)
prob.Objective=trace(R - R*X'*inv(X*R*X' + sigma2*eye(T))*X*R);
My code snippet: (Here, T, N and sigma2 are constants and R is an NxN PSD matrix)
prob=optimproblem("Description","MSE min")
X=optimvar("X",T,N,"Type",'continuous');
prob.Objective=trace(R - R*X'*inv(X*R*X' + sigma2*eye(T))*X*R);
댓글 수: 5
채택된 답변
Bruno Luong
2023년 11월 17일
The inv() is not supported by optimization expression see https://www.mathworks.com/help/optim/ug/supported-operations-on-optimization-variables-expressions.html
Workaround using https://www.mathworks.com/help/optim/ug/fcn2optimexpr.html
추가 답변 (2개)
Himanshu
2023년 11월 17일
Hey,
I understand that you are an encountering an error while running the given code snippet.
The error message indicates that the error occurs because the ‘inv’ function is being called with inputs of invalid datatype. The ‘inv’ function only accepts inputs of type double or single. In the given code, ‘inv’ function is trying to operate on an expression involving an optimization variable (X), which is not of type double or single.
Please refer to the following documentation page for more information on the ‘inv’ function:
Pratyush
2023년 11월 17일
Hi Sai,
I understand that you are getting an error using the "inv" function.
The error you receive is because the "inv" function accepts matrix of data type single or double as argument, but it seems your "X*R*X' + sigma2*eye(T)" expression is not of the correct type.
To fix this issue, you can explicitly convert the matrix to the desired type using the "double" function. Try modifying the last line of your script to :
prob.Objective = trace(R - R*X'*inv(double(X*R*X' + sigma2*eye(T)))*X*R);
참고 항목
카테고리
Help Center 및 File Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!