1. 程式人生 > >matlab二次規劃求解

matlab二次規劃求解

clear all,close all;

syms x y ;

% 求解該函式的最小值
f = (x - 2)^2 + (y - 3)^2 + 5; % s.t. 1 <= x <= 3,2 <= y <= 4

%lb = [1 2]’;ub = [3 4]’; % lb = [-inf -inf]’ ub = [inf inf]’
lb = zeros(2,1);
ub = zeros(2,1);
lb(:) = -inf;
ub(:) = inf;
%% 求解海森矩陣 H

H = hessian(f,[x,y]);

% convert to double type
H = double(H);

%% 求解一次項係數 F
% fexp = expand(f);
fcol = collect(f,{‘x’,’y’}); % x^2 - 4*x + y^2 - 6*y + 18
disp(fcol);
% get F = [-4 -6]’
F = [-4 -6]’;
% method : interior-point-convex ,trust-region-reflective ,active-set
options = optimoptions(‘quadprog’,’Algorithm’,’interior-point-convex’);
[x,fval,exitflag,output] = quadprog(H,F,[],[],[],[],lb,ub,[],options);