MATLAB Answers

I'm getting an error related to fmincon

6 ビュー (過去 30 日間)
Zeynep  Toprak
Zeynep Toprak 2020 年 3 月 29 日
コメント済み: Ameer Hamza 2020 年 3 月 29 日
Question is this;
My matlab code is as follows:
%Define Matrices
>> M = magic(5);
>> P = pascal (5);
% Define the variable x
>> syms x
%Define the given matrix A
>> A = x*M + (1-x)*P;
%Define the eigenvalue lambda as y;
>> syms y
%Find determinant of |A - lambda * I|
>> D = det (A - y*eye(5))
%Define Objective function
objective = @(y) y
%And Define the constraint
constraint = @(x,y) (-1)*D
%initial value x0 = (0:0.001:1);
%Minimization problem solving
x = fmincon(objective, constraint, x0)
I get this error;
Error using fmincon (line 221)
FMINCON requires the following inputs to be of data type double: 'X0'.
If I use another function: fminsearch
x = fminsearch(objective, constraint, x0)
In this case I get the following error:
Error using fminsearch (line 96)
FMINSEARCH accepts inputs only of data type double.
How can I deal with these errors ? Where is my mistake? How can I correct them?

  3 件のコメント

Torsten
Torsten 2020 年 3 月 29 日
What do mean by "largest eigenvalue" ? The largest real-valued eigenvalue or the eigenvalue largest in magnitude ?
Zeynep  Toprak
Zeynep Toprak 2020 年 3 月 29 日
I guess you asked behind its math. Okay, I can explain as follows:
Zeynep  Toprak
Zeynep Toprak 2020 年 3 月 29 日
Is this explanation and solution enough?

Sign in to comment.

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 29 日
This is an example with fmincon
M = magic(5);
P = pascal (5);
f = @(x) max(abs(eig(x*M + (1-x)*P)));
x_sol = fmincon(f, rand, [], [], [], [], 0, 1);

  9 件のコメント

表示 6 件の古いコメント
Zeynep  Toprak
Zeynep Toprak 2020 年 3 月 29 日
This is correct when I compare it with what I solve by other ways.
Zeynep  Toprak
Zeynep Toprak 2020 年 3 月 29 日
Many thanks again :)
Ameer Hamza
Ameer Hamza 2020 年 3 月 29 日
Glad to be of help.

Sign in to comment.

その他の回答 (1 件)

Torsten
Torsten 2020 年 3 月 29 日
編集済み: Torsten 2020 年 3 月 29 日
If you mean the eigenvalue largest in magnitude, this should give you a start.
Incorporating fmincon can automatize the search for an optimal x.
M=magic(5);
P=pascal(5);
A=@(x) x*M+(1-x)*P;
x=0:0.02:1;
for i=1:numel(x)
d(i) = abs( eigs(A(x(i)),1) );
end
[dmin,idd] = min(d);
x(idd) %show x for which eigenvalue largest in magnitude is minimum

  4 件のコメント

表示 1 件の古いコメント
Zeynep  Toprak
Zeynep Toprak 2020 年 3 月 29 日
Torsten
Torsten 2020 年 3 月 29 日
The error was corrected in the meantime.
Zeynep  Toprak
Zeynep Toprak 2020 年 3 月 29 日
yeah, it works!! Many many thankss!! Take care of yourself! :))

Sign in to comment.


Translated by