1.8函数与极限例1.8.1求2uy、1xu的复合函数.symsxu;y=u^2;u=x-1;compose(y,u)ans=(x-1)^2例1.8.2求11xxy的反函数.symsx;y=(x-1)/(x+1);finverse(y)ans=-(x+1)/(x-1)例1.8.3求30、45、60的正切值.x=[304560];tan(x*pi/180)ans=0.57741.00001.7321在MATLAB中,求函数的极限主要有以下命令:limit(f,x,a))(limxfaxlimit(f,x,a,’left’)左极限)(limxfaxlimit(f,x,a,’right’)右极限)(limxfax在以上命令中,a可为无穷大(inf)、正无穷大(+inf)或负无穷大(-inf).例1.8.4求极限2031lim1xxx.symsx;f=(3*x+1)/(x^2-1);limit(f,x,0)ans=-1例1.8.5求极限11lim21xxx.symsx;limit((x^2-1)/(x-1),x,1)%0/0型极限ans=2例1.8.6求极限xxtanlim2.symsx;limit(tan(x),x,pi/2,'right')ans=-Inf例1.8.7求极限xxx)11(lim.symsx;limit((1+1/x)^x,x,inf)ans=exp(1)例1.8.8求二元函数的极限2101limxyxyxy.symsxy;f=(x*y+1)/(x+y^2);limit(limit(f,x,1),y,0)ans=11.9导数与偏导数在MATLAB中,求函数的导数与偏导数的命令主要有:diff(f,x)一阶导数)(xf、偏导数xfdiff(f,x,n)n阶导数)()(xfn例1.9.1求函数2()fxaxbxc的导数.symsabcx;f=a*x^2+b*x+c;diff(f,x)%可不指定变量ans=2*a*x+b例1.9.2设xxy,求y和(5)y.symsx;y=x^x;%幂指函数diff(y,x,3)ans=x^x*(log(x)+1)^3+3*x^x*(log(x)+1)/x-x^x/x^2diff(y,x,5)ans=x^x*(log(x)+1)^5+10*x^x*(log(x)+1)^3/x+15*x^x*(log(x)+1)/x^2-10*x^x*(log(x)+1)^2/x^2-10*x^x/x^3+10*x^x*(log(x)+1)/x^3-6*x^x/x^4例1.9.3设22yxxyz,求z的偏导数.symsxy;z=x*y/(x^2+y^2);zx=diff(z,x)%z对x的偏导数zx=y/(x^2+y^2)-2*x^2*y/(x^2+y^2)^2zy=diff(z,y)%z对y的偏导数zy=x/(x^2+y^2)-2*x*y^2/(x^2+y^2)^2例1.9.4设xyzsin,求z的二阶偏导数.symsxy;z=sin(x*y);zxx=diff(z,x,2)%z对x的二阶偏导数zxx=-sin(x*y)*y^2zyy=diff(z,y,2)%z对y的二阶偏导数zyy=-sin(x*y)*x^2zxy=diff(diff(z,x),y)%z先对x后对y的二阶偏导数zxy=-sin(x*y)*x*y+cos(x*y)zyx=diff(diff(z,y),x)%z先对y后对x的二阶偏导数zyx=-sin(x*y)*x*y+cos(x*y)例1.9.5设方程0xyexy确定的隐函数为()yyx,求y.symsxy;f=exp(x*y)-x+y;-diff(f,x)/diff(f,y)ans=(-y*exp(x*y)+1)/(x*exp(x*y)+1)例1.9.6设椭圆参数方程cos(02)sinxattybt确定的函数为()yyx,求dydx,22dydx.symsabt;x=a*cos(t);y=b*sin(t);f1=diff(y,t)/diff(x,t)f1=-b*cos(t)/a/sin(t)f2=diff(f1,t)/diff(x,t)f2=-(b/a+b*cos(t)^2/a/sin(t)^2)/a/sin(t)注dtdxdtdydxdy,dtdxdxdydtddxdtdxdydtddxdydxddxyd22.1.10微分方程(组)在MATLAB中,求微分方程的解析解的命令为:dsolve('e','c','x')其中e为微分方程,c为初值条件,x为变量(必须指定,否则默认t为变量)。求微分方程组的解析解的命令为:dsolve('e1','e2','c1','c2','x1','x2')其中e1,e2为微分方程,c1,c2为初值条件,x1,x2为变量(必须指定)。求微分方程),(yxfy的数值解的命令为:[x,y]=ode45('M',[ab],y0)其中M为定义函数f的M文件名,[a,b]为x的初值和终值,y0为y的初值。求微分方程组),,(),,(21122111yyxfyyyxfy的数值解的命令为:[x,y]=ode45('M',[ab],[cd])其中M为定义函数f1,f2的M文件名,[a,b]为x的初值和终值,c,d分别为y1,y2的初值。例1.10.1解一阶微分方程12ydxdy.dsolve('Dy=y^2+1','x')%Dy为y的导数ans=tan(x+C1)如不指定变量,结果如下:dsolve('Dy=y^2+1')%未指定变量ans=tan(t+C1)例1.10.2解二阶微分方程xeyyyxsin2596.dsolve('D2y-6*Dy+9*y=25*exp(x)*sin(x)','x')%D2y为y的二阶导数ans=exp(3*x)*C2+exp(3*x)*x*C1+exp(x)*(4*cos(x)+3*sin(x))例1.10.3求微分方程初值问题2)1(yxydxdy的解析解.dsolve('Dy=y/x','y(1)=2','x')ans=2*x例1.10.4求微分方程初值问题xyyeyyy59)0(3)0(834的解析解.dsolve('D2y-4*Dy+3*y=8*exp(5*x)','y(0)=3','Dy(0)=9','x')ans=exp(3*x)+exp(x)+exp(5*x)例1.10.5求微分方程组232(0)2(0)1dyyxdtdxyxdtxy的解析解.dsolve('Dy=2*y+3*x','Dx=y-2*x','x(0)=2','y(0)=1','t')ans=x:[1x1sym]y:[1x1sym]为得到解析解,可对上述命令作如下修改:jiexi=dsolve('Dy=2*y+3*x','Dx=y-2*x','x(0)=2','y(0)=1','t');jiexi.x,jiexi.yans=1/3*(1/2+4/7*7^(1/2))*7^(1/2)*exp(7^(1/2)*t)-1/3*(1/2-4/7*7^(1/2))*7^(1/2)*exp(-7^(1/2)*t)-2/3*(1/2+4/7*7^(1/2))*exp(7^(1/2)*t)-2/3*(1/2-4/7*7^(1/2))*exp(-7^(1/2)*t)ans=(1/2+4/7*7^(1/2))*exp(7^(1/2)*t)+(1/2-4/7*7^(1/2))*exp(-7^(1/2)*t)例1.10.6求微分方程初值问题1)0(2yyy的解析解和数值解.1.解析解dsolve('Dy=-y^2','y(0)=1','x')ans=1/(x+1)x=0:.01:1;y=1./(x+1);plot(x,y)%绘制解析解图像00.10.20.30.40.50.60.70.80.910.50.550.60.650.70.750.80.850.90.951xy图1.3微分方程解析解图像2.数值解编写M文件func.m:functionmyfunc=func(x,y)myfunc=-y^2;执行:[x,y]=ode45('func',[01],1);plot(x,y,'o')%绘制数值解图像00.10.20.30.40.50.60.70.80.910.50.550.60.650.70.750.80.850.90.951xy图1.4微分方程数值解图像例1.10.7求微分方程初值问题0)0()0()()()1(5)1()()1(52222yxytytxdtdyxytxdtdx的数值解.编写M文件func.m:functiondy=func(t,y)dy=zeros(2,1);dy(1)=5*(1-y(1))/sqrt((1-y(1))^2+(t-y(2))^2);dy(2)=5*(t-y(2))/sqrt((1-y(1))^2+(t-y(2))^2);执行:[t,y]=ode45('func',[02],[00]);plot(y(:,1),y(:,2),'*')%绘制数值解图像00.20.40.60.811.21.400.20.40.60.811.21.41.61.82xy图1.5微分方程初值问题的数值解