您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 机械/模具设计 > 用MATLAB实现最速下降法-牛顿法和共轭梯度法求解实例
实验的题目和要求一、所属课程名称:最优化方法二、实验日期:三、实验目的掌握最速下降法,牛顿法和共轭梯度法的算法思想,并能上机编程实现相应的算法。二、实验要求用MATLAB实现最速下降法,牛顿法和共轭梯度法求解实例。四、实验原理最速下降法是以负梯度方向最为下降方向的极小化算法,相邻两次的搜索方向是互相直交的。牛顿法是利用目标函数)(xf在迭代点kx处的Taylor展开式作为模型函数,并利用这个二次模型函数的极小点序列去逼近目标函数的极小点。共轭梯度法它的每一个搜索方向是互相共轭的,而这些搜索方向kd仅仅是负梯度方向kg与上一次接待的搜索方向1kd的组合。五.运行及结果如下:最速下降法:题目:f=(x-2)^2+(y-4)^2M文件:function[R,n]=steel(x0,y0,eps)symsx;symsy;f=(x-2)^2+(y-4)^2;v=[x,y];j=jacobian(f,v);T=[subs(j(1),x,x0),subs(j(2),y,y0)];temp=sqrt((T(1))^2+(T(2))^2);x1=x0;y1=y0;n=0;symskk;while(tempeps)d=-T;f1=x1+kk*d(1);f2=y1+kk*d(2);fT=[subs(j(1),x,f1),subs(j(2),y,f2)];fun=sqrt((fT(1))^2+(fT(2))^2);Mini=Gold(fun,0,1,0.00001);x0=x1+Mini*d(1);y0=y1+Mini*d(2);T=[subs(j(1),x,x0),subs(j(2),y,y0)];temp=sqrt((T(1))^2+(T(2))^2);x1=x0;y1=y0;n=n+1;endR=[x0,y0]调用黄金分割法:M文件:functionMini=Gold(f,a0,b0,eps)symsx;formatlong;symskk;u=a0+0.382*(b0-a0);v=a0+0.618*(b0-a0);k=0;a=a0;b=b0;array(k+1,1)=a;array(k+1,2)=b;while((b-a)/(b0-a0)=eps)Fu=subs(f,kk,u);Fv=subs(f,kk,v);if(Fu=Fv)b=v;v=u;u=a+0.382*(b-a);k=k+1;elseif(FuFv)a=u;u=v;v=a+0.618*(b-a);k=k+1;endarray(k+1,1)=a;array(k+1,2)=b;endMini=(a+b)/2;输入:[R,n]=steel(0,1,0.0001)R=1.999994136676423.99999120501463R=1.999994136676423.99999120501463n=1牛顿法:题目:f=(x-2)^2+(y-4)^2M文件:symsx1x2;f=(x1-2)^2+(x2-4)^2;v=[x1,x2];df=jacobian(f,v);df=df.';G=jacobian(df,v);epson=1e-12;x0=[0,0]';g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});G1=subs(G,{x1,x2},{x0(1,1),x0(2,1)});k=0;mul_count=0;sum_count=0;mul_count=mul_count+12;sum_count=sum_count+6;while(norm(g1)epson)p=-G1\g1;x0=x0+p;g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});G1=subs(G,{x1,x2},{x0(1,1),x0(2,1)});k=k+1;mul_count=mul_count+16;sum_count=sum_count+11;end;kx0mul_countsum_count结果::k=1x0=24mul_count=28sum_count=17共轭梯度法:题目:f=(x-2)^2+(y-4)^2M文件:functionf=conjugate_grad_2d(x0,t)x=x0;symsxiyiaf=(xi-2)^2+(yi-4)^2;fx=diff(f,xi);fy=diff(f,yi);fx=subs(fx,{xi,yi},x0);fy=subs(fy,{xi,yi},x0);fi=[fx,fy];count=0;whiledouble(sqrt(fx^2+fy^2))ts=-fi;ifcount=0s=-fi;elses=s1;endx=x+a*s;f=subs(f,{xi,yi},x);f1=diff(f);f1=solve(f1);iff1~=0ai=double(f1);elsebreakx,f=subs(f,{xi,yi},x),countendx=subs(x,a,ai);f=xi-xi^2+2*xi*yi+yi^2;fxi=diff(f,xi);fyi=diff(f,yi);fxi=subs(fxi,{xi,yi},x);fyi=subs(fyi,{xi,yi},x);fii=[fxi,fyi];d=(fxi^2+fyi^2)/(fx^2+fy^2);s1=-fii+d*s;count=count+1;fx=fxi;fy=fyi;endx,f=subs(f,{xi,yi},x),count输入:conjugate_grad_2d([0,0],0.0001)结果:x=0.24998825499785-0.24999998741273f=0.12499999986176count=10ans=0.12499999986176六、结论如下:最速下降法越接近极小值,步长越小,前进越慢。牛顿法要求二阶导数,计算量很大。共轭梯度法是介于最速下降和牛顿法之间的算法,克服了最速下降法的收敛速度慢的缺点,又避免了牛顿法的大计算量。
本文标题:用MATLAB实现最速下降法-牛顿法和共轭梯度法求解实例
链接地址:https://www.777doc.com/doc-4749089 .html