Note 10 Introduction to MATLAB & SIMULINK Department of Mechanical Engineering, University Of Saskatchewan, 57 Campus Drive, Saskatoon, SK S7N 5A9, Canada 1
1 Introduction to MATLAB MATLAB stands for the matrix laboratory It is an easy-to-use programming environment for performing numerical computation and graphically displaying the results It was first designed to perform matrix computation in 1960 Since then MATLAB has been developed being very powerful and has been widely used in many different engineering fields as well as scientific and non-scientific areas When used with routines from its companion software, the Control System Toolbox, MATLAB can be used to analyze and design control system problems such as those covered in our class 11 MATLAB Variables In MATLAB, variables are used to store various types of data such as scalars, vectors, matrices, etc MATLAB variables are created when they are assigned values For example, enter the following commands in the Command Window, >> x=150 % Assign 150 to x and display >> P1=[1 3 17] % Assign the vector of [1 3 17] to P and display (using a space between any % two elements) >> P=[1 0 3]; % The command ending with ; causes MATLAB not to display the result In MATLAB, % is used to indicate comments followed, which will not be executed by MATLAB Once the above commends are executed in MATLAB, the names of three variables, ie, x, P1, and P and their values are saved in a part of computer memory, called the Workspace in MATLAB You may use >> who % List all variables in the workspace >> x % Enter the name of a variable to display its value >> clear % Remove all variables and their values from the workspace In addition, you may use >> clc % Clear command window 1 Common Commands or Functions used in Control Systems A command (also referred to a function) is an instruction for a computer to perform a specific operation Create Polynomials >> P=[1 7-3 3] % Create a polynomial: P = s 3 + 7s 3s + 3 % The vector contains the coefficients of the polynomial Department of Mechanical Engineering, University Of Saskatchewan, 57 Campus Drive, Saskatoon, SK S7N 5A9, Canada
>> P=poly([- -5-6]) % Create a polynomial: P = ( s + )( s + 5)( s + 6) % The vector in poly () contains the roots of the polynomial >> P=conv([1 7], [1-3 ]) % Create a polynomial: P = ( s + 7)( s 3s + ) % The two vectors in conv() contains the coefficients of the two polynomials % respectively Create Transfer Function - using the function tf (, ) Using the following three commands to generate a transfer function s + G ( s) s + 3s + 16 >> numf=[1 ] % Create a polynomial: numf = s + >> denf=[1 3 16] % Create a polynomial: denf = s + 3s + 16 >> G1=tf(numf, denf) % Create a transfer function G1 % The first argument in tf() defines the numerator of the transfer function and % the second one defines its denominator Using the following three commands to generate a transfer function >> numf=[1 ]] % Create a polynomial: numf = s + >> denf=poly([-1-5]) % Create a polynomial: denf = ( s + 1)( s + 5) >> G=tf(numf, denf) % Create a transfer function G s + G ( s) ( s + 1)( s + 5) Find the closed-loop transfer function - by using the function feedback (, ) >> numf=[5]] % Create a polynomial: >> denf=poly([0-5]) % Create a polynomial: >> G=tf(numf, denf) % Create a transfer function G(s) >> T=feedback(G, 1) % Find the closed-loop transfer function T(s) Time Response using the function step( ) 16 Using the following commands to plot the step response of the system G ( s) s + 3s + 16 >> numf=[16] % Create a polynomial: numf = 16 >> denf=[1 3 16] % Create a polynomial: denf = s + 3s + 16 >> G=tf(numf, denf) % Create a transfer function G >> step (G) % Plot the step response, which is displayed in the widow of Figure >> grid on % Adds major grid lines to the axes Department of Mechanical Engineering, University Of Saskatchewan, 57 Campus Drive, Saskatoon, SK S7N 5A9, Canada 3
Find the poles of a system (then determine its stability) - by using the function pole() >> pole(g); % Compute the poles of the system defined by G Draw the Bode plots - by using the function bode ( ) >> bode(g) % Draw the Bode plot of the system defined by G >> grid on % Turn on grid for Bode plot, ie, adds major grid lines to the axes Draw Root locus - by using the function rlocus( ) Using the following commands to draw the root locus for K( s 3)( s 5) KGH = ( s + 1)( s + ) >> numf=poly([3 5]) % Create a polynomial: numf = ( s 3)( s 5) >> denf=poly([-1 -]) % Create a polynomial: denf = ( s + 1)( s + ) >> GH=tf(numf, denf) % Create the open-loop transfer function GH >> rlocus (GH) % Draw the root locus, which is displayed in the widow of Figure >> grid on % Adds major grid lines to the axes 13 M-files Using the Command Window to solve problems by entering and executing individual MATLAB command can be a tedious process, especially if the same or a similar set of commands have to be entered a number of times An alternative approach for executing multiple commands is to use a MATLAB M-file A MATLAB M-file is a file that contains a series of Matlab commands The file is generated using the MATLAB Editor(enter the command edit in the Command Window to get such a Editor); and the commands in the file are executed by selecting Run under the Debug menu in the MATLAB Editor, or entering the name of the file in the Command Window For example, the following is an example of MATLAB M-file % This program is used to plot the step response of a system numf=[16]; % Create the numerator polynomial denf=[1 3 16]; % Create the denominator polynomial G=tf(numf, denf); % Create a transfer function G step (G); % Plot the step response grid on; % Adds major grid lines to the axes % The End Department of Mechanical Engineering, University Of Saskatchewan, 57 Campus Drive, Saskatoon, SK S7N 5A9, Canada 4
SIMULINK SIMULINK, a companion program to MATLAB, is a software package for modeling, simulating, and analyzing dynamic systems In SIMULINK, you can use the graphical user interface (GUI) to generate your dynamic or control system, and then run a simulation for the system The procedure of using SIMULINK is described as follows (1) Starting SIMULINK Entering the command simulink in the Command Window, then you will see the SIMULINK Library Brower () Create a New Model In the SIMULINK Library Brower, click on the button of Create a new model, you will have a window to create a model Then, Drag the required blocks to your mode window from the SIMULINK Library Brower A block is used to perform a specific function; and the SIMULINK Library Brower can provide all kinds of blocks, which are organized in different categories such as Continuous (ie, subsystems), Sources (ie, function generators), and Sinks (ie, scopes), etc Quite often, you may need to choose the parameters for a block in your model To do that, Double-click the block and type in the desired parameters Interconnect the blocks by drawing lines Position the pointer over the first block s output port, press the mouse button, and drag the pointer to the second s input pot Please note: to draw a branch line from an existing line, you need to hold down the key of Ctrl (3) Run a Simulation for the Model In your model window, select Start under the Simulation menu Sometimes, you may need to choose the parameter for a simulation, such as simulation time To do that, select Configuration Parameters under the Simulation menu in your model window and set the desired parameters (4) Viewing the Simulation Results Double-click the Scope block in you model to view the simulation results For this purpose, when you create your model you need to drag a Scope block from the SIMULINK Library Brower and to connect it the output of the system, which you want to view the simulation results Examples of using SIMULINK by following the above procedure will be illustrated in class Department of Mechanical Engineering, University Of Saskatchewan, 57 Campus Drive, Saskatoon, SK S7N 5A9, Canada 5