《matlab进退法-切线法求极值》由会员分享,可在线阅读,更多相关《matlab进退法-切线法求极值(4页珍藏版)》请在人人文库网上搜索。
1、7、用进退法求极值:;a=0.5,h=0.5,=0.01。%建立函数f(x)。function y=f(x)if nargin=1y=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)eif f(x0)f(x1)x0=x1;x1=x0+h*2m;m=m+1;elsex1=x0;x。
2、0=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=1y=x2-16*x+35;endend%建立一阶导函数;function yd=fd(x)if nargin=1yd=2*x-16;endend%建立二阶导函数;function ydd=fdd(x)if nargin=1ydd=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:nx1=x0-fd(x0)/fdd(x0);x0=x1;endxj=x0;yj=f(xj);end此题中,自定义初值x0=2,n=30;xj =8,yj =-29。