Linear System Theory - MATLAB Exercise Prof. Robert X. Gao Electromechanical Systems Laboratory Department of Mechanical Engineering
Outline Review System Modeling Procedure Example Simple Problem Example Translational Problem Example 3 Rotational Problem Summary / 37
Review MATLAB Operations User Interface Define a Variable Matrix Operations Graphics Help System Modeling Function Command Define a Matrix A A=[ 3; 4 5 6; 7 8 9] Find Dimension of A size(a) Find Transpose of A A' Matrix Addition A+B (where B is a same dimension matrix) Matrix Multiplication A*B (where B is a compatible dimension matrix) Element Multiplication A.*B(where B is the same dimension matrix) Find Inverse of A inv(a) D Line Plot plot(x,y) 0.5 0-0.5-0 3 4 5 6 3 / 37
Use Help Whenever encounters a problem use the built in Help Menu \ Help \ Product Help (F) Type in keyword and search for answer Demos: an easy way to learn 4 / 37
System Modeling Procedure Analyze the problem Free-body Diagrams Equilibrium Equations State Variables Rewrite State Variables in the form of Matrices Realization in MATLAB 5 / 37
Example. Given: M = 0 kg, friction can be ignored. When t = 0, x = 0, v = 0. Input: F a (t) = 0 N Output: Displacement of M when t = 0 seconds. 6 / 37
Analysis Free body Diagram Equilibrium Equation : F a (t) = Mx State Variables: x,v x v v F / M Displacement x is a state variable, so there is no need to write a separate output equation. Classic Method: Since acceleration is a constant, x = ½ * at where a = F/M = m/s x = ½ * *0 = 50 meters 7 / 37
Modeling in State Space Rewrite state-variable equations to include all variables x (0) x() v (0) F v (0) x(0) v(/ M) F Rewrite state-variable in the form of matrices x 0 x 0 F v 0 0 v / M Making the following substitutions x x y, and y v v State variable equation becomes 0 0 y y F 0 0 / M 8 / 37
Define the System Equation % System Equation Definition function dy = sys_0_a(t,y) %-----------Constants-------------------- m = 0; %---- Initialization and State Space Equation ----- dy =zeros(,); f = 0; System Input dy = [0,;0,0]*y + [0; /m]*f; State Space Equations 9 / 37
Launch the ODE Solver % ODE Solver T=0; %----Initial Conditions---------- IC0=[0 0]; %-----------Solver Input-------- timeperiod=[0:t/00:t]; options = odeset('reltol',e-,'abstol',e-); %-----------ODE Solver----------- Set Initial Conditions and Solving Range Set Solver [T,Y] = ode45(@sys_0_a,timeperiod,ic0,options); %----------Plot Output---------- figure; plot(t,y(:,),'linewidth',3); grid on; xlabel('time (seconds)','fontsize',34); ylabel('displacement (meters)','fontsize',34); set(gca,'fontsize',4); axis tight; fprintf('\nfinal Displacement:%f\n',Y(length(Y),)); Invoke ODE Solver Present Result 0 / 37
Result Displacement (meters) 50 40 30 0 0 When t = 0 seconds, Final Displacement:50.000000 0 0 4 6 8 0 Time (seconds) / 37
Example. Given: M = 0 kg, friction can be ignored. When t = 0, x = 0, v = 0. Input: F a (t) = 30t a = 3t. same as Problem in the Preliminary Quiz Output: Displacement of M when t = 5 seconds. / 37
Solution Free body Diagram Equilibrium Equation : F a (t) = Mx State Variables: x,v x v v F / M Rewrite State Variable Equations to Include All Variables x (0) x() v (0) F x x y, and y v (0) x(0) v(/ M) F v v Re formed State Variable Equation 0 0 y y F 0 0 / M Everything here remains the same as the Ex.. 3 / 37
Modeling in State Space Rewrite state-variable equations to include all variables x (0) x() v (0) F v (0) x(0) v(/ M) F Rewrite state-variable in the form of matrices x 0 x 0 F v 0 0 v / M Making the following substitutions x x y, and y v v State variable equation becomes 0 0 y y F 0 0 / M 4 / 37
Define the System Equation % System Equation Definition function dy = sys a(t,y) %-----------Constants-------------------- m = 0; %---- Initialization and State Space Equation ----- dy =zeros(,); f = 30*t; System Input dy = [0,;0,0]*y + [0; /m]*f; State Space Equations 5 / 37
Launch the ODE Solver % ODE Solver T=5; %----Initial Conditions---------- IC0=[0 0]; %-----------Solver Input-------- timeperiod=[0:t/00:t]; options = odeset('reltol',e-,'abstol',e-); %-----------ODE Solver----------- Set Initial Conditions and Solving Range Set Solver [T,Y] = ode45(@sys a,timeperiod,ic0,options); %----------Plot Output---------- figure; plot(t,y(:,),'linewidth',3); grid on; xlabel('time (seconds)','fontsize',34); ylabel('displacement (meters)','fontsize',34); set(gca,'fontsize',4); axis tight; fprintf('\nfinal Displacement:%f\n',Y(length(Y),)); Invoke ODE Solver Present Result 6 / 37
Result Displacement (meters) 60 50 40 30 0 0 When t = 5, Final Displacement:6.500000 0 0 0.5.5.5 3 3.5 4 4.5 5 Time (seconds) 7 / 37
Example Problem 3.0 PART I Find the state variable model Output: Energy stored in each spring PART II Given: M = 0 kg, M =0 kg K = 5 N/m, K = 00 N/m, K 3 = 50 N/m B= 45 N.s/m, g= 9.8 m/s Suppose x = 0, x = m, v = 0, v = 0, Find the energy stored in spring K when t = seconds 8 / 37
Free body Diagrams 9 / 37
Equilibrium Equations Equilibrium Equations Mx K( xx) KxMg0 Mx KxBx K( xx) Mg0 3 Re organize Mx ( K K) xkx Mg0 KxMx Bx ( K K) x Mg0 3 Identify State Variables x, v, x, v 0 / 37
Modeling in State Space - State Variable Equations x v v ( K K ) x K x M g x M v v K x ( K K ) x Bv M g 3 M Re write to Include All Variables Output Equations Ek K( xx) x [0] x [] v [0] x [0] v [0] g K K K v [ ] x [0] v [ ] x [0] v [] g M M x [0] x [0] v [0] x [] v [0] g K K K B v [ ] x [0] v [ ] x [ ] v [ ] g 3 M M M / 37
Modeling in State Space - Collect terms to express as product of matrices 0 0 0 x x K K K 0 0 0 v M v M g x 0 0 0 x 0 v K K K3 B 0 v M M M Output Equation x v Ek K( xx) KN, where N 0 0 x v / 37
Modeling in State Space 3 Make the Following Substitutions x x v v q thus q x x v v The State Variable equations become 0 0 0 K K K 0 0 0 M M q q g 0 0 0 0 K K K3 B 0 M M M Ek KN wheren q, 0 0 3 / 37
Define the System Equation % System Equation Definition function [dy,e] = sys_(t,y) %------Constants-------------------- m = 0; m = 0; k = 5; k = 00; k3 = 50; b = 45; g = 9.8; % kg % N/m % N/(m/s) % m/s^ % - Initialization - dy =zeros(4,); % - State Space Equation dy = [0,, 0, 0; -(k+k)/m, 0, k/m, 0; 0, 0, 0, ; k/m, 0, -(k+k3)/m, -b/m]*y + [0,,0,-]'*g; State Space Equations 4 / 37
Launch the ODE Solver % ODE Solver T=0; % ---Initial Conditions---------- IC0=[0 0 0]; %-----------Solver Input-------- timeperiod=[0:t/000:t]; options = odeset('reltol',e-,'abstol',e-); %-----------ODE Solver----------- Set Initial Conditions and Solving Range Set Solver [T,Y,E] = ode45(@sys_,timeperiod,ic0',options); %----------Plot Output---------- figure; k = 00; E = /*k*(y(:,)-y(:,3)).^; plot(t,e,'linewidth',3) grid on; xlabel('time (seconds)','fontsize',34); ylabel('energy (Joules)','FontSize',34); set(gca,'fontsize',4); axis tight; fprintf('\n Energy in Spring K when T = :%f\n',e(0)); Invoke ODE Solver Present Result 5 / 37
Plot the Results OUTPUT : Energy Stored in Spring K 500 450 400 When T =, Energy Stored in K = 43.3687 J. Energy (Joules) 350 300 50 00 50 00 50 0 4 6 8 0 Time (seconds) 6 / 37
Example 3 Problem 5.5 PART I Find the state variable model Output: Spring Force (Positive when spring is in tension ) PART II Given: M = 0 kg K = 5 N/m L = meter Suppose θ =, θ = 0, ω = 0, ω = 0, Find the spring force when t = 0 ~ 0 seconds 7 / 37
Free body Diagrams Assuming θ and θ to be sufficiently small so that the following approximation is allowed sinθ θ cosθ Therefore, K(sinθ L/-sinθ L/) = ½KL(θ -θ ) Centrifugal Force F = MωR 8 / 37
Equilibrium Equations Moment Moment Equilibrium Equations KL KL ML MgLsin ( )cos 0 KL KL ML MgLsin ( )cos 0 Re organize and Simplify KL KL ML ( MgL ) 0 4 4 KL KL ML ( MgL ) 0 4 4 Identify State Variables,,, sinθ θ cosθ 9 / 37
Modeling in State Space - State Variable Equations Output Equations g K K L 4M 4M K g K 4M L 4M Re write to Include All Variables [0] [] [0] [0] g K K [ ( )] [0] [ ] [0] L 4M 4M [0] [0] [0] [] K g K [ ] [0] [ ( )] [0] 4M L 4M KL Fk ( ) 30 / 37
Modeling in State Space - Collect terms to express as product of matrices 0 0 0 g K K ( ) 0 0 L 4M 4M 0 0 0 K g K 0 ( ) 0 4M L 4M Output Equation KL KL KL Fk ( ) 0 0 3 / 37
Modeling in State Space 3 Make the Following Substitutions q thus q The State Variable equations become 0 0 0 g K K ( ) 0 0 q L 4M 4M q 0 0 0 K g K 0 ( ) 0 4M L 4M F k KL KL 0 0 q 3 / 37
Define the System Equation % System Equation Definition function dy = sys_3(t,y) %------Constants-------------------- m = 0; % Mass 0 kg k = 5; % Spring 5 N/m L = ; % Length meter g = 9.8; % g 9.8 m/s^ % - Initialization - dy =zeros(4,); % - State Space Equation - dy = [0,, 0, 0;... -(g/l+k/4/m), 0, k/4/m, 0;... 0, 0, 0, ;... k/4/m, 0, -(g/l+k/4/m), 0]*y; State Space Equations 33 / 37
Launch the ODE Solver % ODE Solver T=0; % ---Initial Conditions---------- IC0=[ 0 0 0]; %-----------Solver Input-------- timeperiod=[0:t/000:t]; options = odeset('reltol',e-,'abstol',e-); %-----------ODE Solver----------- [T,Y,E] = ode45(@sys_3,timeperiod,ic0',options); %----------Plot Output---------- figure; F = /*5*(Y(:,3)-Y(:,)); plot(t,f,'linewidth',3) grid on; xlabel('time (seconds)','fontsize',34); ylabel('spring Tension Force (Newton)','FontSize',34); set(gca,'fontsize',4); axis tight; Set Initial Conditions and Solving Range Set Solver Invoke ODE Solver Present Result 34 / 37
Plot the Result OUTPUT : Tension Force in Spring K Spring Tension Force (Newton) 0 5 0 5 0-5 -0-5 -0-5 0 4 6 8 0 Time (seconds) 35 / 37
Discussion Manipulation of the output vectors Compare the output of Problem and Problem 3, discuss the difference Importance of finding the right the state variable equations 36 / 37
Summary Reviewed basic MATLAB operations Demonstrated the sequence of modeling a dynamic system Demonstrated finding the numerical solution with MATLAB 37 / 37