MATLAB(定稿)
发布时间:2017-01-17 09:38:36
发布时间:2017-01-17 09:38:36
实验一
练习
1 使用who whos查看变量内容
>> a=2.5
a =
2.5000
>> b=[1 2;3 4]
b =
1 2
3 4
>> c='a'
c =
a
>> d=sin(a*b*pi/180)
d =
0.0436 0.0872
0.1305 0.1736
>> e=a+c
e =
99.5000
>> who
Your variables are:
a b c d e
>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
b 2x2 32 double
c 1x1 2 char
d 2x2 32 double
e 1x1 8 double
2 绘制柱状图
>> x=[1 2 3 4 5];
>> y=sin(x)
y =
0.8415 0.9093 0.1411 -0.7568 -0.9589
>> plot(y,'DisplayName','y','YDataSource','y');figure(gcf)
>> hist(y);figure(gcf);
>> x=[1 2 3 4 5];
>> y=sin(x)
y =
0.8415 0.9093 0.1411 -0.7568 -0.9589
>> hist(y);figure(gcf);
自我练习
x=[1 3 5 7 9]
y=2*x
plot(y)
实验二
练习
1使用全下标方式获取a矩阵中第二列子矩阵块
a=[1 2 3;4 5 6;7 8 9]
b=a(:,2)
a = b =
1 2 3 2
4 5 6 5
7 8 9 8
2使用logspace函数创建0-4*pi行向量 有二十个元素,查看元素分布情况。
>> c=logspace(0,4*pi,20)
c =
1.0e+12 *
Columns 1 through 7
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Columns 8 through 14
0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0004
Columns 15 through 20
0.0018 0.0083 0.0382 0.1752 0.8035 3.6844
3 验证关系式
A=[1/3 0 0;0 1/4 0;0 0 1/7]
B=inv(A)*inv(inv(A)-eye(3))*6*A
ZUO=A\(B*A)
YOU=6*A+B*A
A =
0.3333 0 0
0 0.2500 0
0 0 0.1429
B =
3.0000 0 0
0 2.0000 0
0 0 1.0000
ZUO =
3.0000 0 0
0 2.0000 0
0 0 1.0000
YOU =
3.0000 0 0
0 2.0000 0
0 0 1.0000
4 將矩正的乘除运算改为数组的点除运算
A=[1 2 3;4 5 6;7 8 9]
B=[1 1 1;2 2 2;3 3 3]
A*B
c1=A.*B
d1=A/B
d2=A./B
A = B =
1 2 3 1 1 1
4 5 6 2 2 2
7 8 9 3 3 3
ans = c1 =
14 14 14 1 2 3
32 32 32 8 10 12
50 50 50 21 24 27
Warning: Matrix is singular to working precision.
> In matlabP322 at 5
d1 =
NaN NaN NaN
NaN NaN NaN
NaN NaN Na
d2 =
1.0000 2.0000 3.0000
2.0000 2.5000 3.0000
2.3333 2.6667 3.0000
5 使用数组编辑窗口查看变量a,b,c
6 使用setfield命令进行上述修改
student(1)=struct('name','Rose','Id','20030102','score',[95,93,84,72,88])
student =
name: 'Rose'
Id: '20030102'
score: [95 93 84 72 88]
>> student=setfield(student,{1},'score','[95,73,84,72,88]')
student =
name: 'Rose'
Id: '20030102'
score: '[95,73,84,72,88]'
7 使用图形和文字显示average的各元胞内容
>> student(1)=struct('name','John','Td','20030115','scores',[85,96,74,82,68])
student =
name: 'John'
Td: '20030115'
scores: [85 96 74 82 68]
>> student(2)=struct('name','Rose','Td','20030102','scores',[95,93,84,72,88])
student =
1x2 struct array with fields:
name
Td
scores
>> student(3)=struct('name','Billy','Td','20030117','scores',[72,83,78,80,83])
student =
1x3 struct array with fields:
name
Td
scores
>> all_scores=cat(1,student.scores)
all_scores =
85 96 74 82 68
95 93 84 72 88
72 83 78 80 83
>> average_scores=mean(all_scores)
average_scores =
84.0000 90.6667 78.6667 78.0000 79.6667
方法一
>> average={'平均成绩',average_scores}
average =
'平均成绩' [1x5 double]
方法二
>> average(1)={'平均成绩'}
average =
'平均成绩'
>> average(2)={average_scores}
average =
'平均成绩' [1x5 double]
方法三
>> average{1}='平均成绩';
>> average{2}=average_scores
average =
'平均成绩' [1x5 double]
自我练习
1 使用LU和QR分解线性方程
A=[2 -3 0 2;1 5 2 1;3 -1 1 -1;4 1 2 2]
B=[8;2;7;12]
X=A\B
[L,U]=lu(A)
X=U\(L\B)
[Q,R]=qr(A)
X=R\(Q\B)
A = B =
2 -3 0 2 8
1 5 2 1 2
3 -1 1 -1 7
4 1 2 2 12
X = L =
3.0000 0.5000 -0.7368 1.0000 0
0.0000 0.2500 1.0000 0 0
-1.0000 0.7500 -0.3684 0.5000 1.0000
1.0000 1.0000 0 0 0
U = X =
4.0000 1.0000 2.0000 2.0000 3.0000
0 4.7500 1.5000 0.5000 0.0000
0 0 0.1053 1.3684 -1.0000
0 0 0 -3.0000 1.0000
Q =
-0.3651 0.5000 0.6708 0.4082
-0.1826 -0.8333 0.5217 -0.0000
-0.5477 0.1667 0.0745 -0.8165
-0.7303 -0.1667 -0.5217 0.4082
R = X =
-5.4772 0 -2.3735 -1.8257 3.0000
0 -6.0000 -1.8333 -0.3333 -0.0000
0 0 0.0745 0.7454 -1.0000
0 0 0 2.4495 1.0000
>> Untitled3
A = B =
2 -3 0 2 8
1 5 2 1 2
3 -1 1 -1 7
4 1 2 2 12
X = L =
3.0000 0.5000 -0.7368 1.0000 0
0.0000 0.2500 1.0000 0 0
-1.0000 0.7500 -0.3684 0.5000 1.0000
1.0000 1.0000 0 0 0
U = X =
4.0000 1.0000 2.0000 2.0000 3.0000
0 4.7500 1.5000 0.5000 0.0000
0 0 0.1053 1.3684 -1.0000
0 0 0 -3.0000 1.0000
Q = R =
-0.3651 0.5000 0.6708 0.4082 -5.4772 0 -2.3735 -1.8257
-0.1826 -0.8333 0.5217 -0.0000 0 -6.0000 -1.8333 -0.3333
-0.5477 0.1667 0.0745 -0.8165 00 0 0.0745 0.7454
-0.7303 -0.1667 -0.5217 0.4082 0 0 0 2.4495
X =
3.0000
-0.0000
-1.0000
1.0000
2 按表达式得出三阶,五阶拟合表达式和曲线
x=0:100;
y=[6 4 2 -7 10]
y0=polyval(y,x);
p3=polyfit(x,y0,3);
p5=polyfit(x,y0,5);
y3=polyval(p3,x);
y5=polyval(p5,x);
plot(x,y0,'o')
hold on
plot(x,y3,'r')
plot(x,y5,'g')
实验三
练习
1 用y替换x,查看结果及其数据类型
>> f=sym('sin(x)')
f =
sin(x)
>> x=0:10;
>> y=subs(f,x)
y =
Columns 1 through 7
0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794
Columns 8 through 11
0.6570 0.9894 0.4121 -0.5440
>> z=subs(f,y)
z =
Columns 1 through 7
0 0.7456 0.7891 0.1407 -0.6866 -0.8186 -0.2758
Columns 8 through 11
0.6107 0.8357 0.4006 -0.5176
2 将y1用sym函数转换为符号对象,并用d f e r四种格式表示
f=sym('sin(x)')
x=0:10;
y=subs(f,x)
whos
z=subs(f,y)
whos
y1=sym('sin(5)')
y=double(y1)
d=sym(y,'d')
f=sym(y,'f')
e=sym(y,'e')
r=sym(y,'r')
f =
sin(x)
y =
Columns 1 through 7
0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794
Columns 8 through 11
0.6570 0.9894 0.4121 -0.5440
Name Size Bytes Class Attributes
A 4x4 128 double
B 4x1 32 double
L 4x4 128 double
Q 4x4 128 double
R 4x4 128 double
U 4x4 128 double
X 4x1 32 double
f 1x1 112 sym
f1 1x1 112 sym
f2 1x1 112 sym
f3 1x1 112 sym
x 1x11 88 double
y 1x11 88 double
z =
Columns 1 through 7
0 0.7456 0.7891 0.1407 -0.6866 -0.8186 -0.2758
Columns 8 through 11
0.6107 0.8357 0.4006 -0.5176
Name Size Bytes Class Attributes
A 4x4 128 double
B 4x1 32 double
L 4x4 128 double
Q 4x4 128 double
R 4x4 128 double
U 4x4 128 double
X 4x1 32 double
f 1x1 112 sym
f1 1x1 112 sym
f2 1x1 112 sym
f3 1x1 112 sym
x 1x11 88 double
y 1x11 88 double
z 1x11 88 double
y1 =
sin(5)
y =
-0.9589
d =
-0.95892427466313845396683746002964
f =
-8637222012098867/9007199254740992
e =
-8637222012098867/9007199254740992
r =
-8637222012098867/9007199254740992
3 使用expand collect simplify 函数进行,,,,
f=sym('x^2+3*x+2')
f1=simplify(f)
f2=expand(f1)
f3=collect(f2)
f =
x^2 + 3*x + 2
f1 =
(x + 1)*(x + 2)
f2 =
x^2 + 3*x + 2
f3 =
x^2 + 3*x + 2
4 将f转换为以t为符号变量的符号表达式,
f=poly2sym([1 3 2])
f =
x^2 + 3*x + 2
h=poly2sym([1 3 2],sym('t'))
h =
t^2 + 3*t + 2
5 对符号举证A进行求特征值,对角阵等运算
A=sym('[x^2;2*x cos(2*t)]')
A =
[ x, x^2]
[ 2*x, cos(2*t)]
>> diag(A)
ans =
x
cos(2*t)
6 对符号举证A求极限和积分
>> int(A)
ans =
[ x^2/2, x^3/3]
[ x^2, x*cos(2*t)]
>> limit(A)
ans =
[ 0, 0]
[ 0, cos(2*t)]
7 当y(0)=1,z(0)=5等,求微分方程组的解
>> [y,z]=dsolve('Dy-z=cos(x),Dz+y=1','y(0)=1,z(0)=5','x')
y =
(11*sin(x))/2 + (x*cos(x))/2 + 1
z =
5*cos(x) - (x*sin(x))/2
自我练习
1 已知开环传递函数F(S)=;;;;
syms R F C s
R=1/s;
F=(2*s^2+3*s+3)/((s+1)*(s+3)^3);
C=R*F;
sym f;
f=ilaplace(C)
f =
(5*exp(-3*t))/36 - exp(-t)/4 + (t*exp(-3*t))/6 + t^2*exp(-3*t) + 1/9
2 用notebook计算,,,
实验四
练习
1 修改横坐标的刻度为“0 pi/2 2”:
t1=0:0.01:2;
y1=sin(2*pi*t1);
plot(t1,y1)
axis([0,2,-2,2])
set(gca,'XTick',0:pi/2:2)
set(gca,'XTicklabel',{'0','pi/2','2*pi'})
2 将三条曲线用不同的线性,为图形加坐标框
t1=0:0.01:2;
y1=exp(-t1);
plot(t1,y1,'r-')
hold on
y2=exp(-2*t1);
plot(t1,y2,'o');
hold on
y3=exp(-3*t1);
plot(t1,y3,'*');
axis on
3 添加图形的网格并添加文字指数曲线在第一条旁边
4 将坐标轴字体设置为12号粗体,蓝色
5 使用area和scatter命令,绘制面积图和点图。
> x=0:0.3:2*pi;
>> y=sin(x);
>> subplot(1,2,1)
>> area(x,y)
>> subplot(1,2,2)
>> scatter(x,y)
6 使用plottools窗口查看图形和变量。
GUI设计:
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
grid on
msgbox('Grid on','message')
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton1.
function pushbutton1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x=[0 1 1 2 2 3];
y=[1 1 0 0 1 1];
plot(x,y)
axis([0 4 0 2])
msgbox('Painting square wave','message')
自我练习
1 在图中画出一排两个子图 分别用条形图和饼状图画成3*3魔方阵:
magic(3)
subplot(1,2,1)
bar(ans)
subplot(1,2,2)
pie(ans)
2 绘制双纵坐标曲 纵坐标分别为正弦和余弦曲线
x1=0:0.1:2*pi;
x2=-pi:0.1:pi;
plotyy(x1,sin(x1),x2,cos(x2))
实验五
练习
1将该M函数文件改为M脚本文件,将数列元素个数通过键盘输入,程序该如何修改。
f(1)=1;f(2)=1;
%i=2;
%while i<=n;
%f(i+1)=f(i-1)+f(i);
%i=i+1;
n=input('input n number:');
for i=2:n;
if f(i)>50 break;
else
f(i+1)=f(i-1)+f(i);
end
end
2 修改程序,使用while循环代替for循环实现本例功能
function f=shiyan0501(n)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
%SHIYAN0501 Fibonacci¹¹³É
%FibonacciÊýÁÐ
%n ÔªËظöÊý
%f ¹¹³ÉFibonacciÊýÁÐÏòÁ¿
%copyright 2003-08-01
f(1)=1;f(2)=1;
i=2;
while i<=n;
f(i+1)=f(i-1)+f(i);
i=i+1;
end
3 如果不使用子函数factorial,而直接在cal函数中计算阶乘,应如何修改程序。
function k = cal(n1 )
%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here
%¼ÆËãϵÊýk
%global n;
for m=1:n1
k=factorial(2*n1)/(2^(2*n1)*(factorial(n1))^2*(2*n1+1));
m=1;
while m<=n1;
k=factorial(2*n1)/(2^(2*n1)*(factorial(n1))^2*(2*n1+1));
m=m+1;
End
4 利用函数句柄求[-1,1]的面积
y=quad(@shiyan0503,-1,1)
5 利用内联函数求8附近的极小值
>> a=0.1;
>> b=0.5;
>> f=inline('(sin(t)).^2.*exp(.1*t)-0.5*abs(t)','t')
f =
Inline function:
f(t) = (sin(t)).^2.*exp(.1*t)-0.5*abs(t)
>> x1=fminbnd(f,6,10)
x1 =
9.5214
自我练习
1 编写函数计算苏如参数r为圆半径的圆面积和周长:
function [S,C]=exercise(r)
%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here
S=pi*r^2;
C=2*pi*r;
%[S,C]=exercises(z);
%function [x,y]=exercises(r)
end
2 创建内联函数,并绘制其曲线:
function exercise2( )
%UNTITLED7 Summary of this function goes here
% Detailed explanation goes here
t=-10:0.01:10;
h_plotxy=str2func('plotxy')
y=feval(h_plotxy,t);
function y1=plotxy(r)
y1=inline('sin(r)./r','r');
plot(r,y1(r))
实验六
练习
1 将传递函数转换为状态空间法和零极点增益描述
a =
-3.3333 -5.3333 -4.5000 -2.5000 -1.5000
1.0000 0 0 0 0
0 1.0000 0 0 0
0 0 1.0000 0 0
0 0 0 1.0000 0
b =
1
0
0
0
0
c =
0 0 0 0 0.8333
d =
0
z =
Empty matrix: 0-by-1
p =
-1.0388 + 1.0728i
-1.0388 - 1.0728i
-1.2799
0.0122 + 0.7248i
0.0122 - 0.7248i
k =
0.8333
2 查看所有共轭极点的阻尼系数和固有频率
[wn,zeta]=damp(GT2)
wn =
0
0.7249
0.7249
1.2799
1.3333
1.4934
1.4934
zeta =
-1.0000
-0.0168
-0.0168
1.0000
1.0000
0.6956
0.6956
3 GT1转换为状态空间法模型和零极点增益模型并查看属性
[a,b,c,d]=tf2ss(num{1},den{1})
get(GT1)
[z,p,k]=tf2zp(num{1},den{1})
get(GT1)
a =
-3.3333 -5.3333 -4.5000 -2.5000 -1.5000
1.0000 0 0 0 0
0 1.0000 0 0 0
0 0 1.0000 0 0
0 0 0 1.0000 0
b =
1
0
0
0
0
c =
0 0 0 0 0.8333
d =
0
num: {[0 0 0 0 0 0.8333]}
den: {[1 3.3333 5.3333 4.5000 2.5000 1.5000]}
Variable: 'z'
ioDelay: 0
InputDelay: 0
OutputDelay: 0
Ts: 0.1000
TimeUnit: 'seconds'
InputName: {''}
InputUnit: {''}
InputGroup: [1x1 struct]
OutputName: {''}
OutputUnit: {''}
OutputGroup: [1x1 struct]
Name: ''
Notes: {}
UserData: []
z =
Empty matrix: 0-by-1
p =
-1.0388 + 1.0728i
-1.0388 - 1.0728i
-1.2799
0.0122 + 0.7248i
0.0122 - 0.7248i
k =
0.8333
num: {[0 0 0 0 0 0.8333]}
den: {[1 3.3333 5.3333 4.5000 2.5000 1.5000]}
Variable: 'z'
ioDelay: 0
InputDelay: 0
OutputDelay: 0
Ts: 0.1000
TimeUnit: 'seconds'
InputName: {''}
InputUnit: {''}
InputGroup: [1x1 struct]
OutputName: {''}
OutputUnit: {''}
OutputGroup: [1x1 struct]
Name: ''
Notes: {}
UserData: []
4 在Nicholas曲线添加M线和线,用鼠标单击曲线可以看到所单击点的坐标和频率。
>> ngrid()
5使用rltool命令打开根轨迹分析的图形界面,并修改系统参数查看根轨迹的变化
>> rltool(GG2)
6 使用双线性变换的方式将连续系统转换为离散系统
Ga=c2d(G,0.5,’tustin’)
Ga =
0.0495 z^2 + 0.09901 z + 0.0495
-------------------------------
z^2 - 1.485 z + 0.6832
Sample time: 0.5 seconds
Discrete-time transfer function.
7 在窗口单击右键选择菜单“PLOT TYPE”--“BODE”显示伯德图 并显示其频域指示
显示伯德图
实验7
练习
1 将传递函数1/(0.5s+1)修改为前向通道为1/0.5*s的单位反馈闭环系统
2修改仿真参数“Max step size”为2,“Min step size”为1,在示波器上查看波形。
3 修改示波器Y坐标范围为0—2,横坐标范围0--15
4 在Icon选卡中为封装子系统的封面添加传递函数和曲线
4 将仿真时间设为0—20 将SCOPE时间设为0—15 将SCOPE1时间范围设置为0--25
5 使用sinks模块库的out1模块作为工作空间的变量输出,用plot绘制波形
5调整“Pluse Generate”查看不同方波延时和脉宽变化
自我练习
1 使用阶跃信号作为输入
2 在命令窗口输入T值
综合实验
1 编程实现两序列卷积,序列初始值自定,给出各序列图形,并将输入输出数据文件;
x=[ones(1,15)];
x1=[ones(1,15),zeros(1,45)];
N1=length(x);
n1=0:N1-1;
N2=60;n2=0:N2-1;
h=0.8.^n2;
y=conv(x,h);
N=N1+N2-1;n=0:N-1;
subplot(3,1,1);stem(n2,x1);title('x(n)=u(n)-u(n-15)');
subplot(3,1,2);stem(n2,h);title('h(n)=0.8^n*u(n)');
subplot(313);stem(n,y);title('¾í»ý');
以第一步程序为基础,实现两序列卷积的GUI设计,两输入序列值由控件输入,输出值由控件输出并绘图
2 mt = cos(2*pi*fm*t); %信源(调制信号)
s_am = (A+mt).*cos(2*pi*fc*t); %已调信号
编程完善程序,频率fm、fc值自定,并绘图显示;
以第一步数据建立.MDL模拟调制系统模型,设置好参数,观察已调信号。