1、梯 度 法 求 最 优 解 问 题姓 名:李刚 学号:2016210309 1、 所求问题 用梯度法求目标函数的无约束最优解,已知初始点,或2、Matlab编程实现建立了一个Stair(e).m的子函数来求解,e为函数的输入变量,即问题中的。只需要在Matlab“命令行窗口”去调用此函数即可,用或去代替Stair(e).m函数中的输入变量e即可。程序源码:function z =Stair(e)%输入变量e来代替求解精度问题,方便求解不同静精度问题syms x1 x2 a f=1.5*x12+0.5*x22-x1*x2-2*x1; %目标函数dx1=diff(f,x1); %对x1求偏导dx2
2、=diff(f,x2); %对x2求偏导g=dx1,dx2; %梯度 X=x1,x2; %建立坐标变量X0=-2,4; %初始点坐标 g0=subs(g,X,X0); %subs函数,用X0代替g函数中的XG=norm(g0); %梯度的模k=0; %迭代次数变量F=subs(f,X,X0); %subs函数,用X0代替f函数中的XF=double(F); %数据类型转换 fprintf(迭代次数k=%d;,k);fprintf(坐标值x1=%5.7f ,X0(1);fprintf(x2=%5.7f;,X0(2);%fprintf(坐标值 X=%d %dn,X0);fprintf(函数值F=%
3、5.7fnn,F)for i=1:20 %迭代求解 if (Ge) %判断是否满足精度要求 k=k+1;d=-g0/G; %确定搜索方向F=subs(f,X,X0); %计算F(x)目标函数的值fm=subs(f,X,X0+a*d); %求最优步长,以计算新的迭代点fmd=diff(fm); %求偏导a0=solve(fmd); %求出偏导函数的零解,求得最优步长X0=X0+a0*d; %求得新的迭代点g0=subs(g,X,X0); %计算新的梯度G=norm(g0); %计算新的梯度的模g0=double(g0); %数据类型转化G=double(G);X0=double(X0);F=do
4、uble(subs(f,X,X0);a0=double(a0); %结果输出fprintf(迭代次数k=%d;,k); fprintf(坐标值x1=%5.7f ,X0(1);fprintf(x2=%5.9f;,X0(2);fprintf(函数值F=%5.7fn,F);fprintf(梯度g(k)=%5.7f %5.7f;,g0);fprintf(梯度的模|g(k)|=%5.7f;,G);fprintf(步长a(k)=%5.7fnn,a0);endendz=X0 F;end 运行结果当时,命令窗口输入Stair(0.02),运行结果见下图:当时,命令窗口输入Stair(0.02),运行结果见下图
5、:具体见表格:()k0123X(k)-2,41.5294118, 2.235294110.9411765, 1.058823531.0103806, 1.02422145F(X(k)26-0.4705882-0.9896194-0.9997965g(k)0.3529412,0.7058824-0.235294,0.11764710.0069204,0.0138408|g(k)|0.78920050.26306680.0154745(k)3.94600231.31533410.0773726具体见表格:()k012345X(k)-241.5294118, 2.235294110.9411765,
6、 1.058823531.0103806, 1.024221450.9988466, 1.0011534031.0002035, 1.000474930F(X(k)26-0.4705882-0.9896194-0.9997965-0.9999960-0.9999999g(k)0.3529412,0.7058824-0.235294,0.11764710.0069204,0.0138408-0.0046136,0.00230680.0001357,0.0002714|g(k)|0.78920050.26306680.01547450.00515820.0003034(k)3.94600231.31533410.07737260.02579090.0015171