SIMULATION OF DYNAMICAL SYSTEMS In practice we wish to study the behavior of complex systems subject to general input disturbances. Then it is not feasible to solve the (differential) equations which describe the system analytically. Instead, it is necessary to use numerical simulations. In numerical differential equation solvers the systems are usually represented in terms of coupled first-order differential equations. Then the system is characterized using a number of state variables, x1(t), x2(t), . . . , xn(t) and the time derivative of each variable is expressed as a function of the other variable and the input(s): dxi(t) = fi (x1(t), x2(t), . . . , xn(t), u(t)) , i = 1, 2, . . . , n dt 2 Example: Second-order system d2y(t) dy(t) + a + a2y(t) = bu(t) 1 dt2 dt Define the state variables x1(t) = y(t) dy(t) x2(t) = dt Then dx1(t) = x2(t) dt dx2(t) = −a1x2(t) − a2x1(t) + bu(t) dt which has the desired form. There are many good ordinary differential equation (ode) solver available, for example in Matlab. The example below shows how a second-order system with a step change in the input can be simulated. 3 Example: Simulation of second-order system The first program calls Matlab’s ode solver ode23. The command [T,X]=ode23(@sys2,[t_ini t_final],x_ini) returns a vector T of time instants at which the values of the state variables are given. The matrix X has the same number of rows as T, and its columns contain the values of the state variables, so that the kth column of X contains the state variable xk (t) at time instants T(1), T(2), . . .. The function subroutine sys2 returns the time derivatives of the state variables in the vector dxdt. 4 % Simulation of 2nd order system % global a1 a2 b % a1=1; a2=1; b=1; % system parameters t_ini=0; t_final=25; % initial and final times x_ini=[0 0]; % initial values of state variables % [T,X]=ode23(@sys2,[t_ini t_final],x_ini) ; function dxdt=sys2(t,x) % Derivative of second-order systems global a1 a2 b % % Introduce step input at t=10 if t<10 u=0; else u=1; end % y=x(1); dydt=x(2); % dxdt(1)=dydt; dxdt(2)=-a1*dydt-a2*y+b*u ; % dxdt=dxdt’; % end 5 Example: Simulation of second-order system controlled by a PID controller The same approach can be applied to simulate more complex system connections. For illustration, consider the second-order system d2 y(t) dy(t) + a2 y(t) = bu(t) + a 1 dt2 dt where u(t) is determined by the PID control law Z t dy(t) u(t) = Kp (r(t) − y(t))+Ki (r(τ ) − y(τ )) dτ +Kd − τ =0 dt (Observe that the derivative action is applied only to y). The we can define the state variables: x1 (t) = y(t) dy(t) x2 (t) = dt Z t x3 (t) = (r(τ ) − y(τ )) dτ τ =0 Then: dx1 (t) = x2 (t) dt dx2 (t) = −a1 x2 (t) − a2 x1 (t) + bu(t) dt dx3 (t) = r(t) − y(t) dt where u(t) = Kp (r(t) − x1 (t)) + Ki x3 (t) + Kd (−x2 (t)) 6 The closed-loop system can be simulated with the following Matlab programs: % Simulation of 2nd order system with PID control % global a1 a2 b Kp Ki Kd % a1=0.25; a2=1; b=1; % system parameters % Kp=1; Ki=1; Kd=1; % controller parameters % t_ini=0; t_final=25; % initial and final times % x_ini=[0 0 0]; % initial values of state variables % [T,X]=ode23(@sys2PID,[t_ini t_final],x_ini) ; % % % end 7 function dxdt=sys2PID(t,x) % ---------------------------------% Derivative of second-order system % under PID control % ---------------------------------% global a1 a2 b Kp Ki Kd % % Introduce step change in setpoint at t=10 % if t<10 r=0; else r=1; end % y=x(1); dydt=x(2); yi=x(3); u=Kp*(r-y)+Ki*yi-Kd*dydt; % dxdt(1)=dydt; dxdt(2)=-a1*dydt-a2*y+b*u ; dxdt(3)=r-y; % dxdt=dxdt’; % end 8 Simulink As seen from the above simple examples, when the number of state variables required to describe a system increases, it may become rather tedious to construct programs which compute the time derivatives of all state variables. Therefore, various tools which facilitate the analysis and simulation of dynamical systems have been developed. One widely used tool of this kind is Simulink for use with Matlab, in which the block diagram and the individual blocks can be constructed using a graphical user interface. Below is shown an example of a Simulink program consisting of a first-order system G(s) = K Ts + 1 (s corresponds to p) and a time delay controlled by a PID controller, where there is a step change in setpoint. The block Scope plots the associated variable. 9 The parameters of the various blocks can be defined by opening the individual blocks. The block may have internal structure, for example the structure of the PID block is shown below. 10