1. 优化
1)无约束非线性优化命令
[x,fval,exitflag,output]=fminsearch(fun,x0,options)
输入参数:参数fun表示优化的目标函数,参数表示执行优化的初始数值,参数“options”表示进行优化的各种属性,一般需要使用optimset函数来进行设置
输出参数:参数x表示最优解,fval表示最优解对应的函数数值,参数flag表示函数退出优化运算的原因,取值为0、-1、1其中数值1表示函数收敛于最优解,0表示函数迭代次数超过了优化属性的设置,-1表示优化迭代算法被output函数终止。
----
>> x0=[1,1];
>> options=optimset('Display','iter','TolFun',1e-18,'GradObj','on');
>> [x,fval,exitflag,output,grad]=fminunc(@optfun,x0,options)
Norm of First-order
Iteration f(x) step optimality CG-iterations
0 6 8
1 2.34193e-031 1.41421 1.11e-015 1
2 0 5.55112e-016 0 1
Local minimum found.
Optimization completed because the size of the gradient is less than
the selected value of the function tolerance.
<stopping criteria details>
x =
0 0
fval =
0
exitflag =
1
output =
iterations: 2
funcCount: 3
cgiterations: 2
firstorderopt: 0
algorithm: 'large-scale: trust-region Newton'
message: [1x542 char]
grad =
0
0
>> [x,fval,exitflag,output]=fminsearch(@optfun,x0,options)
Iteration Func-count min f(x) Procedure
0 1 6
1 3 6 initial simplex
2 5 5.52062 expand
3 7 4.91391 expand
4 9 3.86566 expand
5 11 2.52517 expand
6 13 1.31047 expand
7 15 0.973674 reflect
8 17 0.973674 contract outside
9 19 0.973674 contract inside
10 21 0.973674 contract outside
11 23 0.795334 expand
12 25 0.736819 expand
13 27 0.361494 expand
14 28 0.361494 reflect
15 29 0.361494 reflect
16 31 0.057735 expand
17 33 0.057735 contract inside
18 35 0.000685158 reflect
19 37 0.000685158 contract outside
20 39 0.000685158 contract inside
21 41 0.000685158 contract outside
22 43 0.000685158 contract inside
23 45 0.000685158 contract inside
24 47 0.000685158 contract inside
25 49 0.000295783 contract outside
26 51 0.000111281 contract inside
27 53 0.000111281 contract inside
28 55 5.64752e-005 reflect
29 57 1.28721e-005 contract inside
30 59 1.28721e-005 contract outside
31 61 1.16146e-005 contract inside
32 63 2.73574e-006 contract outside
33 65 1.51444e-006 contract inside
34 67 1.19712e-006 contract inside
35 69 5.90582e-007 contract inside
36 71 2.26821e-007 contract inside
37 73 7.82125e-008 contract inside
38 75 7.82125e-008 contract outside
39 77 2.5115e-008 contract inside
40 78 2.5115e-008 reflect
41 80 1.36352e-008 contract inside
42 82 9.30314e-009 contract inside
43 84 1.6625e-009 contract inside
44 86 1.6625e-009 contract inside
45 87 1.6625e-009 reflect
46 89 1.992e-010 contract inside
47 91 1.992e-010 contract inside
48 93 1.992e-010 contract inside
49 95 1.31571e-010 contract outside
50 97 3.59883e-011 contract inside
51 99 3.59883e-011 contract inside
52 101 7.31994e-012 contract outside
53 103 3.29879e-012 contract inside
54 105 3.29879e-012 contract inside
55 107 1.91792e-012 contract inside
56 109 1.29002e-012 contract inside
57 111 1.26784e-013 contract inside
58 113 1.26784e-013 contract outside
59 115 1.26784e-013 contract inside
60 116 1.26784e-013 reflect
61 118 3.00685e-014 contract inside
62 120 3.00685e-014 contract inside
63 122 2.09383e-014 contract inside
64 123 2.09383e-014 reflect
65 125 5.88086e-015 contract inside
66 127 5.88086e-015 contract inside
67 129 2.88806e-015 contract inside
68 131 8.13289e-016 reflect
69 133 7.06148e-016 contract inside
70 135 4.06592e-016 contract inside
71 137 8.40165e-017 contract inside
72 139 8.40165e-017 contract inside
73 141 6.35289e-017 contract inside
74 143 5.87172e-018 contract inside
75 145 5.87172e-018 contract inside
76 147 5.87172e-018 contract inside
77 149 2.26684e-018 contract inside
78 151 1.95216e-018 contract inside
79 153 4.03648e-019 contract inside
80 155 4.03648e-019 contract inside
81 157 4.03648e-019 contract inside
Optimization terminated:
the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-004
and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 1.000000e-018
x =
1.0e-009 *
-0.4052 0.1308
fval =
4.0365e-019
exitflag =
1
output =
iterations: 81
funcCount: 157
algorithm: 'Nelder-Mead simplex direct search'
message: [1x196 char]
----
2)约束条件的非线性优化命令
[x,fval,exitflag,output,lambda]=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
输入参数: 参数fun表示的是优化目标函数,x0表示的是优化的初始值,参数A、b表示的是满足线性关系式Ax<=b的系数矩阵和结果矩阵,参数Aeq、beq表示的是满足线性等式Aeq.x=beq的矩阵,参数lb、ub表示满足参数取值范围lb<=x<=ub的上限和下限;参数nonlcon则表示需要参数满足的非线性关系式c(x)<=0和ceq(x)<=0的优化情况。
---
function f=optcon(x)
f=-x(1)*x(2)*x(3);
>> A=[1,-2,-2;1,2,2];
>> b=[0;72];
>> x0=[10;10;10];
>> [x,fval,exitflag,output,lamda]=fmincon(@optcon,x0,A,b)
--
2. 线性规划
[x,fval,exitflag,output,lambda]=linprog(f,A,b,Aeq,beq,lb,ub,x0,options)
使用遗传算法求解二次规划
1)无约束非线性优化命令
[x,fval,exitflag,output]=fminsearch(fun,x0,options)
输入参数:参数fun表示优化的目标函数,参数表示执行优化的初始数值,参数“options”表示进行优化的各种属性,一般需要使用optimset函数来进行设置
输出参数:参数x表示最优解,fval表示最优解对应的函数数值,参数flag表示函数退出优化运算的原因,取值为0、-1、1其中数值1表示函数收敛于最优解,0表示函数迭代次数超过了优化属性的设置,-1表示优化迭代算法被output函数终止。
----
>> x0=[1,1];
>> options=optimset('Display','iter','TolFun',1e-18,'GradObj','on');
>> [x,fval,exitflag,output,grad]=fminunc(@optfun,x0,options)
Norm of First-order
Iteration f(x) step optimality CG-iterations
0 6 8
1 2.34193e-031 1.41421 1.11e-015 1
2 0 5.55112e-016 0 1
Local minimum found.
Optimization completed because the size of the gradient is less than
the selected value of the function tolerance.
<stopping criteria details>
x =
0 0
fval =
0
exitflag =
1
output =
iterations: 2
funcCount: 3
cgiterations: 2
firstorderopt: 0
algorithm: 'large-scale: trust-region Newton'
message: [1x542 char]
grad =
0
0
>> [x,fval,exitflag,output]=fminsearch(@optfun,x0,options)
Iteration Func-count min f(x) Procedure
0 1 6
1 3 6 initial simplex
2 5 5.52062 expand
3 7 4.91391 expand
4 9 3.86566 expand
5 11 2.52517 expand
6 13 1.31047 expand
7 15 0.973674 reflect
8 17 0.973674 contract outside
9 19 0.973674 contract inside
10 21 0.973674 contract outside
11 23 0.795334 expand
12 25 0.736819 expand
13 27 0.361494 expand
14 28 0.361494 reflect
15 29 0.361494 reflect
16 31 0.057735 expand
17 33 0.057735 contract inside
18 35 0.000685158 reflect
19 37 0.000685158 contract outside
20 39 0.000685158 contract inside
21 41 0.000685158 contract outside
22 43 0.000685158 contract inside
23 45 0.000685158 contract inside
24 47 0.000685158 contract inside
25 49 0.000295783 contract outside
26 51 0.000111281 contract inside
27 53 0.000111281 contract inside
28 55 5.64752e-005 reflect
29 57 1.28721e-005 contract inside
30 59 1.28721e-005 contract outside
31 61 1.16146e-005 contract inside
32 63 2.73574e-006 contract outside
33 65 1.51444e-006 contract inside
34 67 1.19712e-006 contract inside
35 69 5.90582e-007 contract inside
36 71 2.26821e-007 contract inside
37 73 7.82125e-008 contract inside
38 75 7.82125e-008 contract outside
39 77 2.5115e-008 contract inside
40 78 2.5115e-008 reflect
41 80 1.36352e-008 contract inside
42 82 9.30314e-009 contract inside
43 84 1.6625e-009 contract inside
44 86 1.6625e-009 contract inside
45 87 1.6625e-009 reflect
46 89 1.992e-010 contract inside
47 91 1.992e-010 contract inside
48 93 1.992e-010 contract inside
49 95 1.31571e-010 contract outside
50 97 3.59883e-011 contract inside
51 99 3.59883e-011 contract inside
52 101 7.31994e-012 contract outside
53 103 3.29879e-012 contract inside
54 105 3.29879e-012 contract inside
55 107 1.91792e-012 contract inside
56 109 1.29002e-012 contract inside
57 111 1.26784e-013 contract inside
58 113 1.26784e-013 contract outside
59 115 1.26784e-013 contract inside
60 116 1.26784e-013 reflect
61 118 3.00685e-014 contract inside
62 120 3.00685e-014 contract inside
63 122 2.09383e-014 contract inside
64 123 2.09383e-014 reflect
65 125 5.88086e-015 contract inside
66 127 5.88086e-015 contract inside
67 129 2.88806e-015 contract inside
68 131 8.13289e-016 reflect
69 133 7.06148e-016 contract inside
70 135 4.06592e-016 contract inside
71 137 8.40165e-017 contract inside
72 139 8.40165e-017 contract inside
73 141 6.35289e-017 contract inside
74 143 5.87172e-018 contract inside
75 145 5.87172e-018 contract inside
76 147 5.87172e-018 contract inside
77 149 2.26684e-018 contract inside
78 151 1.95216e-018 contract inside
79 153 4.03648e-019 contract inside
80 155 4.03648e-019 contract inside
81 157 4.03648e-019 contract inside
Optimization terminated:
the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-004
and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 1.000000e-018
x =
1.0e-009 *
-0.4052 0.1308
fval =
4.0365e-019
exitflag =
1
output =
iterations: 81
funcCount: 157
algorithm: 'Nelder-Mead simplex direct search'
message: [1x196 char]
----
2)约束条件的非线性优化命令
[x,fval,exitflag,output,lambda]=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
输入参数: 参数fun表示的是优化目标函数,x0表示的是优化的初始值,参数A、b表示的是满足线性关系式Ax<=b的系数矩阵和结果矩阵,参数Aeq、beq表示的是满足线性等式Aeq.x=beq的矩阵,参数lb、ub表示满足参数取值范围lb<=x<=ub的上限和下限;参数nonlcon则表示需要参数满足的非线性关系式c(x)<=0和ceq(x)<=0的优化情况。
---
function f=optcon(x)
f=-x(1)*x(2)*x(3);
>> A=[1,-2,-2;1,2,2];
>> b=[0;72];
>> x0=[10;10;10];
>> [x,fval,exitflag,output,lamda]=fmincon(@optcon,x0,A,b)
--
2. 线性规划
[x,fval,exitflag,output,lambda]=linprog(f,A,b,Aeq,beq,lb,ub,x0,options)
使用遗传算法求解二次规划