实验二2MATLAB的符号计算及可视化
实验二 MATLAB的符号计算与可视化
1:完成教材实验三第1节“1.创建符号表达式和符号表达式的操作”中(1)-(5)部分的内容,分别用sym和syms创建符号表达式f和g,并对它们进行相关操作,思考每一条命令的作用是什么,并提交命令行和结果;
(1)创建符号变量。
①使用sym命令创建符号表达式:
>> f=sym('sin(x)')
f =
sin(x)
>> g=sym('y/exp(-2*t)')
g =
y*exp(2*t)
②使用syms命令创建符号表达式:
>> syms x y t
>> f=sym(sin(x))
f =
sin(x)
>> g=sym(y/exp(-2*t))
g =
y*exp(2*t)
(2):自由变量的确定:
>> symvar(g)
ans =
[ t, y]
>> symvar(g,1)
ans =
y
>> findsym(g,2)
ans =
y,t
(3):用常数替换符号变量:
>> x=0:10;
>> y=subs(f,x)
y =
Columns 1 through 8
Columns 9 through 11
练习:用y替换x,查看结果及其数据类型。
z=subs(f,y)
z =
Columns 1 through 8
Columns 9 through 11
>> class(z)
ans =
double
(4):符号对象与数值的转换和任意精度控制:
>> f1=subs(f,'5')
f1 =
sin(5)
>> y1=double(f1)
y1 =
>> y2=eval(f1)
y2 =
练习:将y1用sym函数转换为符号对象,并用’d’,’f’,’e’,’r’4种格式表示。>> y2=sym(y1,'d')
y2 =
>> vpa(y2,8)
ans =
-0.
>> class(y2)
ans =
sym
>> y3=sym(y1,'f')
y3 =
-/9007
>> y4=sym(y1,'e')
y4 =
-/9007
>> y5=sym(y1,'r')
y5 =
-/9007
采用digits和vpa实现任意精度控制:
>> digits
Digits = 32
>> vpa(f1)
ans =
>> vpa(f1,10)
ans =
(5):求反函数和复合函数
①用finverse函数求f,g的反函数
>> f=sym('sin(x)');
>> g=sym('y/exp(-2*t)')
g =
y*exp(2*t)
>> finverse(f)
Warning: finverse(sin(x)) is not unique.
ans =
asin(x)
>> finverse(g)
ans =
y/exp(2*t)
>> finverse(g,'t'
ans =
log(t/y)/2
②用compose函数求f,g的复合函数
>> compose(f,g)
ans =
sin(y*exp(2*t))
>> compose(f,g,'z')
ans =
sin(z*exp(2*t))
2;自建两个一元四次符号表达式,分别进行其符号表达式的加、减、乘等运算,并提交命令行和结果;
>> syms x
>> f=x^4+2*x^3-5*x^2+6*x+8
f =
x^4 + 2*x^3 - 5*x^2 + 6*x + 8
>> class(f)
ans =
sym
>> g=2*x^4-5*x^3+8*x^2+7*x-2
g =
2*x^4 - 5*x^3 + 8*x^2 + 7*x - 2
>> f+g
ans =
3*x^4 - 3*x^3 + 3*x^2 + 13*x + 6
>> f-g
ans =
- x^4 + 7*x^3 - 13*x^2 - x + 10
>> f*g
(x^4 + 2*x^3 - 5*x^2 + 6*x + 8)*(2*x^4 - 5*x^3 + 8*x^2 + 7*x - 2)
3:自建一个可化简一元五次多项式和一个三角函数符号表达式,依次使用pretty, horner, factor, simplify和simple等函数对该表达式进行化简,并提交命令行和结果;
syms x y
>> f=x^5+x^4+2*x+2
f =
x^5 + x^4 + 2*x + 2
>> g=cos(y)^2-sin(y)^2
g =
cos(y)^2 - sin(y)^2
>> class(f)
ans =
sym
>> class(g)
ans =
sym
>> pretty(f)
5 4
x + x + 2 x + 2
>> horner(f)
ans =
x*(x^3*(x + 1) + 2) + 2
>> factor(f)
ans =
(x + 1)*(x^4 + 2)
>> simplify(f)
ans =
(x^4 + 2)*(x + 1)
>> simple(f)
simplify:
x^5 + x^4 + 2*x + 2
radsimp:
x^5 + x^4 + 2*x + 2
simplify(100):
(x^4 + 2)*(x + 1)
combine(sincos):
x^5 + x^4 + 2*x + 2
combine(sinhcosh):
x^5 + x^4 + 2*x + 2
combine(ln):
x^5 + x^4 + 2*x + 2
(x + 1)*(x^4 + 2)
expand:
x^5 + x^4 + 2*x + 2
combine:
x^5 + x^4 + 2*x + 2
rewrite(exp):
x^5 + x^4 + 2*x + 2
rewrite(sincos):
x^5 + x^4 + 2*x + 2
rewrite(sinhcosh):
x^5 + x^4 + 2*x + 2
rewrite(tan):
x^5 + x^4 + 2*x + 2
mwcos2sin:
x^5 + x^4 + 2*x + 2
collect(x):
x^5 + x^4 + 2*x + 2
ans =
(x^4 + 2)*(x + 1)
>> pretty(g)
2 2
cos(y) - sin(y)
>> horner(g)
ans =
cos(y)^2 - sin(y)^2
>> factor(g)
ans =
(cos(y) - sin(y))*(cos(y) + sin(y)) >> simplify(g)
ans =
cos(2*y)
>> simple(g)
simplify:
cos(2*y)
radsimp:
cos(y)^2 - sin(y)^2
simplify(100):
cos(2*y)
combine(sincos):
cos(2*y)
combine(sinhcosh):
cos(y)^2 - sin(y)^2
combine(ln):
cos(y)^2 - sin(y)^2
factor:
(cos(y) - sin(y))*(cos(y) + sin(y))
expand:
cos(y)^2 - sin(y)^2
combine:
cos(y)^2 - sin(y)^2
rewrite(exp):
((1/exp(y*i))/2 + exp(y*i)/2)^2 - ((i*exp(i*y))/2 - i/(2*exp(i*y)))^2
rewrite(sincos):
cos(y)^2 - sin(y)^2
rewrite(sinhcosh):
cosh(-y*i)^2 + sinh(-y*i)^2
rewrite(tan):
(tan(y/2)^2 - 1)^2/(tan(y/2)^2 + 1)^2 - (4*tan(y/2)^2)/(tan(y/2)^2 + 1)^2
mwcos2sin:
1 - 2*sin(y)^2
collect(y):
cos(y)^2 - sin(y)^2
ans =
cos(2*y)
4:完成教材实验四第1节“1.绘制二维图线”中的所有内容,绘制4种二维曲线,把图形窗口分割为2行2列,并分别标明图名、坐标值等;
M文件:
clf;
clc;
clear;
%2行2列子图的第1个图
figure(1);
subplot(2,2,1);
t1=0::2;
y1=sin(2*pi*t1);
plot(t1,y1);
title('y=sin(2\pit)');
%2行2列子图的第2个图
subplot(2,2,2);
t2=0::2;
y2=[exp(-t2);exp(-2*t2);exp(-3*t2)];
plot(t2,y2);
axis([0 2 ]);
title('y=e-t,y=e-2t,y=e-3t');
%2行2列子图的第3个图
subplot(2,2,3);
t3=[0 1 1 2 2 3 4];
y3=[0 0 2 2 0 0 0];
plot(t3,y3);
axis([0 4 3]);
title('脉冲信号');
%2行2列子图的第4个图
subplot(2,2,4);
t4=0::2*pi;
plot(sin(t4),cos(t4));
axis([ ]);
axis equal;
title('圆');
插图:
5:参照教材实验四第3节“3.绘制特殊图形”中的相关内容,分别绘制条形图、实心图、阶梯图和火柴杆图,建议选取不同的函数来产生曲线;
M文件:
clf;
clc;
clear;
figure(2);
%条形图
subplot(2,2,1);
x1=3:6;
y1=[ 13
];
bar(x1,y1);
title('条形图');
xlabel('\it X','fontsize',10);
ylabel('Y');
text(5,7,'my picture');
%实心图
subplot(2,2,2);
x2=0::2*pi;
y2=cos(x2);
plot(x2,y2);
fill(x2,y2,'y');
title('实心图');
xlabel('\it X','fontsize',10);
ylabel('\it Y','fontsize',10);
text(3,0,'the second','FontName','commercialscript BT'); %阶梯图
subplot(2,2,3);
x3=0::2*pi;
y3=sin(x3);
stairs(x3,y3);
title('阶梯图');
xlabel('\it X','fontsize',10);
ylabel('\it Y','fontsize',10);
text(3,0,'the thrid','Fontsize',10);
%火柴杆图
subplot(2,2,4);
x4=0::2*pi;
y4=cos(x4)+;
stem(x4,y4);
title('火柴杆图');
xlabel('\it X','fontsize',10);
ylabel('\it Y','fontsize',10);
text(3,0,'the fourth','Fontsize',10);
6:产生10个服从正态分布的随机数,计算该数组的统计变量(包括均值、标准差、最大值、最小值等),并绘制二维和三维饼状图。注意程序运行时是否出现警告信息,思考为什么?M文件:
clc;
clf;
clear;
figure(3);
y=randn(10,1);
mean(y)
std(y)
max(y)
min(y)
subplot(2,1,1);
explode=[0 0 0 0 0 1 0 0 0 0];
pie(y,explode,{'1','2','3','4','5','6','7','8','9','10'});
subplot(2,1,2);
pie3(y,explode,{'1','2','3','4','5','6','7','8','9','10'});运行结果:
ans =
ans =
ans =
ans =
Warning: Ignoring non-positive data in pie chart.
> In pie at 41
In hc5 at 14
Warning: Ignoring non-positive data in pie chart.
> In pie3 at 43
In hc5 at 17
有警告出现,原因是:随机数产生有负数,无法在饼图上表示。