a=1/3;
format rat;
a
结果果然是a=1/3    rats(a)
一旦创建了一个符号表达式,或许想以某些方式改变它;也许希望提取表达式的一部分,合并两个表达式或求得表达的数值。有许多符号工具可以帮助完成这些任务。
  所有符号函数(很少特殊例外的情况,讨论于后)作用到符号表达式和符号数组,并返回符号表达式或数组。其结果有时可能看起来象一个数字,但事实上它是一个内部用字符串表示的一个符号表达式。正如我们前面所讨论的,可以运用MATLAB函数isstr来出像似数字的表达式是否真是一个整数或是一个字符串。
  提取分子和分母
  如果表达式是一个有理分式(两个多项式之比),或是可以展开为有理分式(包括哪些分母为1的分式),可利用numden来提取分子或分母。例如,给定如下的表达式:
  在必要时,numden将表达式合并、有理化并返回所得的分子和分母。进行这项运算的MATLAB语句是:
view plaincopy to clipboardprint?
 >> m= ' x^2 ' % create a simple expression 
  m= 
  x^2 
  >> [n,d]=numden(m) % extract the numerator and denominator 
  n= 
  x^2 
  d= 
  1 
  >> f= ' a*x^2/(b-x) ' % create a rational expression 
  f= 
  a*x^2/(b-x) 
  >> [n,d]=numden(f) % extract the numerator and denominator 
  n= 
  a*x^2 
  d= 
  b-x 
 >> m= ' x^2 ' % create a simple expression
  m=
  x^2
  >> [n,d]=numden(m) % extract the numerator and denominator
  n=
  x^2
  d=
  1
  >> f= ' a*x^2/(b-x) ' % create a rational expression
  f=
  a*x^2/(b-x)
  >> [n,d]=numden(f) % extract the numerator and denominator
  n=
  a*x^2
  d=
  b-x
  前二个表达式得到期望结果。
  view plaincopy to clipboardprint?
>> g= ' 3/2*x^2+2/3*x-3/5 ' % rationalize and extract the parts 
  g= 
  3/2*x^2+2/3*x-3/5 
  >> [n,d]=numden(g) 
  n= 
  45*x^2+20*x-18 
  d= 
  30 
  >> h= ' (x^2+3)/(2*x-1)+3*x/(x-1) ' % the sum of rational polynomials 
  h= 
  (x^2+3)/(2*x-1)+3*x/(x-1) 
  >> [n,d]=numden(h) % rationalize and extract 
  n= 
  x^3+5*x^2-3 
  d= 
  (2*x-1)*(x-1) 
>> g= ' 3/2*x^2+2/3*x-3/5 ' % rationalize and extract the parts
  g=
  3/2*x^2+2/3*x-3/5
  >> [n,d]=numden(g)
  n=
  45*x^2+20*x-18
  d=
  30
  >> h= ' (x^2+3)/(2*x-1)+3*x/(x-1) ' % the sum of rational polynomials
  h=
  (x^2+3)/(2*x-1)+3*x/(x-1)
  >> [n,d]=numden(h) % ration
alize and extract
  n=
  x^3+5*x^2-3
  d=
  (2*x-1)*(x-1)
  在提取各部分之前,这二个表达式g和h被有理化,并变换成具有分子和分母的一个简单表达式。
 view plaincopy to clipboardprint?
 >> k="sym"( ' [3/2,(2*x+1)/3;4/x^2,3*x+4] ' ) % try a symbolic array 
  k= 
  [ 3/2,(2*x+1)/3] 
  [4/x^2, 3*x+4] 
  >> [n,d]=numden(k) 
  n= 
  [3, 2*x+1] 
  [4, 3*x+4] 
  d= 
  [ 2,3] 
  [x^2,1] 
 >> k="sym"( ' [3/2,(2*x+1)/3;4/x^2,3*x+4] ' ) % try a symbolic array
  k=
  [ 3/2,(2*x+1)/3]
  [4/x^2, 3*x+4]
  >> [n,d]=numden(k)
  n=
  [3, 2*x+1]
  [4, 3*x+4]
  d=
  [ 2,3]
  [x^2,1]
  这个表达式k是符号数组,numden返回两个新数组n和d,其中n是分子数组,d是分母数组。如果采用s=numden(f)形式,numden仅把分子返回到变量s中。
  标准代数运算
  很多标准的代数运算可以在符号表达式上执行,函数symadd、symsub、symlnul和symdiv为加、减、乘、除两个表达式,sympow将一个表达式上升为另一个表达式的幂次(MATLAB 7.0中不可用)。例如: 给定两个函数
  view plaincopy to clipboardprint?
>> f= ' 2*x^2+3*x-5 ' % define the symbolic expression 
  f= 
  2*x^2+3*x-5 
  >> g= ' x^2-x+7 ' 
  g= 
  x^2-x+7 
  >> symadd(f,g) % find an expression for f+g 
  ans= 
  3*x^2+2*x+2 
  >> symsub(f,g) % find an expression for f-g 
  ans= 
  x^2+4*x-12 
  >> symmul(f,g) % find an expression for f*g 
  ans= 
  (2*x^2+3*x-5)*(x^2-x+7) 
  >> symdiv(f,g) % find an expression for f/g 
  ans= 
  (2*x^2+3*x-5)/(x^2-x+7) 
  >> sympow(f, ' 3*x ' ) % find an expression for ans= 
  (2*x^2+3*x-5)^3** 
>> f= ' 2*x^2+3*x-5 ' % define the symbolic expression
  f=
  2*x^2+3*x-5
  >> g= ' x^2-x+7 '
  g=
  x^2-x+7
  >> symadd(f,g) % find an expression for f+g
  ans=
  3*x^2+2*x+2
  >> symsub(f,g) % find an expression for f-g
  ans=
  x^2+4*x-12
  >> symmul(f,g) % find an expression for f*g
  ans=
  (2*x^2+3*x-5)*(x^2-x+7)
  >> symdiv(f,g) % find an expression for f/g
  ans=
  (2*x^2+3*x-5)/(x^2-x+7)
  >> sympow(f, ' 3*x ' ) % find an expression for ans=
  (2*x^2+3*x-5)^3**
  另一个通用函数可让用户用其它的符号变量、表达式和算子创建新的表达式。symop取由逗号隔开的、多至16个参量。各个参量可为符号表达式、数值或算子(' + '、' - '、'*'、' / '、' ^ '、'
( '或' ) '),然后symop可将参量联接起来,返回最后所得的表达式.
 view plaincopy to clipboardprint?
 >> f= ' cos(x) ' % create an expression 
  f= 
  cos(x) 
  >> g= ' sin(2*x) ' % create another expression 
  g= 
  sin(2*x) 
  >> symop(f,'/ ',g,'+',3) % combine them 
  ans= 
  cos(x)/sin(2*x)+3 
 >> f= ' cos(x) ' % create an expression
  f=
  cos(x)
  >> g= ' sin(2*x) ' % create another expression
  g=
  sin(2*x)
  >> symop(f,'/ ',g,'+',3) % combine them
  ans=
  cos(x)/sin(2*x)+3
  所有这些运算也同样用数组参量进行。
  高级运算
  MATLAB具有对符号表达式执行更高级运算的功能。函数compose把f(x)和g(x)复合成f(g(x))。函数finverse求表达式的函数逆,而函数symsum求表达式的符号和。
  给定表达式
  view plaincopy to clipboardprint?
>> f= ' 1/(1+x^2) ' ; % create the four expression 
  >> g= ' sin(x) ' ; 
  >> h= ' 1/(1+u^2) ' ; 
  >> k=' sin(v) ' ; 
  >> compose(f,g) % find an expression for f(g(x)) 
  ans= 
  1/(1+sin(x)^2) 
  >> compose(g,f) % find an expression for g(f(x)) 
  ans= 
  sin(1/(1+x^2)) 
>> f= ' 1/(1+x^2) ' ; % create the four expression
  >> g= ' sin(x) ' ;
  >> h= ' 1/(1+u^2) ' ;
  >> k=' sin(v) ' ;
  >> compose(f,g) % find an expression for f(g(x))
  ans=
  1/(1+sin(x)^2)
  >> compose(g,f) % find an expression for g(f(x))
  ans=
  sin(1/(1+x^2))
  compose也可用于含有不同独立变量的函数表达式。
  view plaincopy to clipboardprint?
>> compose(h,k,'u','v') % given h(u),k(v),find(k(v)) 
  ans= 
  1/(1+sin(v)^2) 
>> compose(h,k,'u','v') % given h(u),k(v),find(k(v))
  ans=
  1/(1+sin(v)^2)
  表达式譬如f(x)的函数逆g(x),满足g(f(x))=x。例如, 的函数逆是ln(x),因为ln( )=x。sin(x)的函数逆是arcsin(x),函数 的函数逆是arcsin 。函数fincerse返回表达式的函数逆。如果解不是唯一就给出警告。
  view plaincopy to clipboardprint?
>> finverse( ' 1/x) % the inverse of 1/x is 1/x since ' 1/(1/x)=x ' 
  ans= 
  1/x 
  >> finverse( ' x^2 ' ) % g(x^2)=x has more than one solution 
  Warning: finverse(x^2) is not unique 
  ans= 
  x^(1/2) 
  >> finverse( ' a*x+b ' ) % find the solution to ' g(f(x))=x ' 
  ans= 
  -(b-x)/a 
  >> finverse( ' a*b+c*d-a*z ' ), ' a ' ) % find the solution to ' g(f(a))=a ' 
  ans= 
  -(c*d-a)/(b-z) 
>> finverse( ' 1/x) % the inverse of 1/x is 1/x since ' 1/(1/x)=x '
 ans=
  1/x
  >> finverse( ' x^2 ' ) % g(x^2)=x has more than one solution
  Warning: finverse(x^2) is not unique
  ans=
  x^(1/2)
  >> finverse( ' a*x+b ' ) % find the solution to ' g(f(x))=x '
  ans=
  -(b-x)/a
  >> finverse( ' a*b+c*d-a*z ' ), ' a ' ) % find the solution to ' g(f(a))=a '
  ans=
  -(c*d-a)/(b-z)
  symsun函数求表达式的符号和有四种形式:symsun(f)返回 ;symsum(f, ' s ' )返回 ,symsun(f,a,b)返回 ;最普通的形式symsun(f, ' s ' ,a,b)返回 。
  让我们试一试 ,它应返回: 。
  view plaincopy to clipboardprint?
>> symsum('x^2') 
  ans= 
  1/3*x^3-1/2* x^2+1/6*x 
  又怎么样呢?它应返回 。 
  >> sym('(2*n-1)^2',1,'n') 
  ans= 
  11/3*n+8/3-4*(n+1)^2+4/3*(n+1)^3 
  >> factor(ans) % change the form ( we will revisit 'factor' later on) 
  ans= 
  1/3*n*(2*n-1)*(2*n+1) 
  最后让我们试一试 ,其返回应是 。 
  >> symsum( ' 1/(2*n-1)^2 ' ,1,inf) 
  ans= 
  1/8*pi^2 
>> symsum('x^2')matlab二进制字符串转数组
  ans=
  1/3*x^3-1/2* x^2+1/6*x
  又怎么样呢?它应返回 。
  >> sym('(2*n-1)^2',1,'n')
  ans=
  11/3*n+8/3-4*(n+1)^2+4/3*(n+1)^3
  >> factor(ans) % change the form ( we will revisit 'factor' later on)
  ans=
  1/3*n*(2*n-1)*(2*n+1)
  最后让我们试一试 ,其返回应是 。
  >> symsum( ' 1/(2*n-1)^2 ' ,1,inf)
  ans=
  1/8*pi^2
  变换函数
  本节提出许多工具,将符号表达式变换成数值或反之。有极少数的符号函数可返回数值。然而请注意,某些符号函数能自动地将一个数字变换成它的符号表达式,如果该数字是函数许多参量中的一个。
  函数sym可获取一个数字参量并将其转换为符号表达式。函数numneric的功能正好相反,它把一个符号常数(无变量符号表达式)变换为一个数值。
  view plaincopy to clipboardprint?
>> phi=' (1+sqrt(5))/2 ' % the ' golden ' ratio 
  phi= 
  (1+sqrt(5))/2 % convert to a numeric value 
  >> numeric(phi) 
  ans= 
  1.6180 
>> phi=' (1+sqrt(5))/2 ' % the ' golden ' ratio
  phi=
  (1+sqrt(5))/2 % convert to a numeric value
  >> numeric(phi)
  ans=
  1.6180
  正如第六章所介绍,函数eval将字符串传给MATLAB以便计算。所以eval是另一个可用于把符号常数变换为数字或计算表达式的函数。
  >> eval(phi) % execute the string ' (1+sqrt(5))/2
  ans=
  1.6180
  正如所期望那样,numeric和eval返回相同数值。
  符号函数sym2poly将符号多项式变换成它的MATLAB等价系数向量。
函数poly2syrn功能正好相反,并让用户指定用于所得结果表达式中的变量。
  view plaincopy to clipboardprint?
>> f=' 2*x^2+x^3-3*x+5 ' % f is the symbolic polynomials 
  f= 
  2*x^2+x^3-3*x+5 
  >> n="sym2poly"(f) % extract eht numeric coefficient vector 
  n= 
  1 2 -3 5 
  >> poly2sym(n) % recreate the polynomials in x (the default) 
  ans= 
  2*x^2+x^3-3*x+5 
  >> poly2sym(n,' s ') % recreate the polynomials in s 
  ans= 
  s^3+2*s^2-3*s+5 
>> f=' 2*x^2+x^3-3*x+5 ' % f is the symbolic polynomials
  f=
  2*x^2+x^3-3*x+5
  >> n="sym2poly"(f) % extract eht numeric coefficient vector
  n=
  1 2 -3 5
  >> poly2sym(n) % recreate the polynomials in x (the default)
  ans=
  2*x^2+x^3-3*x+5
  >> poly2sym(n,' s ') % recreate the polynomials in s
  ans=
  s^3+2*s^2-3*s+5
  变量替换
  假设有一个以x为变量的符号表达式,并希望将变量转换为y。MATLAB提供一个工具称作subs,以便在符号表达式中进行变量替换。其格式为 subs(f,old,new),其中f是符号表达式,new和old是字符、字符串或其它符号表达式。‘新’字符串将代替表达式f中各个‘旧’字符串。以下有几个例子:
  view plaincopy to clipboardprint?
>> f= ' a*x^2+b*x+c ' % create a function f(x) 
  f= 
  a*x^2+b*x+c 
  >> subs(f,' s ',' x ') % substitute ' s ' for ' x ' in the expression f 
  ans= 
  a*s^2+b*s+c 
  >> subs(f,' alpha ',' a ') % substitute ' alpha ' for ' a ' in f 
  ans= 
  alpha*x^2+b*x+c 
  >> g=' 3*x^2+5*x-4 ' % create another function 
  g= 
  3*x^2+5*x-4 
  >> h="subs"(g,' 2 ',' x ') % substitute ' 2 ' for ' x ' in g 
  h= 
  18 
  >> isstr(h) % show that the result is a symbolic expression 
  ans= 
  1 
>> f= ' a*x^2+b*x+c ' % create a function f(x)
  f=
  a*x^2+b*x+c
  >> subs(f,' s ',' x ') % substitute ' s ' for ' x ' in the expression f
  ans=
  a*s^2+b*s+c
  >> subs(f,' alpha ',' a ') % substitute ' alpha ' for ' a ' in f
  ans=
  alpha*x^2+b*x+c
  >> g=' 3*x^2+5*x-4 ' % create another function
  g=
  3*x^2+5*x-4
  >> h="subs"(g,' 2 ',' x ') % substitute ' 2 ' for ' x ' in g
  h=
  18
  >> isstr(h) % show that the result is a symbolic expression
  ans=
  1
  最后一个例子表明subs如何进行替换,并力图简化表达式。因为替换结果是一个符号常数,MATLLAB可以将其简化为一个符号值。注意,因为 subs是一个符号函数,所以它返回一个符号表达式。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。