Matlab符号函数练习

19 篇文章 8 订阅

1.用2种方式创建符号表达式f=sin(x)+e^x

方法一

clear
clc
x=sym('x');
f=sin(x)+exp(x)

结果

 
f =
 
exp(x) + sin(x)
 
>> 
方法二
clear
clc
syms x;
f=sin(x)+exp(x)
结果

 
f =
 
exp(x) + sin(x)
 
>> 

2.计算习题 1 中表达式在x=0、x=π/4、x=2π,以及ones(3,4)处的值

clear
clc
syms x;
f=sin(x)+exp(x)
subs(f, x, 0)
subs(f, x, pi/4)
subs(f, x, pi*2)
subs(f, x, ones(3,4))
结果

 
f =
 
exp(x) + sin(x)
 

ans =

     1


ans =

    2.9004


ans =

  535.4917


ans =

    3.5598    3.5598    3.5598    3.5598
    3.5598    3.5598    3.5598    3.5598
    3.5598    3.5598    3.5598    3.5598

>> 
3.计算习题 1 中表达式在x=π/6处的值,并将结果设置为以下 5 种精度: 1 位、2 位、5 位、10位和20位有效数字。
本题在高版本的Matlab(Matlab2010b)上无法设置1位精度。(所以本题1位精度不做了)

clear
clc
syms x;
f=sin(x)+exp(x);
y=subs(f, x, pi/6);
digits(2);
vpa(y)
digits(5);
vpa(y)
digits(10);
vpa(y)
digits(20);
vpa(y)
结果

 
ans =
 
2.2
 
 
ans =
 
2.1881
 
 
ans =
 
2.188091795
 
 
ans =
 
2.1880917949644684839
 
>> 

方法二:

clear
clc
syms x;
f=sin(x)+exp(x);
y=subs(f, x, pi/6);
%vpa(y,1)
vpa(y,2)
vpa(y,5)
vpa(y,10)
vpa(y,20)

结果

 
ans =
 
2.2
 
 
ans =
 
2.1881
 
 
ans =
 
2.188091795
 
 
ans =
 
2.1880917949644684839
 
>> 



4.设x为符号变量,f(x)=x^4+x^2+1,g(x)=x^3+4x^2+5x+8,试进行如下运算
(1)f(x)+g(x)
(2)f(x)×g(x)
(3)对f(x)进行因式分解
(4)求g(x)的反函数
(5)求以g为f(x)自变量的复合函数
(6)用2个命令分别化简f*g的结果

clear
clc
syms x;
f=x^4+x^2+1;
g=x^4+4*x^2+5*x+8;
f+g
f*g
factor(f)
finverse(g)
compose(f,g)
simplify(f)
simple(f)

结果

 
ans =
 
2*x^4 + 5*x^2 + 5*x + 9
 
 
ans =
 
(x^4 + x^2 + 1)*(x^4 + 4*x^2 + 5*x + 8)
 
 
ans =
 
(x^2 - x + 1)*(x^2 + x + 1)
 
 
ans =
 
RootOf(X10^4 + 4*X10^2 + 5*X10 - x + 8, X10)
 
 
ans =
 
(x^4 + 4*x^2 + 5*x + 8)^2 + (x^4 + 4*x^2 + 5*x + 8)^4 + 1
 
 
ans =
 
x^4 + x^2 + 1
 
 
simplify:
 
x^4 + x^2 + 1
 
 
radsimp:
 
x^4 + x^2 + 1
 
 
simplify(100):
 
x^4 + x^2 + 1
 
 
combine(sincos):
 
x^4 + x^2 + 1
 
 
combine(sinhcosh):
 
x^4 + x^2 + 1
 
 
combine(ln):
 
x^4 + x^2 + 1
 
 
factor:
 
(x^2 - x + 1)*(x^2 + x + 1)
 
 
expand:
 
x^4 + x^2 + 1
 
 
combine:
 
x^4 + x^2 + 1
 
 
rewrite(exp):
 
x^4 + x^2 + 1
 
 
rewrite(sincos):
 
x^4 + x^2 + 1
 
 
rewrite(sinhcosh):
 
x^4 + x^2 + 1
 
 
rewrite(tan):
 
x^4 + x^2 + 1
 
 
mwcos2sin:
 
x^4 + x^2 + 1
 
 
collect(x):
 
x^4 + x^2 + 1
 
 
ans =
 
x^4 + x^2 + 1
 
>> 

5.合并同类项
(1)3x-2x^2+5+3x^2-2x-5
(2)3x^2*y-4xy^2-3+5x^2*y+2xy^2+5(分别对x和y)
(3)2x^2-3xy+y^2-2xy-2x^2+5xy-2y+1(分别对x和y)

clear
clc
syms x y;
collect(3*x-2*x^2+5+3*x^2-2*x-5)
collect(3*x^2*y-4*x*y^2-3+5*x^2*y+2*x*y^2+5,x)
collect(3*x^2*y-4*x*y^2-3+5*x^2*y+2*x*y^2+5,y)
collect(2*x^2-3*x*y+y^2-2*x*y-2*x^2+5*x*y-2*y+1,x)
collect(2*x^2-3*x*y+y^2-2*x*y-2*x^2+5*x*y-2*y+1,y)


结果

 
ans =
 
x^2 + x
 
 
ans =
 
(8*y)*x^2 + (-2*y^2)*x + 2
 
 
ans =
 
(-2*x)*y^2 + (8*x^2)*y + 2
 
 
ans =
 
y^2 - 2*y + 1
 
 
ans =
 
y^2 - 2*y + 1
 
>>

改进

clear
clc
syms x y;
f1=3*x-2*x^2+5+3*x^2-2*x-5;
f2=3*x^2*y-4*x*y^2-3+5*x^2*y+2*x*y^2+5;
f3=2*x^2-3*x*y+y^2-2*x*y-2*x^2+5*x*y-2*y+1;
collect(f1)
collect(f2,x)
collect(f2,y)
collect(f3,x)
collect(f3,y)

结果

 
ans =
 
x^2 + x
 
 
ans =
 
(8*y)*x^2 + (-2*y^2)*x + 2
 
 
ans =
 
(-2*x)*y^2 + (8*x^2)*y + 2
 
 
ans =
 
y^2 - 2*y + 1
 
 
ans =
 
y^2 - 2*y + 1
 
>> 
6.因式分解
(1)将 7798666 进行因数分解,分解为素数乘积的形式
(2)-2m^8+512
(3)3a^2*(x-y)^3-4b^2*(y-x)^2
(1)

clear
clc
factor(7798666)
结果

ans =

           2          67       58199

>> 
(2)

clear
clc
syms m;
factor(-2*m^8+512)
结果

 
ans =
 
-2*(m - 2)*(m + 2)*(m^2 + 4)*(m^4 + 16)
 
>> 
(3)
clear
clc
syms a b x y;
factor(3*a^2*(x-y)^3-4*b^2*(y-x)^2)
结果

 
ans =
 
(x - y)^2*(3*a^2*x - 3*a^2*y - 4*b^2)
 
>> 

7.计算下列各式
求2x^2-3xy+y^2-2xy-2x^2+5xy-2y+1在(2,5)的值;将(x,y)替换为{a,rand(2,3)};将(x,y)替换为{a+bt,sin(2t)}
注:本题修改为2x^2+3xy+y^2-2xy-2x^2+5xy-2y+1,第二题矩阵维数不对,这里不做

clear
clc
syms x y;
syms a b t;
f=2*x^2+3*x*y+y^2-2*x*y-2*x^2+5*x*y-2*y+1;
subs(f,{x,y},{2,5})
%subs(f,{x,y},[a,rand(2,3)])  %不能换
subs(f,{x,y},[a+b*t,sin(2*t)])
结果

ans =

    76

 
ans =
 
6*sin(2*t)*(a + b*t) - 2*sin(2*t) + sin(2*t)^2 + 1
 
>>
8.符号矩阵
R=[cos(a)*cos(c)-sin(a)*sin(b)*sin(c) -sin(a)*cos(b) cos(a)*sin(c)+sin(a)*sin(b)*cos(c);
sin(a)*cos(c)+cos(a)*sin(b)*sin(c) cos(a)*cos(b) sin(a)*sin(c)-cos(a)*sin(b)*cos(c);
-cos(b)*sin(c) sin(b) cos(b)*cos(c)]
,求该矩阵的秩,逆及行列式,

clear
clc
syms a b c;
R=[cos(a)*cos(c)-sin(a)*sin(b)*sin(c) -sin(a)*cos(b) cos(a)*sin(c)+sin(a)*sin(b)*cos(c);
sin(a)*cos(c)+cos(a)*sin(b)*sin(c) cos(a)*cos(b) sin(a)*sin(c)-cos(a)*sin(b)*cos(c);
-cos(b)*sin(c) sin(b) cos(b)*cos(c)]
rank(R)
inv(R)
det(R)

结果

 
R =
 
[ cos(a)*cos(c) - sin(a)*sin(b)*sin(c), -cos(b)*sin(a), cos(a)*sin(c) + cos(c)*sin(a)*sin(b)]
[ cos(c)*sin(a) + cos(a)*sin(b)*sin(c),  cos(a)*cos(b), sin(a)*sin(c) - cos(a)*cos(c)*sin(b)]
[                       -cos(b)*sin(c),         sin(b),                        cos(b)*cos(c)]
 
 
ans =
 
3
 
 
ans =
 
[ cos(a)*cos(c) - sin(a)*sin(b)*sin(c), cos(c)*sin(a) + cos(a)*sin(b)*sin(c), -cos(b)*sin(c)]
[                       -cos(b)*sin(a),                        cos(a)*cos(b),         sin(b)]
[ cos(a)*sin(c) + cos(c)*sin(a)*sin(b), sin(a)*sin(c) - cos(a)*cos(c)*sin(b),  cos(b)*cos(c)]
 
 
ans =
 
cos(a)^2*cos(b)^2*cos(c)^2 + cos(a)^2*cos(b)^2*sin(c)^2 + cos(a)^2*cos(c)^2*sin(b)^2 + cos(a)^2*sin(b)^2*sin(c)^2 + cos(b)^2*cos(c)^2*sin(a)^2 + cos(b)^2*sin(a)^2*sin(c)^2 + cos(c)^2*sin(a)^2*sin(b)^2 + sin(a)^2*sin(b)^2*sin(c)^2
 
>> 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值