《matlab进退法-切线法求极值(共4页).docx》由会员分享,可在线阅读,更多相关《matlab进退法-切线法求极值(共4页).docx(4页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上7、用进退法求极值:;a=0.5,h=0.5,=0.01。%建立函数f(x)。function y=f(x)if nargin=1 y=x2-10*x-36;endend%建立用进退法求解极值程序。function y=jintui(a,h,e)a=input(enter the value of a:);h=input(enter the value of h:);e=input(enter the value of e:);m=1;n=1;x0=a;x1=x0+h;while abs(x1-x0)e if f(x0)f(x1) x0=x1; x1=x0+h*2m;
2、 m=m+1; else x1=x0; x0=x1-h/2n; n=n+1; endend x=(x0+x1)/2;y=f(x);end运行结果如下:enter the value of a:0.5enter the value of h:0.5enter the value of e:0.01ans = -59.99228、用切线法求极值:;%建立原函数;function y=f(x)if nargin=1 y=x2-16*x+35;endend%建立一阶导函数;function yd=fd(x)if nargin=1 yd=2*x-16;endend%建立二阶导函数;function yd
3、d=fdd(x)if nargin=1 ydd=2;endend%切线法求极值函数;%x0为给定初值,n为计算次数;%f(x)、fd(x)、fdd(x)分别为原函数,一阶导函数,二阶导函数;function xj,yj=qiexian(x0,n)x0=input(Please enter the value of x0:);n=input(Please enter the value of n:);for k=1:1:n x1=x0-fd(x0)/fdd(x0); x0=x1;endxj=x0;yj=f(xj);end此题中,自定义初值x0=2,n=30;xj =8,yj =-29;专心-专注-专业