一.相关函数:%符号积分int(f,v)int(f,v,a,b)%数值积分trapz(x,y)%梯形法沿列方向求函数Y关于自变量X的积分cumtrapz(x,y)%梯形法沿列方向求函数Y关于自变量X的累计积分quad(fun,a,b,tol)%采用递推自适应Simpson法计算积分quad1(fun,a,b,tol)%采用递推自适应Lobatto法求数值积分dbquad(fun,xmin,xmax,ymin,ymax,zmin,zmax,tol)%二重(闭型)数值积分指令triplequad(fun,xmin,xmax,ymin,ymax,zmin,zmax,tol)%三重(闭型)数值积分指令二.示例:例1:计算f(t)=exp(-t^2)在[0,1]上的定积分本例演示:计算定积分常用方法symsxint(exp(-x^2),0,1)ans=1/2*erf(1)*pi^(1/2)%erf为误差函数vpa(int(exp(-x^2),0,1))ans=.7468241328124270d=0.001;x=0:d:1;d*trapz(exp(-x.^2))ans=0.7468quad('exp(-x.^2)',0,1,1e-8)ans=0.7468例2:计算f(t)=1/log(t)在[0,x],0x1上的积分注意:被积函数于x=0无义,在x--1^-处为负无穷本例演示:用特殊函数表示的积分结果,如何用mfun指令(1)symstxft=1/log(t);sx=int(ft,t,0,x)sx=-Ei(1,-log(x))%完全椭圆函数(2)x=0.5:0.1:0.9sx_n=-mfun('Ei',1,-log(x))x=0.50000.60000.70000.80000.9000sx_n=-0.3787-0.5469-0.7809-1.1340-1.7758(3)%图示被函数和积分函数clfezplot('1/log(t)',[0.1,0.9])gridonholdonplot(x,sx_n,'LineWidth',3)Char1='1/ln(t)';Char2='{int_0^x}1/ln(t)dt';title([Char1,'and',Char2])legend(Char1,Char2,'Location','SouthWest')例3:计算f(t)=exp(-sin(t))在[0,4]上的定积分注意:本题被函数之原函数无封闭解析表达式,符号计算无法解题!本例演示:符号计算有限性(1)符号计算解法symstxft=exp(-sin(t))sx=int(ft,t,0,4)ft=exp(-sin(t))Warning:Explicitintegralcouldnotbefound.Insym.intat58sx=int(exp(-sin(t)),t=0..4)(2)数值计算解法dt=0.05;%采样间隔t=0:dt:4;%数值计算适合于有限区间上,取有限个采样点Ft=exp(-sin(t));Sx=dt*cumtrapz(Ft);%计算区间内曲线下图形面积,为小矩形面积累加得Sx(end)%所求定积分值%图示plot(t,Ft,'*r','MarkerSize',4)holdonplot(t,Sx,'.k','MarkerSize',15)holdoffxlabel('x')legend('Ft','Sx')ans=3.0632例4:绘制积分图形,y=2/3*exp(-t/2)*cos(sqrt(3)/2*t);积分s(x)=int(y,t,0,x)于[0,4*pi]上symsttaoy=2/3*exp(-t/2)*cos(sqrt(3)/2*t);s=subs(int(y,t,0,tao),tao,t);%获得积分函数subplot(2,1,1)%ezplot(y,[0,4*pi]),ylim([-0.2,0.7])%单变量符号函数可视化,多变量用ezsurfgridonsubplot(2,1,2)ezplot(s,[0,4*pi])gridontitle('s=inty(t)dt')int的积分可以是定积分,也可以是不定积分(即有没有积分上下限都可以积)可以得到解析的解,比如你对x^2积分,得到的结果是1/3*x^3,这是通过解析的方法来解的。如果int(x^2,x,1,2)得到的结果是7/3quad是数值积分,它只能是定积分(就是有积分上下限的积分),它是通过simpson数值积分来求得的(并不是通过解析的方法得到解析解,再将上下限代入,而是用小梯形的面积求和得到的)。如果f=inline('x.^2');quad(f,1,2)得到的结果是2.333333,这个数并不是7/3%%符号变量与符号表达式%%%%%%%%%%%%%%%%%%%%%%%%%%%%1.符号变量与符号表达式%%%%%%%%%%%%%%%%%%%%%%%%%%%clearall;clc;closeall;%f=sym('sin(x)+5x')%f——符号变量名%sin(x)+5x——符号表达式%''——符号标识%符号表达式一定要用''单引号括起来matlab才能识别%''的内容可以是符号表达式,也可以是符号方程。%例:%f1=sym('a*x^2+b*x+c')——二次三项式%f2=sym('a*x^2+b*x+c=0')——方程%f3=sym('Dy+y^2=1')——微分方程%符号表达式或符号方程可以赋给符号变量,以后调用方便;也可以不赋给符号变量直接参与运算%syms命令用来建立多个符号量,一般调用格式为:%syms变量1变量2...变量n%%符号矩阵的创建%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%2.符号矩阵的创建%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%数值矩阵A=[1,2;3,4]%A=[a,b;c,d]——不识别%@1.用matlab函数sym创建矩阵(symbolic的缩写)%命令格式:A=sym('[]')%※符号矩阵内容同数值矩阵%※需用sym指令定义%※需用''标识%例如:A=sym('[a,2*b;3*a,0]')%A=%[a,2*b]%[3*a,0]%这就完成了一个符号矩阵的创建。%注意:符号矩阵的每一行的两端都有方括号,这是与matlab数值矩阵的一个重要区别。%@2.用字符串直接创建矩阵(这种方法创建的没有什么用处)%※模仿matlab数值矩阵的创建方法%※需保证同一列中各元素字符串有相同的长度。%例:A=['[a,2*b]';'[3*a,0]']%A=%[a,2*b]%[3*a,0]%@3.符号矩阵的修改%a.直接修改%可用光标键找到所要修改的矩阵,直接修改%b.指令修改%※用A1=sym(A,*,*,'new')来修改。这个经过测试,不能运行%※用A1=subs(A,'new','old')来修改%%例如:A=[a,2*b]%[3*a,0]A=sym('[a,2*b;3*a,0]')%A1=sym(A,2,2,'4*b')%%等效于A(2,2)='4*b';%A1=[a,2*b]%[3*a,4*b]A1=subs(A,'0','4*b')A2=subs(A1,'c','b')%A2=[a,2*c]%[3*a,4*c]%@4.符号矩阵与数值矩阵的转换%※将数值矩阵转化为符号矩阵%函数调用格式:sym(A)A=[1/3,2.5;1/0.7,2/5]%A=%0.33332.5000%1.42860.4000B=sym(A)%ans=%[1/3,5/2]%[10/7,2/5]%※将符号矩阵转化为数值矩阵%函数调用格式:numeric(A)%B=%[1/3,5/2]%[10/7,2/5]%numeric(B)这个函数不存在了VPA(B,4)%发现这个函数可用%R=VPA(S)numericallyevaluateseachelementofthedoublematrix%SusingvariableprecisionfloatingpointarithmeticwithDdecimal%digitaccuracy,whereDisthecurrentsettingofDIGITS.%TheresultingRisaSYM.%%VPA(S,D)usesDdigits,insteadofthecurrentsettingofDIGITS.%DisanintegerortheSYMrepresentationofanumber.%ans=%[.3333,2.500]%[1.429,.4000]%%符号运算%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.符号运算%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%例1:f=sym('2*x^2+3*x-5');g=sym('x^2+x-7');h=f+g%h=%3*x^2+4*x-12%例2:f=sym('cos(x)');g=sym('sin(2*x)');f/g+f*g%ans=%cos(x)/sin(2*x)+cos(x)*sin(2*x)%%查找符号变量%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%4.查找符号变量%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%findsym(expr)按字母顺序列出符号表达式expr中的所有符号变量%%findsym(expr,N)列出expr中离x最近的N个符号变量%若表达式中有两个符号变量与x的距离相等,则ASCII码大者优先。%※常量pi,i,j不作为符号变量%例:f=sym('2*w-3*y+z^2+5*a');findsym(f)%ans=%a,w,y,zfindsym(f,3)%ans=%y,w,zfindsym(f,1)%ans=%y%%计算极限%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5.计算极限%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%limit(f,x,a):计算f(x)当x趋向于a的极限%limit(f,a):当默认变量趋向于a时的极限%limit(f):计算a=0时的极限%limit(f,x,a,'right'):计算右极限%limit(f,x,a,'left'):计算左极限%例:计算symsxhn;L=limit((log(x+h)-log(x))/h,h,0)%L=%1/xM=limit((1-x/n)^n,n,inf)%M=%exp(-x)%%计算导数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%6.计算导数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%g=diff(f,v):求符号表达式f关于v的导数%g=diff(f):求符号表达式f关于默认变量的导数%g=diff(f,v,n):求f关于v的n阶导数symsx;f=sin(x)+3*x^2;g=diff(f,x)%g=%cos(x)+6*x%%计算积分%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%7.计算积分%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%int(f,v,a,b):计算定积分f(v)从a到b%int(f,a,b):计算关于默认变量的定积分%int(f,v):计算不定积分f(v)%int(f):计算关于默认变量的不定积分f=(x^2+1)/(x^2-2*x+2)^2;I=int(f,x)%I=%3/2*atan(x-1)+1/4*(2*x-6)/(x^2-2*x+2)K=int(exp(-x^2),x,0,inf)%K=%1/2*pi^(1/2)%%函数运算%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%8.函数运算%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%1.合并、化简、展开