目标:minimize the norm of a matrix.
计算工具:matlab
特点:作者以矩阵的形式对 argument x 添加约束
即 x是一个9x9 的矩阵且满足每行的和为1
fun = @(x) norm(A*x - B);
rng(4)
x0 = rand(9,9);
lb = zeros(1,81);
ub = ones(1,81)*0.999999;
A = [];
b = [];
Aeq = ones(9,1);
beq = ones(9,1);
nonlcon = [];
options = optimoptions(@fmincon,'Algorithm','sqp','MaxFunctionEvaluations',5000)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon, options);
目标函数中的A 和 B是 15x9 的矩阵
在运行之后出现:Aeq must have 81 column(s)的提示
纠正:
rng(0);
A = rand(9);
B = rand(9);
fun = @(x) norm(A*reshape(x,9,9) - B);
rng(4)
x0 = rand(1,81);
lb = zeros(1,81);
ub = ones(1,81)*0.999999;
A = [];
b = [];
Aeq = repelem(eye(9),1,9);
beq = ones(9,1);
nonlcon = [];
options = optimoptions(@fmincon,'Algorithm','sqp','MaxFunctionEvaluations',5000);
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon, options);
x = reshape(x, 9, 9);
注意:matlab可以用矩阵表示标量 倒是最终还是要以列响亮的形式进行存储。
参考链接