0

I have such an question, And I will do it in matlab. But, I get some errors:

Find the value of x ∈ [0, 1] that minimizes the largest eigenvalue of the matrix A(x) = xM +(1−x)P, where M is a 5×5 magic square and P is a 5 × 5 Pascal matrix.

My matlab code:

 %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'.

Or 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?

1190
  • 365
  • 2
  • 10
  • why are you using the Symbolic Math Toolbox at all? `fmincon` states that it assumes matrices to have a certain form. You don't actually define a symbolic function for this. If you actually want to solve a symbolic function use `solve` from the Symbolic Math Toolbox – max Mar 29 '20 at 15:58
  • The error messages told you exactly what the problems were. Did you read them? "FMINCON requires the following inputs to be of data type double: 'X0'." This means X0 must be a double. So to fix this error, see what data type X0 is in your code, and ensure its double. You should be able to fix these problems yourself, Matlab error messages are usually very helpful---read them! – David Mar 29 '20 at 22:09

2 Answers2

1

I suspect you did not show us the proper code, as you have sums there. I suspect you mean syms.

fmincon only works for numeric data, not symbolic data.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • Im sorry, while copying and paste here, I wrote sums. In matlab, it's correct. Well, how can I solve this question. I have no idea and knowledge except for this.. Can you help me? Thank you for your comment as well. – 1190 Mar 29 '20 at 11:58
  • 1
    @B11b check the documentation. Maybe `solve` will help. – Ander Biguri Mar 29 '20 at 12:03
1

I guess what you are looking for might be fminbnd, which helps to

Find minimum of single-variable function on fixed interval

n = 5;
M = magic(n);
P = pascal(n);
x = fminbnd(@(x) max(eig(x*M + (1-x)*P)),0,1);

such that

>> x
x =  0.79603
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81