好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

2016年大连理工大学优化方法上机大作业.docx

37页
  • 卖家[上传人]:汽***
  • 文档编号:519616581
  • 上传时间:2023-12-12
  • 文档格式:DOCX
  • 文档大小:294.49KB
  • / 37 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 标准文案大全专业班级学号姓名2016 年大连理工大学优化方法上机大作业上机大作业 1 :1.最速下降法function f = fun(x)f = (1-x(1)『2 + 100*(x(2)-x(1)A2F2;endfunction g = grad(x)g = zeros(2,1);g⑴=2*(x(1)-1)+400*x(1)*(x(1)A2-x(2));g(2) = 200*(x(2)-x⑴人2);endfunction x_star = steepest(x0,eps)gk = grad(x0);res = norm(gk);k = 0;while res > eps && k<=1000dk = -gk;ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + 0.1*ak*slopeak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;x0 = xk;gk = grad(xk);res = norm(gk);fprintf( '--The %d-th iter, the residual is %f\n' ,k,res);标准文案endx_star = xk;end> > clear> > x0=[0,0]';> > eps=1e-4;> > x=steepest(x0,eps)大全标准文案2. 牛顿法:function f = fun(x)f = (1-x(1)『2 + 100*(x(2)-x(1)A2)A2;endfunction g = grad2(x)g = zeros(2,2);g(1,1)=2+400*(3*x(1)A2-x(2));大全标准文案g(1,2)=-400*x(1);g(2,1)=-400*x(1);g(2,2)=200;endfunction g = grad(x)g = zeros(2,1);g⑴=2*(x(1)-1)+400*x(1)*(x(1)A2-x(2));g(2) = 200*(x(2)-x⑴人2);end function x_star = newton(x0,eps)gk = grad(x0);bk = [grad2(x0)F(-1);res = norm(gk);k = 0;while res > eps && k<=1000dk=-bk*gk;xk=x0+dk;k = k+1;x0 = xk;gk = grad(xk);bk = [grad2(xk)F(-1);res = norm(gk);,k,res);fprintf( '--The %d-th iter, the residual is %f\n'endx_star = xk;end>> clear>> x0=[0,0]';> > eps=1e-4;> > x1=newton(x0,eps)> -The 1-th iter, the residual is 447.213595> -The 2-th iter, the residual is 0.000000x1 =1.00001.0000大全标准文案3.BFGS 法:大全标准文案function f = fun(x)f = (1-x(1)『2 + 100*(x(2)-x(1)A2F2;endfunction g = grad(x)g = zeros(2,1);g⑴=2*(x(1)-1)+400*x(1)*(x(1)A2-x(2));g(2) = 200*(x(2)-x⑴人2);endfunction x_star = bfgs(x0,eps)g0 = grad(x0);gk=g0;res = norm(gk);Hk=eye(2);k = 0;while res > eps && k<=1000dk = -Hk*gk;ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + 0.1*ak*slopeak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;fa0=xk-x0;x0 = xk;go=gk;gk = grad(xk);y0=gk-g0;Hk=((eye(2)-fa0*(y0)')/((fa0)'*(y0)))*((eye(2)-(y0)*(fa0)')/((fa0)'*(y0)))+(fa0*(fa0)')/((fa0)'*(y0));res = norm(gk);fprintf( '--The %d-th iter, the residual is %f\n' ,k,res);大全标准文案endx_star = xk;End>> clear>> x0=[0,0]';>> eps=1e-4;>> x=bfgs(x0,eps)大全标准文案大全4. 共轭梯度法:function f = fun(x)f = (1-x(1)『2 + 100*(x(2)-x(1)A2F2;endfunction g = grad(x)g = zeros(2,1);g⑴=2*(x(1)-1)+400*x(1)*(x(1)A2-x(2));g(2) = 200*(x(2)-x⑴人2);endfunction x_star =CG(x0,eps)gk = grad(x0);res = norm(gk);k = 0;dk = -gk;while res > eps && k<=1000ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + 0.1*ak*slopeak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;x0 = xk;g0=gk;gk = grad(xk);res = norm(gk);p=(gk/g0『2;dk1=dk;dk=-gk+p*dk1;,k,res);fprintf( '--The %d-th iter, the residual is %f\n'endx_star = xk;end>> clear>> x0=[0,0]';>> eps=1e-4;>> x=CG(x0,eps)标准文案上机大作业 2 :MATLAB R2D13a —OX大全标准文案大全Current FolidiW ®Name。

      白 rrtE电巾 WindowWorkspace» a * mdikib2013 ► bin * 优防法♦审 New to MATIAS? Watdi ihii YHcol we 皿01亦£. or readdlm-feih.fTiEEpdFEEconitFfl;ns.!mDell■修function f= obj(x)f=4*x(1)-x(2『2-12;end,kOTOing CTii・riw ow【i¥,w* (fl the opt point the iteration numberthe ^rslue 日f conjETrains;ans3. 374fi«-0Bthe opt pcint3:L0Q45乐 8080fain-31.9727function [h,g] =constrains(x)h=x(1)A2+x(2)A2-25;g=zeros(3,1);N«me ■ Value3 际皿 -31.9727H k 2Bi 11.0045.4 B9K]田 iopoiCommand History年=U*s, Q]:工 FVAL.li]-«lMin(x_»l):工-FVALr k】F】nain,x= tl -jta f sin- k2 - aAnain

      ]:Zx. f BITi. qg(1)=-10*x(1)+x(1)A2-10*x(2)+x(2)A2+34;g(2)=-x(1);g(3)=-x(2);endfunction f=alobj(x) % 拉格朗日增广函数%N_equ 等式约束个数 ?%N_inequ 不等式约束个数N_equ=1;N_inequ=3;global r_al pena; %全局变量h_equ=0;h_inequ=0;[h,g]=constrains(x);% 等式约束部分 ?for i=1:N_equh_equ=h_equ+h(i)*r_al(i)+(pena/2)*h(i).A2;end% 不等式约束部分for i=1:N_inequh_inequ=h_inequ+(0.5/pena)*(max(0,(r_al(i)+pena*g(i))).A2-r_al(i).A2);end% 拉格朗日增广函数值f=obj(x)+h_equ+h_inequ;function f=compare(x)global r_al pena N_equ N_inequ;N_equ=1;N_inequ=3;h_inequ=zeros(3,1);[h,g]=constrains(x);% 等式部分for i=1:1h_equ=abs(h(i));end% 不等式部分for i=1:3h_inequ=abs(max(g(i),-r_al(i+1)/pena));endh1 = max(h_inequ);f= max(abs(h_equ),h1); %sqrt(h_equ+h_inequ);标准文案。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.