您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > matlab函数求极值
函数的极值1、一元函数的极值函数命令:fminbnd调用格式:[x,feval,exitflag,output]=fminbnd(fun,x1,x2,options)%求fun在区间(x1,x2)上的极值.返回值:x:函数fun在(x1,x2)内的极值点feval:求得函数的极值exitflag:exitflag0,函数收敛于解x处exitflag=0,已达最大迭代次数exiflag0,函数在计算区间内不收敛.|)sin(|)(xexy),(22例1:求函数在上的极小值.fun=inline('(x+pi)*exp(abs(sin(x+pi)))')[x,feval,exitflag,output]=fminbnd(fun,-pi/2,pi/2)fun=Inlinefunction:fun(x)=(x+pi)*exp(abs(sin(x+pi)))x=-1.2999e-005feval=3.1416exitflag=1output=iterations:21funcCount:22algorithm:'goldensectionsearch,parabolicinterpolation'message:[1x112char]xx=-pi/2:pi/200:pi/2;yxx=(xx+pi).*exp(abs(sin(xx+pi)));plot(xx,yxx)xlabel('x'),gridon%可以用命令[xx,yy]=ginput(1)从局部图上取出极值点及相应函数值-2-1.5-1-0.500.511.52345678910111213x例2:求解函数humps的极小值.typehumps%humps是一个Matlab提供的M函数文件function[out1,out2]=humps(x)%HUMPSAfunctionusedbyQUADDEMO,ZERODEMOandFPLOTDEMO.%Y=HUMPS(X)isafunctionwithstrongmaximanearx=.3%andx=.9.%%[X,Y]=HUMPS(X)alsoreturnsX.Withnoinputarguments,%HUMPSusesX=0:.05:1.%%Example:%plot(humps)%%SeeQUADDEMO,ZERODEMOandFPLOTDEMO.%Copyright1984-2002TheMathWorks,Inc.%$Revision:5.8$$Date:2002/04/1503:34:07$ifnargin==0,x=0:.05:1;endy=1./((x-.3).^2+.01)+1./((x-.9).^2+.04)-6;ifnargout==2,out1=x;out2=y;elseout1=y;end[x,y]=fminbnd(@humps,0.5,0.8)x=0.6370y=11.2528xx=0:0.001:2;yy=humps(xx);plot(xx,yy)00.20.40.60.811.21.41.61.82-20020406080100例3:求xxy在(0,1)内的极小值.typemyfunmin1%显示M文件内容functionf=myfunmin1(x)f=x.^x;[x,y]=fminbnd(@myfunmin1,0,1)x=0.3679y=0.6922xx=0:0.001:1;yy=myfunmin1(xx);plot(xx,yy)[x,y]=fminbnd('x.^x',0,1)x=0.3679y=0.692200.10.20.30.40.50.60.70.80.910.650.70.750.80.850.90.9512、多元函数的极值函数命令:fminsearch调用格式:[x,feval,exitflag,output]=fminsearch(fun,x0,optipons)%求在x0附近的极值例4:求2221100)()(),(xxyyxf的极小值.typemyfunmin2functionf=myfunmin2(v)x=v(1);y=v(2);f=100*(y-x.^2).^2+(1-x).^2;[sx,sfeval]=fminsearch(@myfunmin2,[11])sx=11sfeval=0x0=[-5,-2,2,5;-5,-2,2,5][sx,sfeval]=fminsearch(@myfunmin2,x0)x0=-5-225-5-225sx=1.0000-0.68970.41518.08861.0000-1.91684.96437.8004sfeval=2.4112e-010disp([myfunmin2(sx(:,1)),myfunmin2(sx(:,2)),myfunmin2(sx(:,3)),myfunmin2(sx(:,4))])1.0e+005*0.00000.00580.02303.32113、函数零点函数命令:fzero求一元函数的零点调用格式:[x,feval,exitflag,output]=fzero(fun,x0,options)例5:求5.0)(sin)(1.02tettft的零点。(1)采用符号计算S=solve('sin(t)^2*exp(-0.1*t)-0.5*abs(t)','t')S=0.y=inline('sin(t).^2.*exp(-0.1*t)-0.5*abs(t)','t');t=-10:0.01:10;Y=y(t);plot(t,Y,'r');%r用红色作图holdon%在原来基础上作图plot(t,zeros(size(t)),'k');%y横为0,黑色xlabel('t');ylabel('y(t)')holdoff(2)数值法求解zoomon%获局部放大图[tt,yy]=ginput(5);zoomoff%用鼠标获5个零点燃猜测值ttyytt=-2.0046-0.5300-0.11520.62211.6820yy=-0.0088-0.0088-0.0088-0.0088-0.0088[t1,y1]=fzero(y,-0.1)-0.1附近找t1=-0.5198y1=0[t2,y2]=fzero(y,2)t2=1.6738y2=2.2204e-016-10-8-6-4-20246810-5-4-3-2-101ty(t)非线性方程组的求解:fsolvesolve代数方程解语法:[x,fval]=fsolve(fun,x0)例6:求解非线性方程组:00042xyzyzxezyxxsinfunctionf=myfunsolve1(v)%v是一个数组x=v(1);y=v(2);z=v(3);f(1)=sin(x)+y+z^2.*exp(x)-4;f(2)=x+y*z;f(3)=x*y*z;f=fsolve(@myfunsolve1,[1,1,1])Optimizationterminated:relativefunctionvaluechangingbylessthanmax(options.TolFun^2,eps)andsum-of-squaresoffunctionvaluesislessthansqrt(options.TolFun).f=0.0005-0.00021.9995yy=myfunsolve1(f)yy=1.0e-006*-0.26360.1200-0.2131例7:求一个3阶方阵A,使A^3=[123;456;789]functionF=myfsolve2(A)F=A^3-[123;456;789];A=fsolve(@myfsolve2,ones(3))%ones(3)初始矩阵,直全为1myfsolve2(A)4、数值积分函数命令:quadquadlodbquad调用格式:q1=quad(fun,a,b,tol)%采用自适应Simpson算法计算积分q1=quadl(fun,a,b,tol)%采用自适应Lobatto算法计算积分q2=dblquad(fun,xmin,xmax,ymin,ymax,tol)%二重积分q3=triplequad(fun,xmin,xmax,ymin,ymax,zmin,zmax,tol)%三重积分例1:求I=102dxex.(1)利用quad,quadl计算formatlongI=quad('exp(-x.^2)',0,1,1e-8)Il=quadl('exp(-x.^2)',0,1,1e-8)I=0.74682413285445Il=0.74682413398845(2)采用编程计算:functionI=myquad1(a,b,n)x=linspace(a,b,n);%把ab区间平均分成n等份y=exp(-x.^2)*(b-a)/n;%高×底=每个取边梯形的面积I=sum(y);I1=myquad1(0,1,10000)I2=myquad1(0,1,100000)I1=0.74681784375801I2=0.74682350396218例2:计算1021dyxdxsy利用dblquads1=dblquad(‘x.^y’,0,1,1,2),%被积函数写成数组的形式db_(double)二重积分,即两次积分s1=0.40546626724351s2=dblquad(@(x,y)x.^y,0,1,1,2)%采用匿名函数表示被积函数xy的区间s2=0.40546626724351s3=dblquad(inline('x.^y'),0,1,1,2)s3=0.405466267243511211dxxdxxx3212dxxx3212求s1=,s2=,s3=例3:symsxx符号变量f1=1/(1+x^2);f2=1/(x^2+2*x+3);f3=1/(x^2+2*x-3);s1=int(f1,1,inf)1到正无穷s2=int(f2,-inf,inf)int符号积分s3=int(f3,-inf,inf)s1=1/4*pis2=1/2*pi*2^(1/2)s3=NaN不确定的结果s4=quad('1./(1+x.^2)',1,inf)%quad只能计算闭型积分Warning:InfiniteorNot-a-Numberfunctionvalueencountered.Inquadat95s4=NaNs5=quad('1./(1+x.^2)',1,10)s5=0.6857s6=int(f1,1,10')s6=atan(10)-1/4*pivpa(s6,10)ans=.6857295105
本文标题:matlab函数求极值
链接地址:https://www.777doc.com/doc-6420209 .html