实验MATLAB符号运算 下载本文

>> y=sin('w')

??? Undefined function or method 'sin' for input arguments of type 'char'.

>> y=sym('w') y = w

>> f4=x^2+y+sin(2) f4 =

22109510062103075/4503599627370496+w 思考题:syms x 是不是相当于x?sym('x')?

理解:这两个式子是不相同的。syms x 是定义x为符号变量,而x?sym('x')?是给x赋值。

3. 符号复诊创建】 >> syms a1 a2 a3 a4 >> A=[a1 a2;a3 a4] >> A(1),A(3)

>> syms a1 a2 a3 a4 >> A=[a1 a2;a3 a4] A =

[ a1, a2] [ a3, a4]

>> A(1),A(3) ans = a1 ans =

5 / 18

a2 或者

>> B=sym('[ b1 b2;b3 b4]') >> c1=sym('sin(x)') >> c2=sym('x^2') >> c3=sym('3*y+z') >> c4=sym('3') >> C=[c1 c2;c3 c4]

>> B=sym('[ b1 b2;b3 b4]') B =

[ b1, b2] [ b3, b4]

>> c1=sym('sin(x)') c1 = sin(x)

>> c2=sym('x^2') c2 = x^2

>> c3=sym('3*y+z') c3 = 3*y+z

>> c4=sym('3') c4 = 3

6 / 18

>> C=[c1 c2;c3 c4] C =

[ sin(x), x^2] [ 3*y+z, 3]

练习:分别用sym和syms 创建符号表达式:f1?cosx??sin2x,f2?4.符号算术运算

1) 符号量相乘、相除

符号量相乘运算和数值量相乘一样,分成矩阵乘和数组乘。 >> a=sym(5);b=sym(7); >> c1=a*b >> c2=a/b

>> a=sym(5);B=sym([3 4 5]); >> C1=a*B,C2=a\\B >> syms a b

>> A=[5 a;b 3];B=[2*a b;2*b a]; >> C1=A*B, C2=A.*B >> C3=A\\B,C4=A./B

>> a=sym(5);b=sym(7); >> c1=a*b

c1 =

35

>> c2=a/b

c2 =

5/7

>> a=sym(5);B=sym([3 4 5 s] ??? s] |

Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.

>> a=sym(5);B=sym([3 4 5]); >> C1=a*B,C2=a\\B

7 / 18

ye?2t.

C1 =

[ 15, 20, 25] C2 =

[ 3/5, 4/5, 1]

>> syms a b

>> A=[5 a;b 3];B=[2*a b;2*b a]; >> C1=A*B, C2=A.*B C1 =

[ 10*a+2*a*b, 5*b+a^2] [ 2*a*b+6*b, b^2+3*a] C2 =

[ 10*a, a*b] [ 2*b^2, 3*a]

>> C3=A\\B,C4=A./B C3 =

[ 2*a*(b-3)/(-15+a*b), (a^2-3*b)/(-15+a*b)] [ 2*b*(a-5)/(-15+a*b), -(5*a-b^2)/(-15+a*b)] C4 =

[ 5/2/a, a/b] [ 1/2, 3/a]

8 / 18