第五章 符号数学基础
Chapter 5:Foundation of Symbolic Mathematics
一,符号对象的创建(Creating a symbolic object)
1,创建符号变量和表达式(Creating a symbolic variable and expression)
创建符号变量和表达式的两个基本函数:sym,syms
*x=sym(‘x’) 创建一个符号变量x,可以是字符、字符串、表达式或字符表达式。
*syms用于方便地一次创建多个符号变量,调用格式为,syms a b c d,书写简洁意义清楚,建议使用。
例1:使用sym函数创建符号变量.
a=sym(‘a’)
b=sym( ‘hello’)
c=sym(( ‘(1+sqrt(5))/2’)
y=sym( ‘x^3+5*x^2+12*x+20’)
a =
a
b =
hello
C =
(1+sqrt(5))/2
Y =
x^3+5*x^2+12*x+20
例2:用syms函数创建符号变量。
syms a b c d
2,创建符号矩阵(Symbolic matrix Creating)
例1:创建一个循环矩阵。
syms a b c d
n=[a b c d;b c d a;c d a b;d a b c]
n =
[ a,b,c,d]
[ b,c,d,a]
[ c,d,a,b]
[ d,a,b,c]
例2:将3阶Hilbert矩阵转换为符号矩阵。
h=hilb(3)
h1=sym(h)
h =
1.0000 0.5000 0.3333
0.5000 0.3333 0.2500
0.3333 0.2500 0.2000
h1 =
[ 1,1/2,1/3]
[ 1/2,1/3,1/4]
[ 1/3,1/4,1/5]
注意符号矩阵于数值矩阵的区别。
默认符号变量(Implied symbolic variable)
在MATLAB的符号数学工具箱中,以最接近x的顺序排列默认自变量的顺序,可利用findsym函数对默认自变量进行查询。
例1,求符号函数在不同自变量情况下的结果。
创建符号变量x和n,建立函数f=xn,然后分别求f对x和f对n的导数.
syms x n
f=x^n
diff(f) % x作为自变量,求f对x的导数
diff(f,n) % n作为自变量,求f对n的导数
f =
x^n
ans =
x^n*n/x
ans =
x^n*log(x)
例2,查询符号函数中的默认自变量。
创建符号变量 a,b,n,x 和t,建立函数f=axn+bt,然后求f的默认自变量。
syms a b n t x
f=a*x^n+b*t
findsym(f,1)
findsym(f,2)
findsym(f,5) % f表达式中按最接近x顺序排列的5个默认自变量
findsym(f) % f表达式中按最接近字母顺序排列的全部自变量
f =
a*x^n+b*t
ans =
x
ans =
x,t
ans =
x,t,n,b,a
ans =
a,b,n,t,x
>>
符号表达式的化简和替换(simplifying and replacing of Symbolic xpressions)
符号数学工具箱提供的符号表达式的因式分解、展开、合并、化简、通分等操作:
1,符号表达式的化简(Simplifying of symbolic expression)
(1).因式分解(Factorization)
符号表达式的因式分解函数为 factor(S),可分解符号表达式S的各个元素。
例1,对表达式f=x9-1进行因式分解。
syms x
f=factor(x^9-1)
pretty(f)
f =
(x-1)*(x^2+x+1)*(x^6+x^3+1)
2 6 3
(x - 1) (x + x + 1) (x + x + 1)
例2:对大整数12345678901234567890进行因式分解。
factor(sym(‘12345678901234567890’))
ans =
(2)*(3)^2*(5)*(101)*(3803)*(3607)*(27961)*(3541)
(2)符号表达式的展开(Expanding of symbolic expressions)
符号表达式的展开函数为expand(S),此函数因数展开符号表达式S.
例,展开表达式f=(x+1)5和f=sin(x+y)
syms x y
f=(x+1)^5;
expand(f)
f=sin(x+y);
expand(f)
ans =
x^5+5*x^4+10*x^3+10*x^2+5*x+1
ans =
sin(x)*cos(y)+cos(x)*sin(y)
(3).符号表达式的同类项合并(Similar team merging for symbolic expression)
符号表达式的同类项合并函数为 collect(S,n),此函数将符号表达式中自变量的同次幂项的系数合并。
例:对于表达式f=x(x(x-6)+12)t,分别将自变量x和t的同类项合并。
syms x t
f=x*(x*(x-6)+12)*t;
collect(f)
collect(f,t)
ans =
t*x^3-6*t*x^2+12*t*x
ans =
x*(x*(x-6)+12)*t
COLLECT Collect coefficients.
COLLECT(S,v) regards each element of the symbolic matrix S as a
polynomial in v and rewrites S in terms of the powers of v.
COLLECT(S) uses the default variable determined by FINDSYM.
(4),符号表达式的化简(Simplifying of symbolic expression)
符号表达式的两个化简函数:simplify,simple,
simplify:化简函数,可用于化简各种表达式
例1:对表达式f=sin2(x)+cos2(x)进行化简.
syms x
f=sin(x)^2+cos(x)^2;
simplify(f)
ans =
1
[r,how]=simple(S) 函数可寻找符号表达式S的最简型,r为返回的简化形式,how为化简过程中使用的主要方法,simple函数综合使用了下列化简方法:
*simplify 函数对表达式进行化简
*radsimp 函数对含根式(surd)的表达式进行化简
*combine 函数对表达式中以求和、乘积、幂运算等形式出现的项进行合并
*collect 合并同类项
*factor 函数实现因式分解
*convert 函数完成表达式形式的转换
例2:最简表达式的获得。
syms x t
f=cos(x)^2-sin(x)^2;
[r,how]=simple(f)
r =
cos(2*x)
how =
combine
(5)符号表达式的分式通分(Reduction symbolic expression to common denominator)
符号表达式的分式通分函数为 [n,d]=numden(S),此函数将符号表达式转换为分子(Numerator)和分母(denominator)都是正系数的最佳多项式。
例:对表达式 f=x/y+y/x 进行通分。
syms x y
f=x/y+y/x;
[n,d]=numden(f)
n =
x^2+y^2
d =
y*x
NUMDEN Numerator and denominator of a symbolic expression.
[N,D] = NUMDEN(A) converts each element of A to a rational form where the numerator and denominator are relatively prime polynomials with integer coefficients.
(6) 符号表达式的嵌套形式重写(Representation of nested symbolic expression)
符号表达式的嵌套形式重写函数为 horner(S),此函数将符号表达式转换为嵌套形式。
例,对表达式f=x3+6x2+11x-6进行嵌套形式重写。
syms x
f=x^3+6*x^2+11*x-6;
horner(f)
ans =
-6+(11+(6+x)*x)*x
HORNER Horner polynomial representation.
HORNER(P) transforms the symbolic polynomial P into its Horner,
or nested,representation.
2,符号表达式的替换(Replacing of symbolic expression)
MATLAB 的符号数学工具箱提供了两个符号表达式的替换函数subexpr 和subs,可通过符号替换使表达式的输出形式简化。
subexpr函数可将表达式中重复出现的字符串用变量代替。调用格式:
[Y,SIGMA]=subexpr(S,SIGMA),用变量SIGMA的值代替符号表达式S中重复出现的字符串,Y返回替换后的结果。
例:求解并化简三次方程x3+ax+1=0的符号解。
t=solve(‘x^3+a*x+1=0’)
[r,s]=subexpr(t,’s’)
t = [1/6*(-108+12*(12*a^3+81)^(1/2))^(1/3)-2*a/(-108+12*(12*a^3+81)^(1/2))^(1/3)]
[ -1/12*(-108+12*(12*a^3+81)^(1/2))^(1/3)+a/(-108+12*(12*a^3+81)^(1/2))^(1/3)+1/2*i*3^(1/2)*(1/6*(-108+12*(12*a^3+81)^(1/2))^(1/3)+2*a/(-108+12*(12*a^3+81)^(1/2))^(1/3))]
[ -1/12*(-108+12*(12*a^3+81)^(1/2))^(1/3)+a/(-108+12*(12*a^3+81)^(1/2))^(1/3)-1/2*i*3^(1/2)*(1/6*(-108+12*(12*a^3+81)^(1/2))^(1/3)+2*a/(-108+12*(12*a^3+81)^(1/2))^(1/3))]
r =
[ 1/6*s^(1/3)-2*a/s^(1/3)]
[ -1/12*s^(1/3)+a/s^(1/3)+1/2*i*3^(1/2)*(1/6*s^(1/3)+2*a/s^(1/3))]
[ -1/12*s^(1/3)+a/s^(1/3)-1/2*i*3^(1/2)*(1/6*s^(1/3)+2*a/s^(1/3))]
s =
-108+12*(12*a^3+81)^(1/2)
函数subs是用指定符号替换符号表达式中的某一特定符号,调用格式为:R=subs(S,old,new),它可用新的符号变量new替换原来符号表达式S中的old,当new为数值形式时,显示的结果虽然是数值,但它事实上是符号变量。
例:分别用新变量替换表达式a+b和cos(a)+sin(b)中变量。
syms a b
subs(a+b,a,4)
subs(cos(a)+sin(b),{a,b},{sym('alpha'),2}) %用单元数组完成不同性质
%元素的替换
ans =
4+b
ans =
cos(alpha)+sin(2)
三.符号微积分(Differential and integral calculus)
1,符号极限(Symbolic limit)
*limit(F,x,a) 计算符号表达式F在x→a条件下的极限;
*limit(F,a) 计算符号表达式F中由默认自变量趋向于a条件下的极限;
*limit(F,) 计算符号表达式F在默认自变量趋向于0条件下的极限;
*limit(F,x,a,‘right’) 和limit(F,x,a,’left’) 计算符号表达式F在x→a条件下的右极限和左极限。
例:分别计算表达式,,及和
syms x a;
limit(sin(x)/x)
limit(1/x,x,0,’right’)
limit(1/x,x,0,’left’)
v=[(1+a/x)^x,exp(-x)];
limit(v,x,inf,’left’)
ans =
1
ans =
inf
ans =
-inf
ans =
[ exp(a),0]
2,符号微分(symbolic differential calculus)
*diff(S) 求符号表达式S对于默认自变量的微分;
*diff(S,v) 求符号表达式S对于自变量v的微分;
*diff(S,n) 求符号表达式S对于默认自变量的n次微分;
例,分别计算表达式f=xx的导数和3次导数.
syms x;
f=x^x;
diff(f)
diff(f,3)
ans =
x^x*(log(x)+1)
ans =
x^x*(log(x)+1)^3+3*x^x*(log(x)+1)/x-x^x/x^2
3,符号积分(Symbolic integral calculus)
*int(S) 求符号表达式S对于默认自变量的不定积分;
*int(S,v) 求符号表达式S对于自变量v的不定积分;
*int(S,a,b) 求符号表达式S对于默认自变量从a到b的定积分;
例:分别计算表达式、,和。
syms x z;
f=-2*x/(1+x^2)^2;
int(f)
f=x/(1+z^2);
int(f)
int(f,z)
f=x*log(1+x);
int(f,0,1)
ans =
1/(1+x^2)
ans =
1/2*x^2/(1+z^2)
ans =
x*atan(z)
ans =
1/4
4,符号求和(Symbolic summation)
*symsum(S) 求符号表达式S对于默认自变量的不定和;
* symsum(S,v) 求符号表达式S对于自变量v的不定和;
* symsum(S,a,b) 求符号表达式S对于默认自变量从a到b的有限和;
例,分别计算表达式∑k,和
syms k x
symsum(k)
symsum(k^2,0,10)
symsum(x^k/sym(‘k!’),k,0,inf)
ans =
1/2*k^2-1/2*k
ans =
385
ans =
exp(x)
5,Taylor级数展开(Taylor series expanding)
*Taylor(f) 计算符号表达式f对于默认自变量等于0 处的5阶taylor级数展开式;
*taylor(f,n,v) 计算符号表达式f在自变量v=0处的n-1阶Taylor级数展开式;
*taylor(f,n,v,a) 计算符号表达式f在自变量v=a 处的n-1阶Taylor 级数展开式。
例,分别计算表达式 的5 阶Taylor级数展开式和f=exsin(x) 的
5 阶及12 阶Taylor级数展开式。
syms x
f=1/(5+cos(x));
r=taylor(f)
f=exp(x*sin(x));
r=taylor(f,12)
r=taylor(f)
r =
1/6+1/72*x^2
r =
1+x^2+1/3*x^4+1/120*x^6-11/560*x^8-1079/362880*x^10
r =
1+x^2+1/3*x^4
四,符号方程的求解(Symbolic equation solution)
1,符号代数方程组的求解(symbolic algebra equations set solution)
*g=solve(eq) 求解符号表达式eq=0的代数方程,自变量为默认自变量;
*g=solve(eq,var) 求解符号表达式eq=0的代数方程,自变量为var;
*g=solve(eq1,eq2,…,eqn,var1,var2,…,varn))求解符号表达式eq1,eq2,…eqn组成的代数方程组,自变量分别为var1,var2,…varn。
例1:分别求解代数方程ax2+bx+c=0和cos(2x)+sin(x)=1
syms a b c x
s=a*x^2+b*x+c;
solve(s)
solve('cos(2*x)+sin(x)=1')
ans =
[ 1/2/a*(-b+(b^2-4*a*c)^(1/2))]
[ 1/2/a*(-b-(b^2-4*a*c)^(1/2))]
ans = [ 0]
[ pi]
[ 1/6*pi]
[ 5/6*pi]
例2:求解代数方程组x2-y2+z=10,x+y-5z=0,2x=4y+z=0
syms x y z
f=x^2-y^2+z-10;
g=x+y-5*z;
h=2*x-4*y+z;
[x,y,z]=solve(f,g,h) %以数值数组形式输出求解结果
S=solve(f,g,h); %缺省情况将方程组的解存放在结构变量中
[S.x,S.y,S.z]
x =
[ -19/80+19/240*2409^(1/2)]
[ -19/80-19/240*2409^(1/2)]
y =
[ -11/80+11/240*2409^(1/2)]
[ -11/80-11/240*2409^(1/2)]
z =
[ -3/40+1/40*2409^(1/2)]
[ -3/40-1/40*2409^(1/2)]
ans =
[-19/80+19/240*2409^(1/2),-11/80+11/240*2409^(1/2),-3/40+1/40*2409^(1/2)]
[ -19/80-19/240*2409^(1/2),-11/80-11/240*2409^(1/2),-3/40-1/40*2409^(1/2)]
SOLVE Symbolic solution of algebraic equations.
SOLVE('eqn1','eqn2',...,'eqnN')
SOLVE('eqn1','eqn2',...,'eqnN','var1,var2,...,varN')
SOLVE('eqn1','eqn2',...,'eqnN','var1','var2',...'varN')
The eqns are symbolic expressions or strings specifying equations,The
vars are symbolic variables or strings specifying the unknown variables.
SOLVE seeks zeros of the expressions or solutions of the equations,If not specified,the unknowns in the system are determined by FINDSYM.
If no analytical solution is found and the number of equations equals the number of dependent variables,a numeric solution is attempted.
Three different types of output are possible,For one equation and one
output,the resulting solution is returned,with multiple solutions to a nonlinear equation in a symbolic vector,For several equations and an equal number of outputs,the results are sorted in lexicographic order and assigned to the outputs,For several equations and a single output,a structure containing the solutions is returned.
Examples:
solve('p*sin(x) = r') chooses 'x' as the unknown and returns
ans =
asin(r/p)
[x,y] = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 = 0') returns
x =
[ 1]
[ 3]
y =
[ 1]
[ -3/2]
S = solve('x^2*y^2 - 2*x - 1 = 0','x^2 - y^2 - 1 = 0') returns
the solutions in a structure.
S =
x,[8x1 sym]
y,[8x1 sym]
[u,v] = solve('a*u^2 + v^2 = 0','u - v = 1') regards 'a' as a
parameter and solves the two equations for u and v.
S = solve('a*u^2 + v^2','u - v = 1','a,u') regards 'v' as a
parameter,solves the two equations,and returns S.a and S.u.
[a,u,v] = solve('a*u^2 + v^2','u - v = 1','a^2 - 5*a + 6') solves
the three equations for a,u and v.
a =
[ 2]
[ 2]
[ 3]
[ 3]
u =
[ 1/3+1/3*i*2^(1/2)]
[ 1/3-1/3*i*2^(1/2)]
[ 1/4+1/4*i*3^(1/2)]
[ 1/4-1/4*i*3^(1/2)]
v =
[ -2/3+1/3*i*2^(1/2)]
[ -2/3-1/3*i*2^(1/2)]
[ -3/4+1/4*i*3^(1/2)]
[ -3/4-1/4*i*3^(1/2)]
FSOLVE Solves nonlinear equations by a least squares method.
FSOLVE solves equations of the form:
F(X)=0 where F and X may be vectors or matrices,
X=FSOLVE(FUN,X0) starts at the matrix X0 and tries to solve the equations in FUN,FUN accepts input X and returns a vector (matrix) of
equation values F evaluated at X,
X=FSOLVE(FUN,X0,OPTIONS) minimizes with the default optimization
parameters replaced by values in the structure OPTIONS,an argument
created with the OPTIMSET function.See OPTIMSET for details,Used options are Display,TolX,TolFun,DerivativeCheck,Diagnostics,Jacobian,JacobMult,JacobPattern,LineSearchType,LevenbergMarquardt,MaxFunEvals,MaxIter,DiffMinChange and DiffMaxChange,LargeScale,MaxPCGIter,PrecondBandWidth,TolPCG,TypicalX,Use the Jacobian option to specify that
FUN also returns a second output argument J that is the Jacobian matrix at
the point X,If FUN returns a vector F of m components when X has length n,
then J is an m-by-n matrix where J(i,j) is the partial derivative of F(i) with respect to x(j),(Note that the Jacobian J is the transpose of the gradient of F.)
X=FSOLVE(FUN,X0,OPTIONS,P1,P2,...) passes the problem-dependent
parameters P1,P2,..,directly to the function FUN,FUN(X,P1,P2,...),
Pass an empty matrix for OPTIONS to use the default values,
[X,FVAL]=FSOLVE(FUN,X0,...) returns the value of the objective function
at X,
[X,FVAL,EXITFLAG]=FSOLVE(FUN,X0,...) returns a string EXITFLAG that describes the exit condition of FSOLVE,
If EXITFLAG is,> 0 then FSOLVE converged to a solution X,0 then the maximum number of function evaluations was reached.
< 0 then FSOLVE did not converge to a solution.
[X,FVAL,EXITFLAG,OUTPUT]=FSOLVE(FUN,X0,...) returns a structure OUTPUT with the number of iterations taken in OUTPUT.iterations,the number of function evaluations in OUTPUT.funcCount,the algorithm used in OUTPUT.algorithm,the number of CG iterations (if used) in OUTPUT.cgiterations,and the first-order optimality (if used) in OUTPUT.firstorderopt.
[X,FVAL,EXITFLAG,OUTPUT,JACOB]=FSOLVE(FUN,X0,...) returns the
Jacobian of FUN at X,
Examples
FUN can be specified using @:
x = fsolve(@myfun,[2 3 4],optimset('Display','iter'))
where MYFUN is a MATLAB function such as:
function F = myfun(x)
F = sin(x);
FUN can also be an inline object:
fun = inline('sin(3*x)');
x = fsolve(fun,[1 4],optimset('Display','off'));
2,符号微分方程求解(Symbolic differential equation solution)
符号微分方程求解函数:r=dsolve(‘eq1,eq2…’,’cond1,cond2…’,’v’)
求由eq1,eq2,…指定的微分方程的符号解,参数cond1,cond2,…为指定常微分方程的边界条件或初始条件,自变量v如果不指定,将为默认自变量。
方程中D表示一次微分D2和D3分别表示二次及三次微分,D后的字符为默认自变量。
例1,求微分方程dy/dx=ay的通解和当y(0)=b时的特解。
dsolve(‘Dy=a*y’)
dsolve(‘Dy=a*y’,’y(0)=b’,’x’)
ans =
C1*exp(a*t) 默认自变量为t
ans =
b*exp(a*x)
例2,求微分方程=-a2y当y=(0)=1及时的特解。
dsolve(‘D2y=-a^2*y’,’y(0)=1’,’Dy(pi/a)=0’)
ans =
cos(a*t)
五,符号数学的简易绘图函数(Easy ploting function of symbolic mathematics)
1,二维图绘图函数(Two dimensional plotting function)
*ezplot(f) 绘制表达式f(x)的二维图形,轴坐标的近似范围为[-2π,2π].
*ezplot(f,[xmin,xmax]) 绘制表达式f(x)的二维图形,轴坐标的近似范围为[xmin,xmax].
*ezpolar(f) 在极坐标下绘制函数表达式f(x)的二维图形。
例1:绘制函数表达式x2-y2的二维图形
syms x y
ezplot(x^2-y^4)
例2:绘制误差函数f(x)=的二维图形.
syms x
ezplot(erf(x))
grid
ERF Error function.
Y = ERF(X) is the error function for each element of X,X must be
real,The error function is defined as:
erf(x) = 2/sqrt(pi) * integral from 0 to x of exp(-t^2) dt.
例3:在极坐标下绘制函数表达式1+cos(t)的二维图形。
syms t
figure (1)
ezplot(1+cos(t))
figure ( 2)
ezpolar(1+cos(t))
三维绘图函数(Three dimensional plotting function)
*ezplot3(x,y,z) 绘制由表达式x=x(t),y=y(t) 和z=z(t)定义的三维曲线,自变量t的变化范围为 [-2π,2π]。
*ezplot3(x,y,z,[tmin,tmax]) 绘制由表达式x=x(t),y=y(t) 和z=z(t)定义的三维曲线,自变量t的变化范围为 [tmin,tmax]。
*ezplot3(…,’animate’) 如果在函数中增加参数’animate’,则绘制三维动态轨迹图。
例;根据表达式x=sin(t),y=cos(t) 和 z=t,绘制三维曲线.
syms t;
ezplot3(sin(t),cos(t),t,[0,6*pi])
绘动态轨迹图
syms t;
ezplot3(sin(t),cos(t),t,[0,6*pi],'animate')
3,等高线绘图函数
*ezcontour(f) 绘制由表达式f(x,y)定义的等高线,自变量 x和 y
的变化范围为[-2π,2π]。
*ezcontour(f,domain) 绘制由表达式f(x,y)定义的等高线,自变量 x和 y
的变化范围由domain确定,domain可以是4×1的矢量[xmin,xmax,ymin,ymax],也可以是2×1的矢量[min,max],当domain为的矢量时,min<x<max,min<y<max。
*eznotour(…,n) 按n×n的网格密度绘制等高线图,n的缺省值为60。
例1:根据表达式f= 绘制f的等高线。
syms x y
f=3*(1-x)^2*exp(-(x^2)-(y+1)^2)-10*(x/5-x^3-y^5) …
*exp(-x^2-y^2)-1/3*exp(-(x+1)^2-y^2);
ezcontour(f,[-3,3],49)
例2:根据表达式绘制f=的填充等高线。
syms x y
f=3*(1-x)^2*exp(-(x^2)-(y+1)^2)-10*(x/5-x^3-y^5),..
*exp(-x^2-y^2)-1/3*exp(-(x+1)^2-y^2);
ezcontourf(f,[-3,3],49)
CONTOURF Filled contour plot.
CONTOURF(...) is the same as CONTOUR(...) except that the contours
are filled,Areas of the data at or above a given level are filled,Areas below a level are either left blank or are filled by a lower level,NaN's in the data leave holes in the filled contour plot.
C = CONTOURF(...) returns contour matrix C as described in CONTOURC
and used by CLABEL.
[C,H,CF] = CONTOURF(...) also returns a column vector H of handles
to PATCH objects and the contour matrix CF for the filled areas.
The UserData property of each object contains the height value for each
contour.
CLABEL Contour plot elevation labels.
CLABEL(CS,H) adds height labels to the current contour
plot,The labels are rotated and inserted within the contour
lines,CS and H are the contour matrix output and object handle
outputs from CONTOUR,CONTOUR3,or CONTOURF.
Example:
z=peaks;
contourf(z),
hold on,
shading flat
[c,h]=contour(z,'k-');
clabel(c,h),
colorbar
4,网格图绘图函数(Net plot plotting function)
*ezmesh(f) 绘制由表达式f(x,y)定义的网格图,自变量x和y的变化范围为[-2π,2π];
*ezmesh(f,domain) 绘制由表达式f(x,y)定义的网格图,自变量 x和 y的变化范围由domain确定,domain可以是4×1的矢量[xmin,xmax,ymin,ymax],也可以是2×1的矢量[min,max],当domain为的矢量时,min<x<max,min<y<max。
*ezmesh(x,y,z) 绘制由表达式x=x(s,t),y=y(s,t)和z=z(s,t)定义的参数表面网格图,自变量s和t的变化范围为[-2π,2π];
*ezmesh(x,y,z,[smin,smax,tmin,tmax]) 绘制由表达式x=x(s,t),y=y(s,t)和z=z(s,t)定义的参数表面网格图,自变量s和t的变化范围为[smin,smax,tmin,tmax];
*ezmesh(…,n) 绘网格图时按n×n的网格密度绘图,n的缺省值为60;
*ezmesh(…,’circ’) 以圆盘为自变量域绘制网格图,
例1:根据表达式f=,绘制f的网格图.
syms x y
ezmesh(x*exp(-x^2-y^2),[-2.5,2.5],40)
colormap([0 0 1])
例2:根据表达式,以圆盘为自变量域绘制f的网格图.
syms x y
ezmesh(x*exp(-x^2-y^2),[-2.5,2.5],40,’circ’)
例3:根据表达式f=y/(1+x2+y2),绘制f的带等高线网格图。
syms x y
ezmeshc(y/(1+x^2+y^2),[-5,5,-2*pi,2*pi])
5,表面图绘图函数(surface plot plotting function)
*ezsurf(f) 绘制由表达式f(x,y)定义的表面图,自变量x和y的变化范围为[-2π,2π];
*ezsurf(f,domain) 绘制由表达式f(x,y)定义的表面图,自变量 x和 y的变化范围由domain确定,domain可以是4×1的矢量[xmin,xmax,ymin,ymax],也可以是2×1的矢量[min,max],当domain为的矢量时,min<x<max,min<y<max。
*ezsurf(x,y,z) 绘制由表达式x=x(s,t),y=y(s,t)和z=z(s,t)定义的参数表面图,自变量s和t的变化范围均为[-2π,2π];
ezsurf(x,y,z,[smin,smax,tmin,tmax]) 绘制由表达式x=x(s,t),y=y(s,t)和z=z(s,t)定义的参数表面图,自变量s和t的变化范围为[smin,smax,tmin,tmax];
*ezsurf(…,n) 绘表面图时按n×n的网格密度绘图,n的缺省值为60;
*ezsurf(…,’circ’) 以圆盘为自变量域绘制表面图,
例1:根据表达式x=cos(s)cos(t),y=cos(s)sin(t) 和 z=sin(s),绘制表面图。
syms t s
x=cos(s)*cos(t);
y=cos(s)*sin(t);
z=sin(s);
ezsurf(x,y,z,[0,pi/2,0,3*pi/2])
view(17,40)
shading interp
例2:根据表达式f=y/(1+x2+y2),绘制f带等高线的表面图。
syms x y
ezsurfc(y/(1+x^2+y^2),[-5,5,-2*pi,2*pi],35)
view(-65,26)
五,图形化符号函数计算器(Interactive graphing calculator that manipulates
functions of a single variable.)
图形化符号函数计算器函数funtool,在命令窗口funtool,即可启动图形化符号函数计数器。其三个窗口分别为函数f的显示窗口,
函数g的显示窗口,另外一个是函数功能控制窗口。
1,输入框:在控制窗口的上方有4个输入框,用户可以在输入框中输入函数,4个窗分别为:
*f= 为图形窗口1 的控制函数,其缺省值为x;
*g= 为图形窗口2 的控制函数,其缺省值为1;
*x= 为两窗口函数的自变量的取值范围,缺省值为 [-2π,2π];
*a= 为常数的值,缺省值为1/2。
2,计算器的功能(Functions of the calculator)
(1) 函数的自运算,函数功能的控制窗口的第一排按钮为函数的自运算(表 5—2)按钮。
(2) 函数与常数的运算:函数功能的控制窗口的第二排按钮为函数与常数之间的运算(表 5—3)按钮。
(3) 两函数之间的运算:函数功能的控制窗口的第三排按钮为函数f与g之间的运算(表 5—3)按钮。
(4) 函数计算器的系统操作:函数功能的控制窗口的第四排按钮为函数计算器的系统操作,(表 5—5)按钮。
FUNTOOL A function calculator.
FUNTOOL is an interactive graphing calculator that manipulates
functions of a single variable,At any time,there are two functions
displayed,f(x) and g(x),The result of most operations replaces f(x).
The controls labeled 'f = ' and 'g = ' are editable text that may
be changed at any time to install a new function,The control
labeled 'x = ' may be changed to specify a new domain,The control
labeled 'a = ' may be changed to specify a new value of a parameter.
The top row of control buttons are unary function operators that
involve only f(x),These operators are:
df/dx - Symbolically differentiate f(x).
int f - Symbolically integrate f(x).
simple f - Simplify the symbolic expression,if possible.
num f - Extract the numerator of a rational expression.
den f - Extract the denominator of a rational expression.
1/f - Replace f(x) by 1/f(x).
finv - Replace f(x) by its inverse function.
The operators int(f) and finv may fail if the corresponding symbolic
expressions do not exist in closed form.
The second row of buttons translate and scale f(x) by the parameter 'a'.
The operations are:
f + a - Replace f(x) by f(x) + a.
f - a - Replace f(x) by f(x) - a.
f * a - Replace f(x) by f(x) * a.
f / a - Replace f(x) by f(x) / a.
f ^ a - Replace f(x) by f(x) ^ a.
f(x+a) - Replace f(x) by f(x + a).
f(x*a) - Replace f(x) by f(x * a).
The third row of buttons are binary function operators that
operate on both f(x) and g(x),The operations are:
f + g - Replace f(x) by f(x) + g(x).
f - g - Replace f(x) by f(x) - g(x).
f * g - Replace f(x) by f(x) * g(x).
f / g - Replace f(x) by f(x) / g(x).
f(g) - Replace f(x) by f(g(x)).
g = f - Replace g(x) by f(x).
swap - Interchange f(x) and g(x).
The first three buttons in the fourth row manage a list of functions.
The Insert button places the current active function in the list.
The Cycle button rotates the function list.
The Delete button removes the active function from the list,
The list of functions is named fxlist,A default fxlist containing
several interesting functions is provided.
The Reset button sets f,g,x,a and fxlist to their initial values.
The Help button prints this help text,
The Demo button poses the following challenge,Can you generate the
function sin(x) without touching the keyboard,using just the mouse?
The demo does it with a reset and then nine clicks,If you can do
it with fewer clicks,please send e-mail to moler@mathworks.com.
The Close button closes all three windows.
Chapter 5:Foundation of Symbolic Mathematics
一,符号对象的创建(Creating a symbolic object)
1,创建符号变量和表达式(Creating a symbolic variable and expression)
创建符号变量和表达式的两个基本函数:sym,syms
*x=sym(‘x’) 创建一个符号变量x,可以是字符、字符串、表达式或字符表达式。
*syms用于方便地一次创建多个符号变量,调用格式为,syms a b c d,书写简洁意义清楚,建议使用。
例1:使用sym函数创建符号变量.
a=sym(‘a’)
b=sym( ‘hello’)
c=sym(( ‘(1+sqrt(5))/2’)
y=sym( ‘x^3+5*x^2+12*x+20’)
a =
a
b =
hello
C =
(1+sqrt(5))/2
Y =
x^3+5*x^2+12*x+20
例2:用syms函数创建符号变量。
syms a b c d
2,创建符号矩阵(Symbolic matrix Creating)
例1:创建一个循环矩阵。
syms a b c d
n=[a b c d;b c d a;c d a b;d a b c]
n =
[ a,b,c,d]
[ b,c,d,a]
[ c,d,a,b]
[ d,a,b,c]
例2:将3阶Hilbert矩阵转换为符号矩阵。
h=hilb(3)
h1=sym(h)
h =
1.0000 0.5000 0.3333
0.5000 0.3333 0.2500
0.3333 0.2500 0.2000
h1 =
[ 1,1/2,1/3]
[ 1/2,1/3,1/4]
[ 1/3,1/4,1/5]
注意符号矩阵于数值矩阵的区别。
默认符号变量(Implied symbolic variable)
在MATLAB的符号数学工具箱中,以最接近x的顺序排列默认自变量的顺序,可利用findsym函数对默认自变量进行查询。
例1,求符号函数在不同自变量情况下的结果。
创建符号变量x和n,建立函数f=xn,然后分别求f对x和f对n的导数.
syms x n
f=x^n
diff(f) % x作为自变量,求f对x的导数
diff(f,n) % n作为自变量,求f对n的导数
f =
x^n
ans =
x^n*n/x
ans =
x^n*log(x)
例2,查询符号函数中的默认自变量。
创建符号变量 a,b,n,x 和t,建立函数f=axn+bt,然后求f的默认自变量。
syms a b n t x
f=a*x^n+b*t
findsym(f,1)
findsym(f,2)
findsym(f,5) % f表达式中按最接近x顺序排列的5个默认自变量
findsym(f) % f表达式中按最接近字母顺序排列的全部自变量
f =
a*x^n+b*t
ans =
x
ans =
x,t
ans =
x,t,n,b,a
ans =
a,b,n,t,x
>>
符号表达式的化简和替换(simplifying and replacing of Symbolic xpressions)
符号数学工具箱提供的符号表达式的因式分解、展开、合并、化简、通分等操作:
1,符号表达式的化简(Simplifying of symbolic expression)
(1).因式分解(Factorization)
符号表达式的因式分解函数为 factor(S),可分解符号表达式S的各个元素。
例1,对表达式f=x9-1进行因式分解。
syms x
f=factor(x^9-1)
pretty(f)
f =
(x-1)*(x^2+x+1)*(x^6+x^3+1)
2 6 3
(x - 1) (x + x + 1) (x + x + 1)
例2:对大整数12345678901234567890进行因式分解。
factor(sym(‘12345678901234567890’))
ans =
(2)*(3)^2*(5)*(101)*(3803)*(3607)*(27961)*(3541)
(2)符号表达式的展开(Expanding of symbolic expressions)
符号表达式的展开函数为expand(S),此函数因数展开符号表达式S.
例,展开表达式f=(x+1)5和f=sin(x+y)
syms x y
f=(x+1)^5;
expand(f)
f=sin(x+y);
expand(f)
ans =
x^5+5*x^4+10*x^3+10*x^2+5*x+1
ans =
sin(x)*cos(y)+cos(x)*sin(y)
(3).符号表达式的同类项合并(Similar team merging for symbolic expression)
符号表达式的同类项合并函数为 collect(S,n),此函数将符号表达式中自变量的同次幂项的系数合并。
例:对于表达式f=x(x(x-6)+12)t,分别将自变量x和t的同类项合并。
syms x t
f=x*(x*(x-6)+12)*t;
collect(f)
collect(f,t)
ans =
t*x^3-6*t*x^2+12*t*x
ans =
x*(x*(x-6)+12)*t
COLLECT Collect coefficients.
COLLECT(S,v) regards each element of the symbolic matrix S as a
polynomial in v and rewrites S in terms of the powers of v.
COLLECT(S) uses the default variable determined by FINDSYM.
(4),符号表达式的化简(Simplifying of symbolic expression)
符号表达式的两个化简函数:simplify,simple,
simplify:化简函数,可用于化简各种表达式
例1:对表达式f=sin2(x)+cos2(x)进行化简.
syms x
f=sin(x)^2+cos(x)^2;
simplify(f)
ans =
1
[r,how]=simple(S) 函数可寻找符号表达式S的最简型,r为返回的简化形式,how为化简过程中使用的主要方法,simple函数综合使用了下列化简方法:
*simplify 函数对表达式进行化简
*radsimp 函数对含根式(surd)的表达式进行化简
*combine 函数对表达式中以求和、乘积、幂运算等形式出现的项进行合并
*collect 合并同类项
*factor 函数实现因式分解
*convert 函数完成表达式形式的转换
例2:最简表达式的获得。
syms x t
f=cos(x)^2-sin(x)^2;
[r,how]=simple(f)
r =
cos(2*x)
how =
combine
(5)符号表达式的分式通分(Reduction symbolic expression to common denominator)
符号表达式的分式通分函数为 [n,d]=numden(S),此函数将符号表达式转换为分子(Numerator)和分母(denominator)都是正系数的最佳多项式。
例:对表达式 f=x/y+y/x 进行通分。
syms x y
f=x/y+y/x;
[n,d]=numden(f)
n =
x^2+y^2
d =
y*x
NUMDEN Numerator and denominator of a symbolic expression.
[N,D] = NUMDEN(A) converts each element of A to a rational form where the numerator and denominator are relatively prime polynomials with integer coefficients.
(6) 符号表达式的嵌套形式重写(Representation of nested symbolic expression)
符号表达式的嵌套形式重写函数为 horner(S),此函数将符号表达式转换为嵌套形式。
例,对表达式f=x3+6x2+11x-6进行嵌套形式重写。
syms x
f=x^3+6*x^2+11*x-6;
horner(f)
ans =
-6+(11+(6+x)*x)*x
HORNER Horner polynomial representation.
HORNER(P) transforms the symbolic polynomial P into its Horner,
or nested,representation.
2,符号表达式的替换(Replacing of symbolic expression)
MATLAB 的符号数学工具箱提供了两个符号表达式的替换函数subexpr 和subs,可通过符号替换使表达式的输出形式简化。
subexpr函数可将表达式中重复出现的字符串用变量代替。调用格式:
[Y,SIGMA]=subexpr(S,SIGMA),用变量SIGMA的值代替符号表达式S中重复出现的字符串,Y返回替换后的结果。
例:求解并化简三次方程x3+ax+1=0的符号解。
t=solve(‘x^3+a*x+1=0’)
[r,s]=subexpr(t,’s’)
t = [1/6*(-108+12*(12*a^3+81)^(1/2))^(1/3)-2*a/(-108+12*(12*a^3+81)^(1/2))^(1/3)]
[ -1/12*(-108+12*(12*a^3+81)^(1/2))^(1/3)+a/(-108+12*(12*a^3+81)^(1/2))^(1/3)+1/2*i*3^(1/2)*(1/6*(-108+12*(12*a^3+81)^(1/2))^(1/3)+2*a/(-108+12*(12*a^3+81)^(1/2))^(1/3))]
[ -1/12*(-108+12*(12*a^3+81)^(1/2))^(1/3)+a/(-108+12*(12*a^3+81)^(1/2))^(1/3)-1/2*i*3^(1/2)*(1/6*(-108+12*(12*a^3+81)^(1/2))^(1/3)+2*a/(-108+12*(12*a^3+81)^(1/2))^(1/3))]
r =
[ 1/6*s^(1/3)-2*a/s^(1/3)]
[ -1/12*s^(1/3)+a/s^(1/3)+1/2*i*3^(1/2)*(1/6*s^(1/3)+2*a/s^(1/3))]
[ -1/12*s^(1/3)+a/s^(1/3)-1/2*i*3^(1/2)*(1/6*s^(1/3)+2*a/s^(1/3))]
s =
-108+12*(12*a^3+81)^(1/2)
函数subs是用指定符号替换符号表达式中的某一特定符号,调用格式为:R=subs(S,old,new),它可用新的符号变量new替换原来符号表达式S中的old,当new为数值形式时,显示的结果虽然是数值,但它事实上是符号变量。
例:分别用新变量替换表达式a+b和cos(a)+sin(b)中变量。
syms a b
subs(a+b,a,4)
subs(cos(a)+sin(b),{a,b},{sym('alpha'),2}) %用单元数组完成不同性质
%元素的替换
ans =
4+b
ans =
cos(alpha)+sin(2)
三.符号微积分(Differential and integral calculus)
1,符号极限(Symbolic limit)
*limit(F,x,a) 计算符号表达式F在x→a条件下的极限;
*limit(F,a) 计算符号表达式F中由默认自变量趋向于a条件下的极限;
*limit(F,) 计算符号表达式F在默认自变量趋向于0条件下的极限;
*limit(F,x,a,‘right’) 和limit(F,x,a,’left’) 计算符号表达式F在x→a条件下的右极限和左极限。
例:分别计算表达式,,及和
syms x a;
limit(sin(x)/x)
limit(1/x,x,0,’right’)
limit(1/x,x,0,’left’)
v=[(1+a/x)^x,exp(-x)];
limit(v,x,inf,’left’)
ans =
1
ans =
inf
ans =
-inf
ans =
[ exp(a),0]
2,符号微分(symbolic differential calculus)
*diff(S) 求符号表达式S对于默认自变量的微分;
*diff(S,v) 求符号表达式S对于自变量v的微分;
*diff(S,n) 求符号表达式S对于默认自变量的n次微分;
例,分别计算表达式f=xx的导数和3次导数.
syms x;
f=x^x;
diff(f)
diff(f,3)
ans =
x^x*(log(x)+1)
ans =
x^x*(log(x)+1)^3+3*x^x*(log(x)+1)/x-x^x/x^2
3,符号积分(Symbolic integral calculus)
*int(S) 求符号表达式S对于默认自变量的不定积分;
*int(S,v) 求符号表达式S对于自变量v的不定积分;
*int(S,a,b) 求符号表达式S对于默认自变量从a到b的定积分;
例:分别计算表达式、,和。
syms x z;
f=-2*x/(1+x^2)^2;
int(f)
f=x/(1+z^2);
int(f)
int(f,z)
f=x*log(1+x);
int(f,0,1)
ans =
1/(1+x^2)
ans =
1/2*x^2/(1+z^2)
ans =
x*atan(z)
ans =
1/4
4,符号求和(Symbolic summation)
*symsum(S) 求符号表达式S对于默认自变量的不定和;
* symsum(S,v) 求符号表达式S对于自变量v的不定和;
* symsum(S,a,b) 求符号表达式S对于默认自变量从a到b的有限和;
例,分别计算表达式∑k,和
syms k x
symsum(k)
symsum(k^2,0,10)
symsum(x^k/sym(‘k!’),k,0,inf)
ans =
1/2*k^2-1/2*k
ans =
385
ans =
exp(x)
5,Taylor级数展开(Taylor series expanding)
*Taylor(f) 计算符号表达式f对于默认自变量等于0 处的5阶taylor级数展开式;
*taylor(f,n,v) 计算符号表达式f在自变量v=0处的n-1阶Taylor级数展开式;
*taylor(f,n,v,a) 计算符号表达式f在自变量v=a 处的n-1阶Taylor 级数展开式。
例,分别计算表达式 的5 阶Taylor级数展开式和f=exsin(x) 的
5 阶及12 阶Taylor级数展开式。
syms x
f=1/(5+cos(x));
r=taylor(f)
f=exp(x*sin(x));
r=taylor(f,12)
r=taylor(f)
r =
1/6+1/72*x^2
r =
1+x^2+1/3*x^4+1/120*x^6-11/560*x^8-1079/362880*x^10
r =
1+x^2+1/3*x^4
四,符号方程的求解(Symbolic equation solution)
1,符号代数方程组的求解(symbolic algebra equations set solution)
*g=solve(eq) 求解符号表达式eq=0的代数方程,自变量为默认自变量;
*g=solve(eq,var) 求解符号表达式eq=0的代数方程,自变量为var;
*g=solve(eq1,eq2,…,eqn,var1,var2,…,varn))求解符号表达式eq1,eq2,…eqn组成的代数方程组,自变量分别为var1,var2,…varn。
例1:分别求解代数方程ax2+bx+c=0和cos(2x)+sin(x)=1
syms a b c x
s=a*x^2+b*x+c;
solve(s)
solve('cos(2*x)+sin(x)=1')
ans =
[ 1/2/a*(-b+(b^2-4*a*c)^(1/2))]
[ 1/2/a*(-b-(b^2-4*a*c)^(1/2))]
ans = [ 0]
[ pi]
[ 1/6*pi]
[ 5/6*pi]
例2:求解代数方程组x2-y2+z=10,x+y-5z=0,2x=4y+z=0
syms x y z
f=x^2-y^2+z-10;
g=x+y-5*z;
h=2*x-4*y+z;
[x,y,z]=solve(f,g,h) %以数值数组形式输出求解结果
S=solve(f,g,h); %缺省情况将方程组的解存放在结构变量中
[S.x,S.y,S.z]
x =
[ -19/80+19/240*2409^(1/2)]
[ -19/80-19/240*2409^(1/2)]
y =
[ -11/80+11/240*2409^(1/2)]
[ -11/80-11/240*2409^(1/2)]
z =
[ -3/40+1/40*2409^(1/2)]
[ -3/40-1/40*2409^(1/2)]
ans =
[-19/80+19/240*2409^(1/2),-11/80+11/240*2409^(1/2),-3/40+1/40*2409^(1/2)]
[ -19/80-19/240*2409^(1/2),-11/80-11/240*2409^(1/2),-3/40-1/40*2409^(1/2)]
SOLVE Symbolic solution of algebraic equations.
SOLVE('eqn1','eqn2',...,'eqnN')
SOLVE('eqn1','eqn2',...,'eqnN','var1,var2,...,varN')
SOLVE('eqn1','eqn2',...,'eqnN','var1','var2',...'varN')
The eqns are symbolic expressions or strings specifying equations,The
vars are symbolic variables or strings specifying the unknown variables.
SOLVE seeks zeros of the expressions or solutions of the equations,If not specified,the unknowns in the system are determined by FINDSYM.
If no analytical solution is found and the number of equations equals the number of dependent variables,a numeric solution is attempted.
Three different types of output are possible,For one equation and one
output,the resulting solution is returned,with multiple solutions to a nonlinear equation in a symbolic vector,For several equations and an equal number of outputs,the results are sorted in lexicographic order and assigned to the outputs,For several equations and a single output,a structure containing the solutions is returned.
Examples:
solve('p*sin(x) = r') chooses 'x' as the unknown and returns
ans =
asin(r/p)
[x,y] = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 = 0') returns
x =
[ 1]
[ 3]
y =
[ 1]
[ -3/2]
S = solve('x^2*y^2 - 2*x - 1 = 0','x^2 - y^2 - 1 = 0') returns
the solutions in a structure.
S =
x,[8x1 sym]
y,[8x1 sym]
[u,v] = solve('a*u^2 + v^2 = 0','u - v = 1') regards 'a' as a
parameter and solves the two equations for u and v.
S = solve('a*u^2 + v^2','u - v = 1','a,u') regards 'v' as a
parameter,solves the two equations,and returns S.a and S.u.
[a,u,v] = solve('a*u^2 + v^2','u - v = 1','a^2 - 5*a + 6') solves
the three equations for a,u and v.
a =
[ 2]
[ 2]
[ 3]
[ 3]
u =
[ 1/3+1/3*i*2^(1/2)]
[ 1/3-1/3*i*2^(1/2)]
[ 1/4+1/4*i*3^(1/2)]
[ 1/4-1/4*i*3^(1/2)]
v =
[ -2/3+1/3*i*2^(1/2)]
[ -2/3-1/3*i*2^(1/2)]
[ -3/4+1/4*i*3^(1/2)]
[ -3/4-1/4*i*3^(1/2)]
FSOLVE Solves nonlinear equations by a least squares method.
FSOLVE solves equations of the form:
F(X)=0 where F and X may be vectors or matrices,
X=FSOLVE(FUN,X0) starts at the matrix X0 and tries to solve the equations in FUN,FUN accepts input X and returns a vector (matrix) of
equation values F evaluated at X,
X=FSOLVE(FUN,X0,OPTIONS) minimizes with the default optimization
parameters replaced by values in the structure OPTIONS,an argument
created with the OPTIMSET function.See OPTIMSET for details,Used options are Display,TolX,TolFun,DerivativeCheck,Diagnostics,Jacobian,JacobMult,JacobPattern,LineSearchType,LevenbergMarquardt,MaxFunEvals,MaxIter,DiffMinChange and DiffMaxChange,LargeScale,MaxPCGIter,PrecondBandWidth,TolPCG,TypicalX,Use the Jacobian option to specify that
FUN also returns a second output argument J that is the Jacobian matrix at
the point X,If FUN returns a vector F of m components when X has length n,
then J is an m-by-n matrix where J(i,j) is the partial derivative of F(i) with respect to x(j),(Note that the Jacobian J is the transpose of the gradient of F.)
X=FSOLVE(FUN,X0,OPTIONS,P1,P2,...) passes the problem-dependent
parameters P1,P2,..,directly to the function FUN,FUN(X,P1,P2,...),
Pass an empty matrix for OPTIONS to use the default values,
[X,FVAL]=FSOLVE(FUN,X0,...) returns the value of the objective function
at X,
[X,FVAL,EXITFLAG]=FSOLVE(FUN,X0,...) returns a string EXITFLAG that describes the exit condition of FSOLVE,
If EXITFLAG is,> 0 then FSOLVE converged to a solution X,0 then the maximum number of function evaluations was reached.
< 0 then FSOLVE did not converge to a solution.
[X,FVAL,EXITFLAG,OUTPUT]=FSOLVE(FUN,X0,...) returns a structure OUTPUT with the number of iterations taken in OUTPUT.iterations,the number of function evaluations in OUTPUT.funcCount,the algorithm used in OUTPUT.algorithm,the number of CG iterations (if used) in OUTPUT.cgiterations,and the first-order optimality (if used) in OUTPUT.firstorderopt.
[X,FVAL,EXITFLAG,OUTPUT,JACOB]=FSOLVE(FUN,X0,...) returns the
Jacobian of FUN at X,
Examples
FUN can be specified using @:
x = fsolve(@myfun,[2 3 4],optimset('Display','iter'))
where MYFUN is a MATLAB function such as:
function F = myfun(x)
F = sin(x);
FUN can also be an inline object:
fun = inline('sin(3*x)');
x = fsolve(fun,[1 4],optimset('Display','off'));
2,符号微分方程求解(Symbolic differential equation solution)
符号微分方程求解函数:r=dsolve(‘eq1,eq2…’,’cond1,cond2…’,’v’)
求由eq1,eq2,…指定的微分方程的符号解,参数cond1,cond2,…为指定常微分方程的边界条件或初始条件,自变量v如果不指定,将为默认自变量。
方程中D表示一次微分D2和D3分别表示二次及三次微分,D后的字符为默认自变量。
例1,求微分方程dy/dx=ay的通解和当y(0)=b时的特解。
dsolve(‘Dy=a*y’)
dsolve(‘Dy=a*y’,’y(0)=b’,’x’)
ans =
C1*exp(a*t) 默认自变量为t
ans =
b*exp(a*x)
例2,求微分方程=-a2y当y=(0)=1及时的特解。
dsolve(‘D2y=-a^2*y’,’y(0)=1’,’Dy(pi/a)=0’)
ans =
cos(a*t)
五,符号数学的简易绘图函数(Easy ploting function of symbolic mathematics)
1,二维图绘图函数(Two dimensional plotting function)
*ezplot(f) 绘制表达式f(x)的二维图形,轴坐标的近似范围为[-2π,2π].
*ezplot(f,[xmin,xmax]) 绘制表达式f(x)的二维图形,轴坐标的近似范围为[xmin,xmax].
*ezpolar(f) 在极坐标下绘制函数表达式f(x)的二维图形。
例1:绘制函数表达式x2-y2的二维图形
syms x y
ezplot(x^2-y^4)
例2:绘制误差函数f(x)=的二维图形.
syms x
ezplot(erf(x))
grid
ERF Error function.
Y = ERF(X) is the error function for each element of X,X must be
real,The error function is defined as:
erf(x) = 2/sqrt(pi) * integral from 0 to x of exp(-t^2) dt.
例3:在极坐标下绘制函数表达式1+cos(t)的二维图形。
syms t
figure (1)
ezplot(1+cos(t))
figure ( 2)
ezpolar(1+cos(t))
三维绘图函数(Three dimensional plotting function)
*ezplot3(x,y,z) 绘制由表达式x=x(t),y=y(t) 和z=z(t)定义的三维曲线,自变量t的变化范围为 [-2π,2π]。
*ezplot3(x,y,z,[tmin,tmax]) 绘制由表达式x=x(t),y=y(t) 和z=z(t)定义的三维曲线,自变量t的变化范围为 [tmin,tmax]。
*ezplot3(…,’animate’) 如果在函数中增加参数’animate’,则绘制三维动态轨迹图。
例;根据表达式x=sin(t),y=cos(t) 和 z=t,绘制三维曲线.
syms t;
ezplot3(sin(t),cos(t),t,[0,6*pi])
绘动态轨迹图
syms t;
ezplot3(sin(t),cos(t),t,[0,6*pi],'animate')
3,等高线绘图函数
*ezcontour(f) 绘制由表达式f(x,y)定义的等高线,自变量 x和 y
的变化范围为[-2π,2π]。
*ezcontour(f,domain) 绘制由表达式f(x,y)定义的等高线,自变量 x和 y
的变化范围由domain确定,domain可以是4×1的矢量[xmin,xmax,ymin,ymax],也可以是2×1的矢量[min,max],当domain为的矢量时,min<x<max,min<y<max。
*eznotour(…,n) 按n×n的网格密度绘制等高线图,n的缺省值为60。
例1:根据表达式f= 绘制f的等高线。
syms x y
f=3*(1-x)^2*exp(-(x^2)-(y+1)^2)-10*(x/5-x^3-y^5) …
*exp(-x^2-y^2)-1/3*exp(-(x+1)^2-y^2);
ezcontour(f,[-3,3],49)
例2:根据表达式绘制f=的填充等高线。
syms x y
f=3*(1-x)^2*exp(-(x^2)-(y+1)^2)-10*(x/5-x^3-y^5),..
*exp(-x^2-y^2)-1/3*exp(-(x+1)^2-y^2);
ezcontourf(f,[-3,3],49)
CONTOURF Filled contour plot.
CONTOURF(...) is the same as CONTOUR(...) except that the contours
are filled,Areas of the data at or above a given level are filled,Areas below a level are either left blank or are filled by a lower level,NaN's in the data leave holes in the filled contour plot.
C = CONTOURF(...) returns contour matrix C as described in CONTOURC
and used by CLABEL.
[C,H,CF] = CONTOURF(...) also returns a column vector H of handles
to PATCH objects and the contour matrix CF for the filled areas.
The UserData property of each object contains the height value for each
contour.
CLABEL Contour plot elevation labels.
CLABEL(CS,H) adds height labels to the current contour
plot,The labels are rotated and inserted within the contour
lines,CS and H are the contour matrix output and object handle
outputs from CONTOUR,CONTOUR3,or CONTOURF.
Example:
z=peaks;
contourf(z),
hold on,
shading flat
[c,h]=contour(z,'k-');
clabel(c,h),
colorbar
4,网格图绘图函数(Net plot plotting function)
*ezmesh(f) 绘制由表达式f(x,y)定义的网格图,自变量x和y的变化范围为[-2π,2π];
*ezmesh(f,domain) 绘制由表达式f(x,y)定义的网格图,自变量 x和 y的变化范围由domain确定,domain可以是4×1的矢量[xmin,xmax,ymin,ymax],也可以是2×1的矢量[min,max],当domain为的矢量时,min<x<max,min<y<max。
*ezmesh(x,y,z) 绘制由表达式x=x(s,t),y=y(s,t)和z=z(s,t)定义的参数表面网格图,自变量s和t的变化范围为[-2π,2π];
*ezmesh(x,y,z,[smin,smax,tmin,tmax]) 绘制由表达式x=x(s,t),y=y(s,t)和z=z(s,t)定义的参数表面网格图,自变量s和t的变化范围为[smin,smax,tmin,tmax];
*ezmesh(…,n) 绘网格图时按n×n的网格密度绘图,n的缺省值为60;
*ezmesh(…,’circ’) 以圆盘为自变量域绘制网格图,
例1:根据表达式f=,绘制f的网格图.
syms x y
ezmesh(x*exp(-x^2-y^2),[-2.5,2.5],40)
colormap([0 0 1])
例2:根据表达式,以圆盘为自变量域绘制f的网格图.
syms x y
ezmesh(x*exp(-x^2-y^2),[-2.5,2.5],40,’circ’)
例3:根据表达式f=y/(1+x2+y2),绘制f的带等高线网格图。
syms x y
ezmeshc(y/(1+x^2+y^2),[-5,5,-2*pi,2*pi])
5,表面图绘图函数(surface plot plotting function)
*ezsurf(f) 绘制由表达式f(x,y)定义的表面图,自变量x和y的变化范围为[-2π,2π];
*ezsurf(f,domain) 绘制由表达式f(x,y)定义的表面图,自变量 x和 y的变化范围由domain确定,domain可以是4×1的矢量[xmin,xmax,ymin,ymax],也可以是2×1的矢量[min,max],当domain为的矢量时,min<x<max,min<y<max。
*ezsurf(x,y,z) 绘制由表达式x=x(s,t),y=y(s,t)和z=z(s,t)定义的参数表面图,自变量s和t的变化范围均为[-2π,2π];
ezsurf(x,y,z,[smin,smax,tmin,tmax]) 绘制由表达式x=x(s,t),y=y(s,t)和z=z(s,t)定义的参数表面图,自变量s和t的变化范围为[smin,smax,tmin,tmax];
*ezsurf(…,n) 绘表面图时按n×n的网格密度绘图,n的缺省值为60;
*ezsurf(…,’circ’) 以圆盘为自变量域绘制表面图,
例1:根据表达式x=cos(s)cos(t),y=cos(s)sin(t) 和 z=sin(s),绘制表面图。
syms t s
x=cos(s)*cos(t);
y=cos(s)*sin(t);
z=sin(s);
ezsurf(x,y,z,[0,pi/2,0,3*pi/2])
view(17,40)
shading interp
例2:根据表达式f=y/(1+x2+y2),绘制f带等高线的表面图。
syms x y
ezsurfc(y/(1+x^2+y^2),[-5,5,-2*pi,2*pi],35)
view(-65,26)
五,图形化符号函数计算器(Interactive graphing calculator that manipulates
functions of a single variable.)
图形化符号函数计算器函数funtool,在命令窗口funtool,即可启动图形化符号函数计数器。其三个窗口分别为函数f的显示窗口,
函数g的显示窗口,另外一个是函数功能控制窗口。
1,输入框:在控制窗口的上方有4个输入框,用户可以在输入框中输入函数,4个窗分别为:
*f= 为图形窗口1 的控制函数,其缺省值为x;
*g= 为图形窗口2 的控制函数,其缺省值为1;
*x= 为两窗口函数的自变量的取值范围,缺省值为 [-2π,2π];
*a= 为常数的值,缺省值为1/2。
2,计算器的功能(Functions of the calculator)
(1) 函数的自运算,函数功能的控制窗口的第一排按钮为函数的自运算(表 5—2)按钮。
(2) 函数与常数的运算:函数功能的控制窗口的第二排按钮为函数与常数之间的运算(表 5—3)按钮。
(3) 两函数之间的运算:函数功能的控制窗口的第三排按钮为函数f与g之间的运算(表 5—3)按钮。
(4) 函数计算器的系统操作:函数功能的控制窗口的第四排按钮为函数计算器的系统操作,(表 5—5)按钮。
FUNTOOL A function calculator.
FUNTOOL is an interactive graphing calculator that manipulates
functions of a single variable,At any time,there are two functions
displayed,f(x) and g(x),The result of most operations replaces f(x).
The controls labeled 'f = ' and 'g = ' are editable text that may
be changed at any time to install a new function,The control
labeled 'x = ' may be changed to specify a new domain,The control
labeled 'a = ' may be changed to specify a new value of a parameter.
The top row of control buttons are unary function operators that
involve only f(x),These operators are:
df/dx - Symbolically differentiate f(x).
int f - Symbolically integrate f(x).
simple f - Simplify the symbolic expression,if possible.
num f - Extract the numerator of a rational expression.
den f - Extract the denominator of a rational expression.
1/f - Replace f(x) by 1/f(x).
finv - Replace f(x) by its inverse function.
The operators int(f) and finv may fail if the corresponding symbolic
expressions do not exist in closed form.
The second row of buttons translate and scale f(x) by the parameter 'a'.
The operations are:
f + a - Replace f(x) by f(x) + a.
f - a - Replace f(x) by f(x) - a.
f * a - Replace f(x) by f(x) * a.
f / a - Replace f(x) by f(x) / a.
f ^ a - Replace f(x) by f(x) ^ a.
f(x+a) - Replace f(x) by f(x + a).
f(x*a) - Replace f(x) by f(x * a).
The third row of buttons are binary function operators that
operate on both f(x) and g(x),The operations are:
f + g - Replace f(x) by f(x) + g(x).
f - g - Replace f(x) by f(x) - g(x).
f * g - Replace f(x) by f(x) * g(x).
f / g - Replace f(x) by f(x) / g(x).
f(g) - Replace f(x) by f(g(x)).
g = f - Replace g(x) by f(x).
swap - Interchange f(x) and g(x).
The first three buttons in the fourth row manage a list of functions.
The Insert button places the current active function in the list.
The Cycle button rotates the function list.
The Delete button removes the active function from the list,
The list of functions is named fxlist,A default fxlist containing
several interesting functions is provided.
The Reset button sets f,g,x,a and fxlist to their initial values.
The Help button prints this help text,
The Demo button poses the following challenge,Can you generate the
function sin(x) without touching the keyboard,using just the mouse?
The demo does it with a reset and then nine clicks,If you can do
it with fewer clicks,please send e-mail to moler@mathworks.com.
The Close button closes all three windows.