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

Rik
Rik 2023년 11월 17일
Can you edit your question to provide example variables that will reproduce the error? You can use the run button to run the code in the editor.
Hi, I was trying to reproduce this error and I used the following code below.
% Set the dimensions
T = 5;
N = 4;
% Generate a random value for sigma2
sigma2 = rand;
% Generate a random NxN PSD matrix for R
R = rand(N, N);
R = R'*R; % This ensures that R is positive semi-definite
prob=optimproblem("Description","MSE min");
X=optimvar("X",T,N,"Type",'continuous')
X =
5×4 OptimizationVariable array with properties: Array-wide properties: Name: 'X' Type: 'continuous' IndexNames: {{} {}} Elementwise properties: LowerBound: [5×4 double] UpperBound: [5×4 double] See variables with show. See bounds with showbounds.
prob.Objective=trace(R - R*X'*inv(X*R*X' + sigma2*eye(T))*X*R);
Error using inv
Invalid data type. Input matrix must be double or single.
In your case, the expression X*R*X' + sigma2*eye(T) is not a double or single matrix, it's an optimization expression, because "X" is an optimization variable. The inv function cannot handle optimization expressions, hence the error. You might need to reconsider the formulation of your problem without the use of 'inv'
Sai Srikar
Sai Srikar 2023년 11월 17일
편집: Sai Srikar 2023년 11월 17일
Hi @Rik, Brahmadev has provided the code with some example variables
Hi @Brahmadev, Can you suggest some way which helps me get rid of this error? My objective function and its optimal solution were discussed in an IEEE paper. I am trying to reproduce the same result using MATLAB's optimization toolbox.
Walter Roberson
Walter Roberson 2023년 11월 17일
Do not use inv() here. Use the \ operator
Sai Srikar
Sai Srikar 2023년 11월 17일
Hi, I tried \ operator like: prob.Objective = trace(R - R*X'*(X*R*X' + sigma2*eye(T))/(X*R)). I still get an error as follows:
Error using /
Division of an OptimizationVariable by nonscalar not supported.

댓글을 달려면 로그인하십시오.

 채택된 답변

Bruno Luong
Bruno Luong 2023년 11월 17일

0 개 추천

댓글 수: 1

Sai Srikar
Sai Srikar 2023년 11월 17일
편집: Sai Srikar 2023년 11월 17일
Thanks Bruno, I am able to get rid of the error.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Himanshu
Himanshu 2023년 11월 17일

0 개 추천

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:

댓글 수: 1

Sai Srikar
Sai Srikar 2023년 11월 17일
Thanks Himanshu. Is there any way to address this issue in MATLAB?, I do know the analytical solution for this optimization problem, I wanted to reproduce the same using MATALAB's optimization toolbox for my work.

댓글을 달려면 로그인하십시오.

Pratyush
Pratyush 2023년 11월 17일

0 개 추천

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);

댓글 수: 1

Sai Srikar
Sai Srikar 2023년 11월 17일
Thanks Pratyush, I tried this. I get a new error: "Error using double
Conversion to double from optim.problemdef.OptimizationExpression is not possible."

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Linear Programming and Mixed-Integer Linear Programming에 대해 자세히 알아보기

제품

릴리스

R2023a

질문:

2023년 11월 17일

편집:

2023년 11월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by