资源描述
实验六
1415100076 陈田
实验项目名称:优化
实验时间:2016-5-27、2016-6—3
实验地点:理学楼525
实验目得:
1、掌握Matlab优化工具箱得基本用法,利用优化工具包解线性规划与非线性规划得问题;对不同算法作初步分析、比较;
2、练习实际问题得非线性最小二乘拟合.
3、初步了解实际问题得线性规划与非线性规划得模型建立。
实验内容:
1、 教材205页习题2;
function f=exam0702fun(x)
f=exp(x(1))*(4*x(1)、^2+2*x(2)、^2+4*x(1)*x(2)+2*x(2)+1);
end
M文件:formatshorte
x0=[—1,1];
’—--case1:bfgs-——'
opt1=optimset('larges’,’off',’maxfunevals',1000);
[x1,v1,exit1,out1]=fminunc('exam0702fun’,x0,opt1)
pause
'—-—case2:dfp--—'
fopt=optimset(opt1,'hessupdate',’dfp');
[x2,v2,exit2,out2]=fminunc('exam0702fun',x0,fopt)
pause
’——-case3:steep---'
fopt=optimset(opt1,'hessupdate’,’steepdesc’);
[x3,v3,exit3,out3]=fminunc('exam0702fun’,x0,fopt)
pause
'+++ result of solutions +++’
solutions=[x1;x2;x3];
funvalues=[v1;v2;v3];
iterations=[out1、funcCount;out2、funcCount;out3、funcCount];
[solutions,funvalues,iterations]
运行结果:>〉 c62
ans =
-—-case1:bfgs—--
Local minimum found、Optimization pleted because the size of the gradient is less than
the default value of the optimality tolerance、〈stopping criteria details>
x1 = 5、0000e-01 -1、0000e+00
v1 = 3、6609e—15
exit1 = 1
out1 = iterations: 8
funcCount: 66
stepsize: 6、3361e-07
lssteplength: 1
firstorderopt: 1、2284e-07
algorithm: 'quasi-newton'
message: 'Local minimum found、…'
ans =
-——case2:dfp—--
Local minimum found、Optimization pleted because the size of the gradient is less than
the default value of the optimality tolerance、<stopping criteria details>
x2 = 5、0000e-01 -1、0000e+00
v2 = 1、2447e—14
exit2 = 1
out2 = iterations: 8
funcCount: 51
stepsize: 3、7463e-06
lssteplength: 1
firstorderopt: 5、8963e—07
algorithm: 'quasi—newton'
message: 'Local minimum found、…’
ans =
---case3:steep—--
Local minimum possible、fminunc stopped because the size of the current step is less than
the default value of the step size tolerance、〈stopping criteria details>
x3 = 5、0000e-01 -1、0000e+00
v3 = 9、0530e—12
exit3 = 2
out3 = iterations: 49
funcCount: 306
stepsize: 1、1892e-06
lssteplength: 1、0003e-01
firstorderopt: 8、4759e-06
algorithm: 'quasi—newton'
message: ’Local minimum possible、…'
ans =
+++ result of solutions +++
ans =
5、0000e-01 -1、0000e+00 3、6609e-15 6、6000e+01
5、0000e—01 -1、0000e+00 1、2447e—14 5、1000e+01
5、0000e-01 -1、0000e+00 9、0530e—12 3、0600e+02
2、 教材205页习题4,要求:实现非线性最小二乘拟合即可,对“用GN与LM两种方法求解、对y作一扰动后观察迭代收敛变慢得情况”不做;
function y=F(x,t,c)
y=x(1)+x(2)*exp(—x(4)*t)+x(3)*exp(-x(5)*t)-c;
end
M文件:x0=[、5 1、5 -1 、01 、02];
t=zeros(1,33);
fori=1:33
t(i)=10*(i-1);
end
c1=[、844 、908 、932 、936 、925 、908 、881 、850 、818 、784 、751 、718 、685 、658 、628 、603 、580 、558 、538 、522 、506 、490 、478 、467 、457 、448 、438 、431 、424 、420 、414 、411 、406];
c2=rand(1,33)/10—0、05;
'--—case1:LM-—-’
opt1=optimset('LargeScale','off','Maxfunevals',1000);
[x11,norm11,res11]=lsqnonlin(’F’,x0,[],[],opt1,t,c1)
'---case2:GM---'
opt2=optimset(opt1,'Algorithm','Levenberg—Marquardt’);
[x12,norm12,res12]=lsqnonlin(’F’,x0,[],[],opt2,t,c1)
’—--case3:LM2--—’
opt3=optimset(opt1,’Disp','iter’);
[x21,norm21,res21]=lsqnonlin('F',x0,[],[],opt3,t,c2)
'---case4:GM2—-—'
opt4=optimset(opt2,'Disp’,’iter');
[x22,norm22,res22]=lsqnonlin('F’,x0,[],[],opt4,t,c2)
运行结果:〉> c64
ans =
——-case1:LM---
x11 =
3、7541e—01 1、9358e+00 -1、4647e+00 1、2868e—02 2、2123e—02
norm11 =
5、4649e-05
ans =
—-—case2:GM---
x12 =
3、7541e-01 1、9358e+00 —1、4647e+00 1、2868e-02 2、2123e-02
norm12 =
5、4649e-05
ans =
--—case3:LM2--—
x21 =
—5、8167e+01 3、6217e-01 5、8127e+01 1、0049e-01 -3、6513e—06
norm21 =
1、3721e-01
ans =
-——case4:GM2---
x22 =
-3、9056e+01 6、0942e+01 -2、1883e+01 1、2573e-04 3、6102e-04
norm22 =
3、0733e-02
3、 教材238页习题1;
M文件:c=[1 -2 -1];
A1=[-1 1 2;1 1 -1;1 2 0;0 1 1];
A2=[0 1 —1];
b1=[2 4 4 4];
b2=[1];
v1=[0 0 0];
[x,f,exitflag,output,lag]=linprog(c,A1,b1,A2,b2,v1)
lag、ineqlin
运行结果:〉> c81
Optimization terminated、
x =
7、9996e-01
1、6000e+00
5、9999e-01
f =
-3、0000e+00
exitflag =
1
output =
iterations: 6
algorithm: 'interior-point—legacy'
cgiterations: 0
message: ’Optimization terminated、'
constrviolation: 2、6423e—14
firstorderopt: 2、4779e—13
lag =
ineqlin: [4x1 double]
eqlin: 1、0000e+00
upper: [3x1 double]
lower: [3x1 double]
>>lag、ineqlin
ans =
1、0000e+00
1、1569e-14
1、7455e-13
1、8636e-14
由运行结果可知所求最大值为—3、0000e+00,起作用约束为第一个约束
4、 教材239页习题5。
M文件:c1=[-20 -30];
A=[1 2;5 4];
b=[20 70];
v1=[0 0];
[x1,f1,exitflag1,output1,lag1]=linprog(c1,A,b,[],[],v1)
y1=lag1、ineqlin
运行结果:〉> c85
Optimization terminated、
x1 =
1、0000e+01
5、0000e+00
f1 =
-3、5000e+02
exitflag1 =
1
output1 =
iterations: 5
algorithm: ’interior-point—legacy'
cgiterations: 0
message: ’Optimization terminated、'
constrviolation: 0
firstorderopt: 6、4731e—11
lag1 =
ineqlin: [2x1 double]
eqlin: [0x1 double]
upper: [2x1 double]
lower: [2x1 double]
y1 =
1、1667e+01
1、6667e+00
由运行结果可知以甲乙分别20、30元出售时安排甲乙分别生产10、5件可使收入最大,当A、B增加1公斤时收入分别增加11、667、1、6667,考虑成本后由11、667-6=5、667,1、6667-2=-1、6667<0可知每进1公斤买A原料可增加利润5、667元,买B原料则会减少利润-1、667元,故应全部买进A原料。
展开阅读全文