fmincon constraints error: Aeq must have 81 column(s)

目标: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可以用矩阵表示标量 倒是最终还是要以列响亮的形式进行存储。
参考链接

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值