%矩阵乘法
A = [1 2;3 4];
B = [5 5;7 8];
A^2*B
%矩阵除法
A = [1 2 3;4 5 6;7 8 9];
B = [1 0 0;0 2 0;0 0 3];
A\B
A/B
%矩阵的转置及共轭转置
A = [5+i,2-i,1;6*i,4,9-i];
A.'
A'
%使用冒号选出指定元素
A = [1 2 3;4 5 6;7 8 9];
A(1:2,3)
A(2:3,:)
A = magic(4);
A(:,4) = [];
A
%3.多项式
px = [1 0 -2 -4];
roots(px)
A = [1.2 3 5 0.9;5 1.7 5 6;3 9 0 1;1 2 3 4];
f = poly(A)
polyval(f,20)%将未知数 20 代入
polyval(f,A)%将矩阵 A 作为未知数代入到多项式中
%4.基本绘图命令
t = linspace(0,2*pi);
y = cos(t);
plot(y)
y = cos(t-0.25);
plot(y)
hold on
y = sin(t-0.5);
plot(y)
% 5.基本绘图控制
t = linspace(0,4*pi);
x1 = 10*sin(t);
plot(t,x1,'-.r+')
xlabel('x') ; ylabel('y')
axis([0,15,-10,10]);
title('x1=10sint')
grid on
%6.基本程序设计
%计算 1+2+3+4+...+n<2000时最大 n 值
a = 0;
n = 1;
while a<2000
a = n + a;
n = n + 1;
end
n
%求 2 的 0 到 n 次幂的和
n = input('please input n: ')
sum = 0;
for i = 0:n
sum = sum + 2^i;
end
sum
%输入 y or Y 时 x=1 n or N 时 x=0,else 退出
a = 0;
x = 0;
while (a==0)
i = input('please input x: ','s');
if ((i == 'y')||(i =='Y'))
x = 1
a = 1;
elseif ((i == 'n')||(i == 'N'))
x = 0
a = 1;
else
break
end
end
**
**
%1.1建立系统传递函数模型
s = tf('s');
G = 4*(s+2)*(s^2+6*s+6)/s/(s+1)^3/(s^3+3*s^2+2*s+5)
%1.2建立系统零极点模型
s = zpk('s');
g11 = 3*(s+12);
g21 = 4*(s+5)*(s+3);
G = [g11;g21]/(s+3)/(s+4)/(s+5)
%1.3建立状态空间模型
A=[-2.8,-1.4,0,0;1.4,0,0,0;-1.8,-0.3,-1.4,-0.6;0,0,0.6,0];
B=[1;0;1;0];
C=[0,0,0,1];
D=zeros(1,1);
G = ss(A,B,C,D)
%2.1 转换为零极点模型
G1 = zpk(G)
%2.2 转换为状态空间模型
G2 = ss(G)
%2.3 转换为零极点模型
G3 = zpk(G)
%3.控制系统模型的连接
A1=[0 1;1 -2];B1=[0;1];C1=[1 3];D1=[1];
A2=[0 1;-1 -3];B2=[0;1];C2=[1 4];D2=[0];
G1=ss(A1,B1,C1,D1);
G2=ss(A2,B2,C2,D2);
G = G2*G1
G = G1+G2
G = feedback(G1,G2)
%4.典型系统的生成
s = tf('s');
w = 6; e = 0.1;
H = w^2/(s^2+2*e*w*s+w^2)
%5.连续系统的离散化
z = [-3]; p = [-1;-2;-5];
G = zpk(z,p,6);
G1 = c2d(G,0.1)
**
**
%1.1时域分析
num=[20];den=[1,8,36,40,0];
G = tf(num,den);
G1 = feedback(G,1);
step(G1,30)
%1.2典型二阶系统
s = tf('s');
wn = 6;zet = [0.2:0.2:1.0,1.5,2.0]
for z = zet
G = wn^2/(s^2+2*wn*z*s+wn^2);
step(G,5);
hold on
end
%1.3
wn = [2:2:12]; zet = 0.7;
s = tf('s')
for w = wn
G = w^2/(s^2+2*zet*w*s+w^2);
step(G,5);hold on
end
%2.根轨迹分析
s = tf('s');
num = [1];den = [1,4,3,0];
G = tf(num,den);
rlocus(G)
%3.1频域分析 伯德图
zet = 0.7;
wn = [2:2:12];
for w = wn
s = tf('s');
G = w^2/(s^2+2*zet*w*s+w^2);
bode(G);hold on;
end
%3.2
s = tf('s');
wn = 6;zet = [0.2:0.2:1.0,1.5,2.0];
for z = zet
G = wn^2/(s^2+2*z*wn*s+wn^2);
bode(G);hold on;
end