subs
Symbolic substitution in symbolic expression_r_r_r or matrix
在符号表达式或矩阵中进行符号替换
Syntax
R = subs(S)
R = subs(S, new)
R = subs(S, old, new)
Description
R = subs(S) replaces alloccurrences of variables in the symbolic expression_r_r_r Swith values obtained from the calling function, or the MATLABworkspace.
R = subs(S, new) replaces thedefault symbolic variable in S with new.
R = subs(S, old, new) replacesold with new in the symbolic expression_r_r_rS.
old is a symbolic variable or a string representing avariable name. new is a symbolic or numeric variable orexpression_r_r_r. That is, R = subs(S,old,new) uates S at old =new. The substitution is first attempted as a MATLABexpression_r_r_r resulting in the computation being done in doubleprecision arithmetic if all the values in new are doubleprecision. Convert the new values to sym toensure symbolic or variable precision arithmetic.
If old and new are vectors or cell arrays ofthe same size and type, each element of old is replaced bythe corresponding element of new. If S andold are scalars and new is an array or cellarray, the scalars are expanded to produce an array result. Ifnew is a cell array of numeric matrices, the substitutionsare performed elementwise (i.e., subs(x*y,{x,y},{A,B}) returns A.*B whenA and B are numeric).
If subs(s,old,new) does notchange s, subs(s,new,old)is tried. This provides backwards compatibility with previousversions and eliminates the need to remember the order of thearguments. subs(s,old,new,0) doesnot switch the arguments if s does not change.
R = subs(S)用由调用函数或Matlab工作空间中获取的值替代了在符号表达式S中的所有当前的变量。
R = subs(S, new)利用new的值代替符号表达式S中的默认符号。
R = subs(S, old,new) 利用new的值代替符号表达式中old的值,old为符号变量或是字符串变量名。new是一个符号货数值变量或表达式。也就是说R= subs(S,old,new)在old=new的条件下重新计算了表达式S。这种替换第一次作为Matlab表达式被尝试,如果所有在new中的数值是双精度的,计算是以双精度算术运算进行的。讲new值转化为符号可以验证符号货变量的运算精度。
如果old和new是大小和类型相同的向量或是元包数组,每一个old的元素都将被相应新的元素替换。
如果S和old是标量,new是数组或元包数组,则扩展标量去计算一个数组结果。
如果new是个数值矩阵元包数组,替换讲运行为/智能元素/(i.e.,subs(x*y,{x,y},{A,B}) returns A.*B whenA and B are numeric)。
如果subs(s,old,new) 没有改变S,则将会试行subs(s,new,old)。这将为前面的版本提供向前的兼容性,消除记忆参数顺序。subs(s,old,new,0)不改变参数,如果S没有改变。
一个较为综合的例子:http://blog.sina.com.cn/s/blog_4b94ff130100ge7n.html
Examples
Single Input
Suppose a = 980 and C2 = 3 exist in theworkspace.
The statement
y = dsolve('Dy = -a*y')
produces
y =C2/exp(a*t)
Then the statements
a = 980; C2 = 3; subs(y)
produce
ans =3/exp(980*t)
Single Substitution
syms a b;subs(a + b, a, 4)
returns
ans =b + 4
Multiple Substitutions
syms a b;subs(cos(a) + sin(b), {a, b}, {sym('alpha'), 2})
returns
ans =sin(2) + cos(alpha)
Scalar Expansion Case
syms t;subs(exp(a*t), 'a', -magic(2))
returns
ans =[ 1/exp(t--), 1/exp(3*t)][ 1/exp(4*t), 1/exp(2*t)]
Multiple Scalar Expansion
syms x y;subs(x*y, {x, y}, {[0 1; -1 0], [1 -1; -2 1]})
returns
ans = 0 -1 2 0