不知道有没有用
s = tf('s');
gs = input('请输入原开环传递函数\n 例如: 1/(s*(0.1*s+1)*(0.05*s+1)) \ngs =');
% 稳态误差
N = input('请输入给定信号表达式\n 例如: 1/s^2 \n N = ');
E =s*N/(1+gs);
G1 = E;
clear s
syms s
[num,den] = tfdata(G1);%开始转换类型 tf->sym
Na = size(den);
for i=1:Na(1)
for k=1:Na(2)
Num=poly2sym(num{i,k},s);
Den=poly2sym(den{i,k},s);
sym_G1(i,k)=Num/Den;
end
end
assignin('base','sym_G1',sym_G1);
E = sym_G1;
ess2 = limit(E,s,0,'right');
ess = input('请输入期望的稳态误差 ess = ');
K = double(ess2)/ess ;%求得满足稳态误差的最小开环增益
clear s
s = tf('s');
disp(['最小开环增益K = ',num2str(K)])
K = input('请输入开环增益K = ');
gs=K*gs%更新系统传递函数
figure(1)
margin(gs);
[gm0 pm0 wg0 wp0] = margin(gs);
gm0=20*log10(gm0);
disp(['原系统幅值裕度Lg = ',num2str(gm0),'dB ','相角裕度γ = ',num2str(pm0)])
lg = input('请输入目标幅值裕度Lg = ');
gama = input('请输入目标相角裕度γ = ');
disp(['选用滞后校正 '])
% 截止频率
gama1=gama+5;
[mu pu w]=bode(gs);
wc=spline(pu,w,(gama1-180));%相对于pu的w的值的规律插值得到相对于(gama1-180)的值
disp(['截止频率wc = ',num2str(wc)]);
% 计算β
na=polyval(gs.num{1},j*wc);
da=polyval(gs.den{1},j*wc);
g=na/da;
g1=abs(g);
h=20*log10(g1);
beta=10^(h/20);
disp(['β = ',num2str(beta)]);
% 计算校正环节参数
T=10/wc
disp('校正环节:')
gc=(T*s+1)/(beta*T*s+1)
disp('校正后系统传递函数:')
g=gs*gc
disp('校正后系统传伯德图:')
figure(2)
bode(g);
[gm pm wg wp]=margin(g);
disp(['校正后系统幅值裕度Lg = ',num2str(gm),'dB ','相角裕度γ = ',num2str(pm)])
%阶跃响应
sys1=feedback(gs,1);
sys2=feedback(g,1);
figure(3)
hold on
step(sys1)
step(sys2)
hold off
figure(4)
hold on
bode(gs)
bode(g)
hold off
程序写的比较烂
[修改于 7年5个月前 - 2017/08/12 19:44:39]