(1) margin函数:求幅值裕度和相角裕度及对应的转折频率
margin(mag,phase,w):由bode指令得到的幅值mag(不是以dB为单位)、相角phase及角频率w矢量绘制出带有裕量及相应频率显示的bode图。
margin(num,den) :可计算出连续系统传递函数表示的幅值裕度和相角裕度并绘制相应波特图。类似,margin(a,b,c,d)可以计算出连续状态空间系统表示的幅值裕度和相角裕度并绘制相应波特图。
[gm,pm,wcg,wcp]=margin(mag,phase,w):由幅值mag(不是以dB为单位)、相角phase及角频率w矢量计算出系统幅值裕度和相角裕度及相应的相角交界频率wcg、截止频率wcp,而不直接绘出Bode图曲线。
注意:vc(或者说MFC)调用matlab时,m文件中不能使用margin函数,应该是margin函数中使用了符号。
注意二:m文件中不能定义特殊符号,如“s=tf('s')”,因为mfc不认识这些符号。
注意三:使用margin函数时,下面的颜色、线型不可自己设置,而像step,bode这些函数都是可以使用这些设置的。
(2)多曲线标记
Matlab提供了一些绘图选项,用于确定所绘曲线的线型、颜色和数据点标记符号。这些选项如表所示:
线型 | 颜色 | 标记符号 | |
- 实线 | b蓝色 | . 点 | s 方块 |
: 虚线 | g绿色 | o 圆圈 | d 菱形 |
-. 点划线 | r红色 | × 叉号 | ∨朝下三角符号 |
-- 双划线 | c青色 | + 加号 | ∧朝上三角符号 |
| m品红 | * 星号 | <朝左三角符号 |
| y黄色 |
| >朝右三角符号 |
| k黑色 |
| p 五角星 |
| w白色 |
| h 六角星 |
在绘制图形时,可以对图形加上一些说明,如图形的名称、坐标轴说明以及图形某一部分的含义等,这些操作称为添加图形标注。有关图形标注函数的调用格式为:
title(’图形名称’) (都放在单引号内)
xlabel(’x轴说明’)
ylabel(’y轴说明’)
text(x,y,’图形说明’)
legend(’图例1’,’图例2’,…)
(4)grid on:绘图时显示网格(5)spline函数:绘点画图,画离散曲线
例:
x=[0 1 2 3 4 5 6 7 8 8.85];y=[13 12.1 11 10.5 10.1 9.9 9.6 9.3 9.0 8.9];
xx=0:0.01:10;
yy=spline(x,y,xx);
plot(x,y,'o',xx,yy)
(6)conv()函数
matlab中help的解释是:
CONV Convolution and polynomial multiplication.
C = CONV(A, B) convolves vectors A and B. The resulting vector is
length MAX([LENGTH(A)+LENGTH(B)-1,LENGTH(A),LENGTH(B)]). If A and B are
vectors of polynomial coefficients, convolving them is equivalent to
multiplying the two polynomials.
C = CONV(A, B, SHAPE) returns a subsection of the convolution with size
specified by SHAPE:
'full' - (default) returns the full convolution,
'same' - returns the central part of the convolution
that is the same size as A.
'valid' - returns only those parts of the convolution
that are computed without the zero-padded edges.
LENGTH(C)is MAX(LENGTH(A)-MAX(0,LENGTH(B)-1),0).
Class support for inputs A,B:
float: double, single
本项目的例子:以下是m文件,实现根据控制对象参数和控制器参数画出系统bode图的功能
function BodeOfChecked(Wn,Zeta,kp,ti,td)
%num0=Wn^2;
%den0=conv([1,0],[1 2*Wn*Zeta Wn^2]);
%G0=tf(num0,den0);
num=conv([0 Wn^2],conv([0 kp],[ti*td ti 1]));
den=conv([1 0],conv([1 2*Wn*Zeta Wn^2],conv([0 1],[ti 0])));
%Gc=tf(num1,den1);
bode(num,den);grid on;
end
(7)程序设计基础
①if
单分支:
if 条件表达式
语句组
end
双分支:
if 条件表达式
语句组1
else
语句组2
end
多分支:
if 条件表达式1
语句组1
elseif条件表达式2
语句组2
... ...
elseif条件表达式m
语句组m
else
语句组
end
②switch
switch 表达式
case 表达式1
语句组1
case 表达式2
语句组2
... ...
case 表达式m
语句组m
otherwise
语句组
end
③for循环
for 循环变量=取值列表
循环体
end
例: clear;y=0; n=100;
for k=1:n
y=y+1/(2*k-1);
end
④while
while 条件表达式
循环体
end
例:while 1
n=input('Please enter n (nonpositive quit): ');
if n<=0, break; end
nt = n; %将 n 的初始值记录下来
whilen>1
if mod(n,2)==0
n = n/2;
else
n = 3*n+1;
end
fprintf(' n=%d \n', n);
end
fprintf(' n=%d is not we need! \n', nt);
end