发现整理SVM时还需要记录下KKT,这篇文章总结下:
(1)等式约束优化问题:
Lagrange乘数法:
解一个约束问题:
matlab代码:
% 定义目标函数和约束条件
f = x1*x2 + x1;
g1 = x1^2+x2^2-1;
% 求解最优化问题
L = f + lambda1*g1;
eqns = [diff(L, x1) == 0, diff(L, x2) == 0, g1 == 0];
vars = [x1, x2, lambda1];
sol = solve(eqns, vars);
(2)不等式约束优化问题:
解一个约束问题:
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = @unitdisk;
x0 = [0,0];
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon);
function [c,ceq] = unitdisk(x)
c = x(1)^2 + x(2)^2 - 1;
ceq = [];
end
fmincon 真方便啊