matlab 入门基本操作命令与函数

一  :tf([  ],[  ]) 函数

是传递函数的意思,一般学自动控制原理的时候经常用,在s域中,比如你要输入G(s)=1/(s^2+2s+1),就可以在matlab中输入G=tf([1],[1 2 1]);就OK了。不懂的话你可以在command窗口输入help tf 就行了

    matlab中  help tf  :

tf

Create or convert to transfer function model

 

Syntax

tf
sys = tf(num,den)
sys = tf(num,den,Ts)
sys = tf(M)
sys = tf(num,den,ltisys)
tfsys =tf(sys)
tfsys = tf(sys,'inv')

Description

tf   is used to create real-or complex-valued transfer function models (TF objects) or to convertstate-space or zero-pole-gain models to transfer function form.

 

 

sysd=c2d(sys,Ts,'z'); % c2d ---连续的时间系统转化为离散的时间系统,即离散化.'t'表示双线性变换法

                                   % Find critical valuecrs Kcr,Wcr % 'z'表示带零阶保持器的Z变换法,可缺省

        sys=tf(5.235e005,[1,87.35,1.047e004,0]);%获得传递函数。
        dsys=c2d(sys,ts,'z');%将连续的时间模型转换成离散的时间模型,采样时间是ts=0.001。
        [num,den]=tfdata(dsys,'v');%获得离散还建模型的分子分母矩阵

: PID MATLAB 仿真程序

  >> ts = 0.001;
sys  = tf (400,[1 ,50,0]);
dsys = c2d (sys,ts,'z');
[num,den] = tfdata(dsys,'v');
u_1  = 0.0 ;u_2 = 0.0; u_3 = 0.0 ;
y_1 = 0   ; y_2 = 0;   y_3 = 0;
x =  [0, 0 ,0];
error_1 = 0;
error_2 = 0;
for k=1:1:1000
  time(k) = k*ts;
  rin(k) = 1.0;
  kp = 8;
  ki = 0.10;
  kd = 10;
  du(k) = kp*x(1) + kd*x(2) + ki*x(3);
  u(k) = u_1 + du(k);
  if u(k) >= 10;
     uk = 10;
  end
  if u(k) <= -10 ;
  u(k) = -10;
  end
  yout(k) = -den(2)*y_1 - den(3)*y_2 + num(2)*u_1 +num(3)*u_2;
  error = rin(k) - yout(k);
  u_3 = u_2 ; u_2 = u_1; u_1 = u(k);
  y_3 = y_2 ; y_2 = y_1; y_1 = yout(k);
  x(1) = error - error_1;
  x(2) = error - 2*error_1 + error_2 ;
  x(3) = error;
  error_2 = error_1;
  error_1 = error;
end
plot(time,rin,'b',time,yout,'r');
xlabel ('time(s)');
ylabel ('rin,yout');

 

 

你可能感兴趣的:(matlab)