优化方法
期末上机大作业
姓 名:李岚松
学 部:电信学部电气工程
学 号
2016 年 11 月 9 日
1°最速下降法
//最速下降法主函数//
function lls=di1titidu(x)=di1titidu(x)
x0=x; eps=1e-4; k=0; g0=g(x0); s0=4; k=0; g0=g(x0); s0=-g0;
while (k>=0)
if norm(g0)
break;
else
lanmed=10;c=0.1;i=0; lanmed=10;c=0.1;i=0;
while i>=0&&i<100 100
x=x0+lanmed*s0;
if f(x)>(f(x0)+c*lanmed*g0'*s0) f(x)>(f(x0)+c*lanmed*g0'*s0)
lanmed=lanmed/2; lanmed=lanmed/2;
i=i+1;
else
break;
end
end
x=x0+lanmed*s0;
x0=x;
g0=g(x);
s0=-g0;
k=k+1;
end
end
lls=x;
sll=f(x);
fprintf('after %d iterations,obtain the optimal solution.iterations,obtain the optimal solution.\n\\nThe optimal
solution is %f.\n\n The optimal "x" is "ans".n The optimal "x" is "ans".\n',k,sll);
//f(x)//
function f=f(x)
x1=[1 0]*x;
x2=[0 1]*x;
f=(1-x1)^2+100*(x2-x1^2)^2;
//梯度函数//
function g=g(x)
x1=[1 0]*x;
x2=[0 1]*x;
g=[-2*(1-x1)-400*x1*(x2-x1^2);
200*(x2-x1^2)];
//运行过程//
>> x=[0;0]
x =
0
0
>> di1titidu(x)
after 4574 iterations,obtain the optimal solution.
The optimal solution is 0.000000.
The optimal "x" is "ans".
ans =
1.0001
1.0002
2 °牛顿法
//牛顿法主函数//
function s=di2tinewton(x)
x0=x; eps=1e-4; k=0;
g0=g(x0); h0=h(x0);s0=-inv(h0)*g0;
while (k>=0&&k<1000)
if norm(g0)
break;
else
x=x0+s0;
x0=x;
g0=g(x);
h0=h(x);
s0=-in