matlabfminsearch

Why matlab gives fminsearch optimisation error?


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?


Solution

  • 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