为什么matlab给出fminsearch优化错误



我有这样一个问题,我会在matlab中做。但是,我遇到了一些错误:

求x∈[0,1]的值,该值最小化矩阵A(x(=xM+(1−x(p的最大特征值,其中M是5×5幻方,p是5×5Pascal矩阵。

我的matlab代码:

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

我得到了这个错误;

使用fmincon时出错(第221行(FMINCON要求以下输入为双数据类型:"X0"。

或者如果我使用另一个功能:fminsearch

x=fminsearch(目标,约束,x0(在这种情况下,我得到以下错误:

使用fminsearch时出错(第96行(FMINSEARCH只接受数据类型为double的输入。

我该如何处理这些错误?我的错误在哪里?我该如何纠正它们?

我想你想要的可能是fminbnd,它有助于

在固定区间上寻找单变量函数的最小值

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

使得

>> x
x =  0.79603

我怀疑您没有向我们显示正确的代码,因为您有sums。我怀疑你指的是syms

fmincon仅适用于数字数据,不适用于符号数据。

最新更新