%% 10-1 列出所有优化参数列表
optimset
%% 实验结果
% ex10_1
% Display: [ off | iter | iter-detailed | notify | notify-detailed | final | final-detailed ]
% MaxFunEvals: [ positive scalar ]
% MaxIter: [ positive scalar ]
% TolFun: [ positive scalar ]
% TolX: [ positive scalar ]
% FunValCheck: [ on | {off} ]
% OutputFcn: [ function | {[]} ]
% PlotFcns: [ function | {[]} ]
% Algorithm: [ active-set | interior-point | interior-point-convex | levenberg-marquardt | ...
% sqp | trust-region-dogleg | trust-region-reflective ]
% AlwaysHonorConstraints: [ none | {bounds} ]
% DerivativeCheck: [ on | {off} ]
% Diagnostics: [ on | {off} ]
% DiffMaxChange: [ positive scalar | {Inf} ]
% DiffMinChange: [ positive scalar | {0} ]
% FinDiffRelStep: [ positive vector | positive scalar | {[]} ]
% FinDiffType: [ {forward} | central ]
% GoalsExactAchieve: [ positive scalar | {0} ]
% GradConstr: [ on | {off} ]
% GradObj: [ on | {off} ]
% HessFcn: [ function | {[]} ]
% Hessian: [ user-supplied | bfgs | lbfgs | fin-diff-grads | on | off ]
% HessMult: [ function | {[]} ]
% HessPattern: [ sparse matrix | {sparse(ones(numberOfVariables))} ]
% HessUpdate: [ dfp | steepdesc | {bfgs} ]
% InitBarrierParam: [ positive scalar | {0.1} ]
% InitTrustRegionRadius: [ positive scalar | {sqrt(numberOfVariables)} ]
% Jacobian: [ on | {off} ]
% JacobMult: [ function | {[]} ]
% JacobPattern: [ sparse matrix | {sparse(ones(Jrows,Jcols))} ]
% LargeScale: [ on | off ]
% MaxNodes: [ positive scalar | {1000*numberOfVariables} ]
% MaxPCGIter: [ positive scalar | {max(1,floor(numberOfVariables/2))} ]
% MaxProjCGIter: [ positive scalar | {2*(numberOfVariables-numberOfEqualities)} ]
% MaxSQPIter: [ positive scalar | {10*max(numberOfVariables,numberOfInequalities+numberOfBounds)} ]
% MaxTime: [ positive scalar | {7200} ]
% MeritFunction: [ singleobj | {multiobj} ]
% MinAbsMax: [ positive scalar | {0} ]
% ObjectiveLimit: [ scalar | {-1e20} ]
% PrecondBandWidth: [ positive scalar | 0 | Inf ]
% RelLineSrchBnd: [ positive scalar | {[]} ]
% RelLineSrchBndDuration: [ positive scalar | {1} ]
% ScaleProblem: [ none | obj-and-constr | jacobian ]
% SubproblemAlgorithm: [ cg | {ldl-factorization} ]
% TolCon: [ positive scalar ]
% TolConSQP: [ positive scalar | {1e-6} ]
% TolPCG: [ positive scalar | {0.1} ]
% TolProjCG: [ positive scalar | {1e-2} ]
% TolProjCGAbs: [ positive scalar | {1e-10} ]
% TypicalX: [ vector | {ones(numberOfVariables,1)} ]
% UseParallel: [ logical scalar | true | {false} ]
%% 10-2 对下列非线性方程组按线性规划问题进行求解:
% min f(x) = -5x1 - 4x2 - 6x3
% x1 - x2 + x3 ≤ 20
% 3x1 + 2x2 + 4x3 ≤ 42
% 3x1 + 2x2 ≤ 20
% x1, x2, x3 ≥ 0
f = [-5;-4;-6] % 输入目标函数系数矩阵
A = [1 -1 1;3 2 4;3 2 0] % 输入不等式约束系数矩阵
b = [20;42;30] % 输入右端点
lb = zeros(3,1);
[x,fval,exitflag,output,lambda] = linprog(f,A,b,[],[],lb,[],[],optimset('Display','iter'))
%% 实验结果
% ex10_2
% f =
% -5
% -4
% -6
% A =
% 1 -1 1
% 3 2 4
% 3 2 0
% b =
% 20
% 42
% 30
%
% LP preprocessing removed 0 inequalities, 0 equalities,
% 0 variables, and added 0 non-zero elements.
%
% Iter Time Fval Primal Infeas Dual Infeas
% 0 0.013 0.000000e+00 0.000000e+00 3.880779e+00
% 3 0.014 -7.800000e+01 0.000000e+00 0.000000e+00
%
% Optimal solution found.
%
% x =
% 0
% 15.0000
% 3.0000
% fval =
% -78
% exitflag =
% 1
% output =
% 包含以下字段的 struct:
%
% iterations: 3
% constrviolation: 0
% message: 'Optimal solution found.'
% algorithm: 'dual-simplex'
% firstorderopt: 1.7764e-15
% lambda =
% 包含以下字段的 struct:
%
% lower: [3×1 double]
% upper: [3×1 double]
% eqlin: []
% ineqlin: [3×1 double]
%% 10-3 对下列非线性方程组按线性规划问题进行求解:
f = [-80,-125] % 输入目标函数系数矩阵
A = [8 5;6 4;4 5] % 输入不等式约束系数矩阵
b = [3500;1800;2800] % 输入右端点
lb = [0;0];
[x,fval,exitflag,output,lambda] = linprog(f,A,b,[],[],lb,[],[],optimset('Display','iter'))
%% 实验结果
% ex10_3
% f =
% -80 -125
% A =
% 8 5
% 6 4
% 4 5
% b =
% 3500
% 1800
% 2800
%
% LP preprocessing removed 0 inequalities, 0 equalities,
% 0 variables, and added 0 non-zero elements.
%
% Iter Time Fval Primal Infeas Dual Infeas
% 0 0 0.000000e+00 0.000000e+00 2.482324e+01
% 1 0.001 -7.000000e+04 4.800041e+02 0.000000e+00
% 2 0.001 -5.625000e+04 0.000000e+00 0.000000e+00
%
% Optimal solution found.
%
% x =
% 0
% 450.0000
% fval =
% -5.6250e+04
% exitflag =
% 1
% output =
% 包含以下字段的 struct:
%
% iterations: 2
% constrviolation: 0
% message: 'Optimal solution found.'
% algorithm: 'dual-simplex'
% firstorderopt: 7.1054e-12
% lambda =
% 包含以下字段的 struct:
%
% lower: [2×1 double]
% upper: [2×1 double]
% eqlin: []
% ineqlin: [3×1 double]
%% 10-4 对下列非线性方程组按线性规划问题进行求解:
f = [-0.1 -0.122 -0.15 -0.12 -0.08] % 输入目标函数系数矩阵
A = [1 -1 -1 -1 -1;0 -1 1 -1 1] % 输入不等式约束系数矩阵
b = [0;0] % 输入右端点
Aeq = [1 1 1 1 1]
beq = [1]
lb = [0;0;0;0;0]
[x,fval,exitflag,output,lambda] = linprog(f,A,b,Aeq,beq,lb,[],[],optimset('Display','iter'))
%% 实验结果
% ex10_4
% f =
% -0.1000 -0.1220 -0.1500 -0.1200 -0.0800
% A =
% 1 -1 -1 -1 -1
% 0 -1 1 -1 1
% b =
% 0
% 0
% Aeq =
% 1 1 1 1 1
% beq =
% 1
% lb =
% 0
% 0
% 0
% 0
% 0
%
% LP preprocessing removed 1 inequalities, 0 equalities,
% 2 variables, and 8 non-zero elements.
%
% Iter Time Fval Primal Infeas Dual Infeas
% 0 0.001 0.000000e+00 1.000000e+00 2.275610e-01
% 1 0.001 -1.500000e-01 1.000000e+00 0.000000e+00
% 2 0.001 -1.360000e-01 0.000000e+00 0.000000e+00
%
% Optimal solution found.
%
% x =
% 0
% 0.5000
% 0.5000
% 0
% 0
% fval =
% -0.1360
% exitflag =
% 1
% output =
% 包含以下字段的 struct:
%
% iterations: 2
% constrviolation: 0
% message: 'Optimal solution found.'
% algorithm: 'dual-simplex'
% firstorderopt: 1.3878e-17
% lambda =
% 包含以下字段的 struct:
%
% lower: [5×1 double]
% upper: [5×1 double]
% eqlin: 0.1360
% ineqlin: [2×1 double]
%% 10-5 求下面函数在区间[-1.2,1]内的最小值
% f(x) = 100*(x2-x1^2)^2 + (a-x1)^2
a = sqrt(2) % 给定未知系数a的值
banana = @(x)100*(x(2)-x(1)^2)^2+(a-x(1))^2 % 使用内联形式定义函数
[x,fval,exitflag,output] = fminsearch(banana,[-1.2,1],optimset('TolX',1e-8)) % 调用fminsearch函数求解最小值
%% 实验结果
% ex10_5
% a =
% 1.4142
% banana =
% 包含以下值的 function_handle:
% @(x)100*(x(2)-x(1)^2)^2+(a-x(1))^2
% x =
% 1.4142 2.0000
% fval =
% 4.2065e-18
% exitflag =
% 1
% output =
% 包含以下字段的 struct:
%
% iterations: 131
% funcCount: 249
% algorithm: 'Nelder-Mead simplex direct search'
% message: '优化已终止:↵ 当前的 x 满足使用 1.000000e-08 的 OPTIONS.TolX 的终止条件,↵F(X) 满足使用 1.000000e-04 的 OPTIONS.TolFun 的收敛条件↵'
%% 10-6 分别用fminunc函数与fminsearch函数求解无约束优化问题
% f(x) = 3*x1^2 + 2*x1*x2 + x2^2
x0 = [1,1];
[x,fval,exitflag,output,grad,hessian] = fminunc(@ex10_6a,x0) % fminunc函数
[x,fval,exitflag,output] = fminsearch(@ex10_6a,x0) % fminsearch函数
function f = ex10_6a(x)
f = 3*x(1)^2 + 2*x(1)*x(2) + x(2)^2;
end
%% 实验结果
% ex10_6
%
% Computing finite-difference Hessian using objective function.
%
% Local minimum found.
%
% Optimization completed because the size of the gradient is less than
% the value of the optimality tolerance.
%
% <stopping criteria details>
% x =
% 1.0e-06 *
% 0.2541 -0.2029
% fval =
% 1.3173e-13
% exitflag =
% 1
% output =
% 包含以下字段的 struct:
%
% iterations: 8
% funcCount: 27
% stepsize: 3.9161e-05
% lssteplength: 1
% firstorderopt: 1.1633e-06
% algorithm: 'quasi-newton'
% message: '↵Local minimum found.↵↵Optimization completed because the size of the gradient is less than↵the value of the optimality tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The first-order optimality measure, 1.292597e-07, is less ↵than options.OptimalityTolerance = 1.000000e-06.↵↵'
% grad =
% 1.0e-05 *
% 0.1163
% 0.0087
% hessian =
% 6.0000 2.0000
% 2.0000 2.0000
% x =
% 1.0e-04 *
% -0.0675 0.1715
% fval =
% 1.9920e-10
% exitflag =
% 1
% output =
% 包含以下字段的 struct:
%
% iterations: 46
% funcCount: 89
% algorithm: 'Nelder-Mead simplex direct search'
% message: '优化已终止:↵ 当前的 x 满足使用 1.000000e-04 的 OPTIONS.TolX 的终止条件,↵F(X) 满足使用 1.000000e-04 的 OPTIONS.TolFun 的收敛条件↵'
%% 10-7 计算下面函数在(-2,2)内的最小值
% f(x) = (x^3 + x)/(x^4 - x^2 + 1)
% help fminbnd
% fminbnd - 查找单变量函数在定区间上的最小值
% fminbnd 是一个一维最小值,用于求由以下条件指定的问题的最小值:
%
% x = fminbnd(fun,x1,x2)
% x = fminbnd(fun,x1,x2,options)
% x = fminbnd(problem)
% [x,fval] = fminbnd(___)
% [x,fval,exitflag] = fminbnd(___)
% [x,fval,exitflag,output] = fminbnd(___)
[x,fval,exitflag,output] = fminbnd(@ex10_7a,-2,2) % fminbnd函数
function f = ex10_7a(x)
f = (x^3 + x)/(x^4 - x^2 + 1);
end
%% 实验结果
% ex10_7
% x =
% -1.0000
% fval =
% -2.0000
% exitflag =
% 1
% output =
% 包含以下字段的 struct:
%
% iterations: 11
% funcCount: 12
% algorithm: 'golden section search, parabolic interpolation'
% message: '优化已终止:↵ 当前的 x 满足使用 1.000000e-04 的 OPTIONS.TolX 的终止条件↵'
%% 10-8 求解在约束条件0≤x1+2*x2+3*x3≤72下,函数f(x)=-x1*x2*x3的最小值的最优解和最优解的数值
A = [-1 -2 -2;1 2 2] % 不等式左边系数
b = [0;72] % 不等式右边系数
x0 = [10;10;10] % 给定初始值
[x,fval,exitflag,output,lambda] = fmincon(@ex10_8a,x0,A,b) % fminunc函数
function f = ex10_8a(x)
f = -x(1) * x(2) * x(3);
end
%% 实验结果
% ex10_8
% A =
% -1 -2 -2
% 1 2 2
% b =
% 0
% 72
% x0 =
% 10
% 10
% 10
%
% Local minimum found that satisfies the constraints.
%
% Optimization completed because the objective function is non-decreasing in
% feasible directions, to within the value of the optimality tolerance,
% and constraints are satisfied to within the value of the constraint tolerance.
%
% <stopping criteria details>
% x =
% 24.0000
% 12.0000
% 12.0000
% fval =
% -3.4560e+03
% exitflag =
% 1
% output =
% 包含以下字段的 struct:
%
% iterations: 9
% funcCount: 43
% constrviolation: 0
% stepsize: 6.5312e-05
% algorithm: 'interior-point'
% firstorderopt: 1.2552e-05
% cgiterations: 0
% message: '↵Local minimum found that satisfies the constraints.↵↵Optimization completed because the objective function is non-decreasing in ↵feasible directions, to within the value of the optimality tolerance,↵and constraints are satisfied to within the value of the constraint tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The relative first-order optimality measure, 4.358349e-08,↵is less than options.OptimalityTolerance = 1.000000e-06, and the relative maximum constraint↵violation, 0.000000e+00, is less than options.ConstraintTolerance = 1.000000e-06.↵↵'
% bestfeasible: [1×1 struct]
% lambda =
% 包含以下字段的 struct:
%
% eqlin: [0×1 double]
% eqnonlin: [0×1 double]
% ineqlin: [2×1 double]
% lower: [3×1 double]
% upper: [3×1 double]
% ineqnonlin: [0×1 double]
%% 10-9 求积下面函数的最小值
% f(x,y,z) = x*y + y*z + z*x
% 约束条件:2*x + y + 2*z = 2; x^2 + y^2 + z^2 = 4
% 求初始值 x0 = [0 1 1]
% help fmincon
% fmincon - 寻找约束非线性多变量函数的最小值
% 非线性规划求解器。
%
% x = fmincon(fun,x0,A,b)
% x = fmincon(fun,x0,A,b,Aeq,beq)
% x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
% x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
% x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
% x = fmincon(problem)
% [x,fval] = fmincon(___)
% [x,fval,exitflag,output] = fmincon(___)
% [x,fval,exitflag,output,lambda,grad,hessian] = fmincon(___)
x0 = [0 1 1]
Aeq = [2 1 2]
Beq = 2
[x,fval,exitflag] = fmincon(@ex10_9a,x0,[],[],Aeq,Beq,[],[],@ex10_9b)
function y= ex10_9a(x)
y = x(1)*x(2) + x(2)*x(3) + x(3)*x(1);
end
function [c,ceq] = ex10_9b(x)
c = [];
ceq = x(1)^2 + x(2)^2 + x(3)^2 - 4;
end
%% 实验结果
% ex10_9
% x0 =
% 0 1 1
% Aeq =
% 2 1 2
% Beq =
% 2
%
% Local minimum found that satisfies the constraints.
%
% Optimization completed because the objective function is non-decreasing in
% feasible directions, to within the value of the optimality tolerance,
% and constraints are satisfied to within the value of the constraint tolerance.
%
% <stopping criteria details>
% x =
% 0.8889 -1.5556 0.8889
% fval =
% -1.9753
% exitflag =
% 1
%% 10-10 求解下面二次规划问题
% min f(x) = 1/2*x1^2 + x2^2 - x1*x2 - 2*x1 - 6*x2
% 约束条件:
% x1 + x2 ≤ 2
% -x1 + 2x2 ≤ 2
% 2x1 + 2x ≤ 3
% x1 ≥ 0, x2 ≥ 0
H = [1 -1;-1 2]
f = [-2;-6]
A = [1 1;-1 2;2 1]
b = [2;2;3]
lb = zeros(2,1)
opts = optimset('Algorithm','interior-point-convex','Display','iter');
[x fval eflag output lambda] = quadprog(H,f,A,b,[],[],[],[],[],opts)
%% 实验结果
% ex10_10
% H =
% 1 -1
% -1 2
% f =
% -2
% -6
% A =
% 1 1
% -1 2
% 2 1
% b =
% 2
% 2
% 3
% lb =
% 0
% 0
%
% Iter Fval Primal Infeas Dual Infeas Complementarity
% 0 -8.884885e+00 3.214286e+00 1.071429e-01 1.000000e+00
% 1 -8.331868e+00 1.321041e-01 4.403472e-03 1.910489e-01
% 2 -8.212804e+00 1.676295e-03 5.587652e-05 1.009601e-02
% 3 -8.222204e+00 8.381476e-07 2.793826e-08 1.809485e-05
% 4 -8.222222e+00 3.019807e-14 1.352696e-12 7.525735e-13
%
% Minimum found that satisfies the constraints.
%
% Optimization completed because the objective function is non-decreasing in
% feasible directions, to within the value of the optimality tolerance,
% and constraints are satisfied to within the value of the constraint tolerance.
%
% <stopping criteria details>
% x =
% 0.6667
% 1.3333
% fval =
% -8.2222
% eflag =
% 1
% output =
% 包含以下字段的 struct:
%
% message: '↵Minimum found that satisfies the constraints.↵↵Optimization completed because the objective function is non-decreasing in ↵feasible directions, to within the value of the optimality tolerance,↵and constraints are satisfied to within the value of the constraint tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The relative dual feasibility, 2.254493e-13,↵is less than options.OptimalityTolerance = 1.000000e-08, the complementarity measure,↵7.525735e-13, is less than options.OptimalityTolerance, and the relative maximum constraint↵violation, 5.033011e-15, is less than options.ConstraintTolerance = 1.000000e-08.↵↵'
% algorithm: 'interior-point-convex'
% firstorderopt: 1.3527e-12
% constrviolation: 1.7764e-15
% iterations: 4
% linearsolver: 'dense'
% cgiterations: []
% lambda =
% 包含以下字段的 struct:
%
% ineqlin: [3×1 double]
% eqlin: [0×1 double]
% lower: [2×1 double]
% upper: [2×1 double]
%% 10-11 求解下面的二次规划问题
% max f(x) = -x1^2 -x2^2 + 3*x1 + 5*x2
% 约束条件
% 3*x1 + 2*x2 ≤ 5
% x1 ≥ 0, x2 ≥ 0
% help quadprog
% quadprog - 二次规划
% 具有线性约束的二次目标函数的求解器。
%
% x = quadprog(H,f)
% x = quadprog(H,f,A,b)
% x = quadprog(H,f,A,b,Aeq,beq)
% x = quadprog(H,f,A,b,Aeq,beq,lb,ub)
% x = quadprog(H,f,A,b,Aeq,beq,lb,ub,x0)
% x = quadprog(H,f,A,b,Aeq,beq,lb,ub,x0,options)
% x = quadprog(problem)
% [x,fval] = quadprog(___)
% [x,fval,exitflag,output] = quadprog(___)
% [x,fval,exitflag,output,lambda] = quadprog(___)
H = [2 0;0 2]
f = [-3 -5]
A = [3 2]
b = [5]
lb = [0 0]
[x,fval,exitflag] = quadprog(H,f,A,b,[],[],lb)
%% 实验结果
% ex10_11
% H =
% 2 0
% 0 2
% f =
% -3 -5
% A =
% 3 2
% b =
% 5
% lb =
% 0 0
%
% Minimum found that satisfies the constraints.
%
% Optimization completed because the objective function is non-decreasing in
% feasible directions, to within the value of the optimality tolerance,
% and constraints are satisfied to within the value of the constraint tolerance.
%
% <stopping criteria details>
% x =
% 0.4615
% 1.8077
% fval =
% -6.9423
% exitflag =
% 1
%% 10-12 求解下面的函数最小最大值问题:
% min(x) max(Fi) {f1(x),f2(x),f3(x),f4(x),f5(x)}
% 约束条件
% x1^2 + x2^2 ≤ 8
% x1 + x2 ≤ 3
% -3 ≤ x1 ≤ 3
% -2 ≤ x2 ≤ 2
% 其中
% f1(x) = 2*x1^2 + x2^2 - 48*x1 - 40*x2 + 304
% f2(x) = -x1^2 - 3*x2^2
% f3(x) = x1 + 3*x2 - 18
% f4(x) = -x1 - x2
% f5(x) = x1 + x2 - 8
% help fminimax
% fminimax - 求解 minimax 约束问题
% fminimax 寻找能够最小化一组目标函数最大值的点。
%
% x = fminimax(fun,x0)
% x = fminimax(fun,x0,A,b)
% x = fminimax(fun,x0,A,b,Aeq,beq)
% x = fminimax(fun,x0,A,b,Aeq,beq,lb,ub)
% x = fminimax(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
% x = fminimax(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
% x = fminimax(problem)
% [x,fval] = fminimax(___)
% [x,fval,maxfval,exitflag,output] = fminimax(___)
% [x,fval,maxfval,exitflag,output,lambda] = fminimax(___)
x0 = [0.1;0.1]
[x,fval,maxfval] = fminimax(@ex10_12a,x0)
options = optimset('MinAbsMax',5); % 最小绝对值
[x,fval,maxfval,exitflag,output,lambda] = fminimax(@ex10_12a,x0,[],[],[],[],[],[],[],options)
function f = ex10_12a(x)
f(1) = 2*x(1)^2 + x(2)^2 - 48*x(1) - 40*x(2) + 304;
f(2) = -x(1)^2 - 3*x(2)^2;
f(3) = x(1) + 3*x(2) - 18;
f(4) = -x(1) - x(2);
f(5) = x(1) + x(2) - 8;
end
%% 实验结果
% ex10_12
% x0 =
% 0.1000
% 0.1000
%
% Local minimum possible. Constraints satisfied.
%
% fminimax stopped because the size of the current search direction is less than
% twice the value of the step size tolerance and constraints are
% satisfied to within the value of the constraint tolerance.
%
% <stopping criteria details>
% x =
% 4.0000
% 4.0000
% fval =
% 0.0000 -64.0000 -2.0000 -8.0000 -0.0000
% maxfval =
% 4.2405e-11
%
% Local minimum possible. Constraints satisfied.
%
% fminimax stopped because the predicted change in the objective function
% is less than the value of the function tolerance and constraints
% are satisfied to within the value of the constraint tolerance.
%
% <stopping criteria details>
% x =
% 4.9256
% 2.0796
% fval =
% 37.2356 -37.2356 -6.8357 -7.0052 -0.9948
% maxfval =
% 37.2356
% exitflag =
% 5
% output =
% 包含以下字段的 struct:
%
% iterations: 7
% funcCount: 35
% lssteplength: 1
% stepsize: 2.7799e-06
% algorithm: 'active-set'
% firstorderopt: []
% constrviolation: 2.8906e-10
% message: '↵Local minimum possible. Constraints satisfied.↵↵fminimax stopped because the predicted change in the objective function↵is less than the value of the function tolerance and constraints ↵are satisfied to within the value of the constraint tolerance.↵↵<stopping criteria details>↵↵Optimization stopped because the predicted change in the objective function,↵2.524100e-10, is less than options.FunctionTolerance = 1.000000e-06, and the maximum constraint↵violation, 2.890630e-10, is less than options.ConstraintTolerance = 1.000000e-06.↵↵'
% lambda =
% 包含以下字段的 struct:
%
% lower: [2×1 double]
% upper: [2×1 double]
% eqlin: [0×1 double]
% eqnonlin: [0×1 double]
% ineqlin: [0×1 double]
% ineqnonlin: [0×1 double]
%% 10-13 求解如下“半无限”多元约束优化问题
% max f(x) = 8*x1 + 4.5*x2^2 + 3*x3
% 约束
% 1.5*x3^2 - x2^2 ≥ 3
% x1 + 4*x2 - 5*x3 ≤ 32
% x1 + 3*x2 + 2*x3 ≤ 29
% x1,x2,x3 ≥ 0
% 半无限约束K_i(w,x)为:
% K1(x,w1) = sin(w1*x1).*cos(w1*x1)-1/1000*(w1-45).^2-sin(w1*x3)-x2-1;
% K2(x,w2) = sin(w2*x2).*cos(w2*x2)-1/1000*(w2-45).^2-sin(w2*x3)-x3-1;
% help fseminf
% fseminf - 求解半无限约束多变量非线性函数的最小值
% 此 MATLAB 函数 从 x0 开始,求解满足 seminfcon 中定义的 ntheta 半无限约束条件的函
% 数 fun 的最小值。
%
% x = fseminf(fun,x0,ntheta,seminfcon)
% x = fseminf(fun,x0,ntheta,seminfcon,A,b)
% x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq)
% x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq,lb,ub)
% x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq,lb,ub,options)
% x = fseminf(problem)
% [x,fval] = fseminf(...)
% [x,fval,exitflag] = fseminf(...)
% [x,fval,exitflag,output] = fseminf(...)
% [x,fval,exitflag,output,lambda] = fseminf(...)
A = [1 4 5;1 3 2];
b = [32 29];
Aeq = [];
beq = [];
lb = zeros(1,size(A,2));
x0 = ones(size(A,2),1);
[x,feval] = fseminf(@ex10_13a,x0,2,@ex10_13b,A,b,Aeq,beq,lb)
function f = ex10_13a(x)
f = 8*x(1) + 4.5*x(2) + 3*x(3);
f = -f;
end
function [c,ceq,K1,K2,s] = ex10_13b(x,s)
if isnan(s(1,1))
s = [0.2 0;0.2 0];
end
% 采样的数据点
w1 = 1:s(1,1):100;
w2 = 1:s(2,1):100;
% 半无限约束
K1 = sin(w1*x(1)).*cos(w1*x(1))-1/1000*(w1-45).^2-sin(w1*x(3))-x(2)-1;
K2 = sin(w2*x(2)).*cos(w2*x(2))-1/1000*(w2-45).^2-sin(w2*x(3))-x(3)-1;
% 非线性约束
c = 3-1.5*x(3)^2-x(2)^2;
ceq = [];
end
%% 实验结果
% ex10_13
%
% Solver stopped prematurely.
%
% fseminf stopped because it exceeded the function evaluation limit,
% options.MaxFunctionEvaluations = 3.000000e+02.
%
% x =
% 9.4493
% 2.0942
% 2.8347
% feval =
% -93.5225
%% 10-14 求解以下的多目标最小化问题
% x = (A+BKC)x+Bu
% y = Cx
% 其中
% A = [-0.5 0 0;0 -2 10;0 1 -2]
% B = [1 0;-2 2;0 1]
% C = [1 0 0;0 0 1]
% K0 = [-1 -1;-1 -1]
% goal = [-5 -3 -1]
% help fgoalattain
% fgoalattain - 求解涉及多目标的目标达到问题
% fgoalattain 求解目标达到问题,这是多目标优化问题最小化的一种表示。
%
% x = fgoalattain(fun,x0,goal,weight)
% x = fgoalattain(fun,x0,goal,weight,A,b)
% x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq)
% x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub)
% x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub,nonlcon)
% x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub,nonlcon,options)
% x = fgoalattain(problem)
% [x,fval] = fgoalattain(___)
% [x,fval,attainfactor,exitflag,output] = fgoalattain(___)
% [x,fval,attainfactor,exitflag,output,lambda] = fgoalattain(___)
A = [-0.5 0 0;0 -2 10;0 1 -2]
B = [1 0;-2 2;0 1]
C = [1 0 0;0 0 1]
K0 = [-1 -1;-1 -1] % 初始化控制矩阵
goal = [-5 -3 -1] % 优化目标特征值
weight = abs(goal) % 目标函数权重
lb = -4 * ones(size(K0)) % 决策变量下限
ub = 4 * ones(size(K0)) % 决策变量上限
options = optimset('Display', 'iter'); % 属性参数设置
[K, fval, attainfactor] = fgoalattain(@(K)ex10_14a(K,A,B,C),K0,goal,weight,[],[],[],[],lb,ub,[],options)
function F = ex10_14a(K,A,B,C)
F = sort(eig(A+B*K*C));
end
%% 实验结果
% ex10_14
% A =
% -0.5000 0 0
% 0 -2.0000 10.0000
% 0 1.0000 -2.0000
% B =
% 1 0
% -2 2
% 0 1
% C =
% 1 0 0
% 0 0 1
% K0 =
% -1 -1
% -1 -1
% goal =
% -5 -3 -1
% weight =
% 5 3 1
% lb =
% -4 -4
% -4 -4
% ub =
% 4 4
% 4 4
%
% Attainment Max Line search Directional
% Iter F-count factor constraint steplength derivative Procedure
% 0 6 0 1.88521
% 1 13 1.031 0.02998 1 0.745
% 2 20 0.3525 0.06863 1 -0.613
% 3 27 -0.1706 0.1071 1 -0.223 Hessian modified
% 4 34 -0.2236 0.06654 1 -0.234 Hessian modified twice
% 5 41 -0.3568 0.007894 1 -0.0812
% 6 48 -0.3645 0.000145 1 -0.164 Hessian modified
% 7 55 -0.3645 0 1 -0.00515 Hessian modified
% 8 62 -0.3675 0.0001546 1 -0.00812 Hessian modified twice
% 9 69 -0.3889 0.008328 1 -0.00751 Hessian modified
% 10 76 -0.3862 0 1 0.00568
% 11 83 -0.3863 2.955e-13 1 -0.998 Hessian modified twice
%
% Local minimum possible. Constraints satisfied.
%
% fgoalattain stopped because the size of the current search direction is less than
% twice the value of the step size tolerance and constraints are
% satisfied to within the value of the constraint tolerance.
%
% <stopping criteria details>
% K =
% -4.0000 -0.2564
% -4.0000 -4.0000
% fval =
% -6.9313
% -4.1588
% -1.4099
% attainfactor =
% -0.3863
%% 10-15 在工程实验中,得到下面一组数据
% ti = [0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0]
% yi = [0 3.2 4.0 4.5 5.8 6.9 8.1 9.9 10.8]
% 求系数a,b,cd,使得函数f(t) = a + b*cos(t) + c*sin(t) + d*t^3
% help lsqcurvefit
% lsqcurvefit - 用最小二乘求解非线性曲线拟合(数据拟合)问题
% 非线性最小二乘求解器
%
% x = lsqcurvefit(fun,x0,xdata,ydata)
% x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
% x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options)
% x = lsqcurvefit(problem)
% [x,resnorm] = lsqcurvefit(___)
% [x,resnorm,residual,exitflag,output] = lsqcurvefit(___)
% [x,resnorm,residual,exitflag,output,lambda,jacobian] = lsqcurvefit(___)
ti = [0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0]
yi = [0 3.2 4.0 4.5 5.8 6.9 8.1 9.9 10.8]
x0 = [1 1 1 1]'
[x,resnorm,residual,exitflag,output,lambda,J] = lsqcurvefit(ex10_15a,x0,ti,yi)
function f = ex10_15a(x,ti)
n = length(ti);
for i = 1:n
f(i) = x(1) + x(2) * cos(ti(i)) + x(3) * sin(ti(i)) + x(4) * ti(i)^2;
end
end
%% 实验结果
% 输入参数的数目不足。
% 出错 ex10_15>ex10_15a (第 19 行)
% n = length(ti);
% 出错 ex10_15 (第 16 行)
% [x,resnorm,residual,exitflag,output,lambda,J] = lsqcurvefit(ex10_15a,x0,ti,yi)