MATLABSimulink Basic MATLAB matrices operators script and function
MATLAB/Simulink Basic MATLAB matrices operators script and function files flow control plotting Basics of Simulink state-space models (1 st order ordinary diff eqns) setting integration properties setting initial conditions input types getting data to the workspace
Basic MATLAB optional windows workspace current directory type commands here command window screen shot of the Matlab window
Matlab’s help features type “help” at the command prompt and Matlab returns a list of help topics
Matlab’s help features >> help lang Matlab’s language constructs
Matlab’s help features >> help for how to use Matlab’s “for” statement
Matlab’s help features you can also access “on-line” help by clicking the question mark in the toolbar separate window
MATLAB Variables all variables are stored in 32 bit floating point format no distinction between real and integer >>a = 3; same assignment for “a” >>a = 3. 0; Matlab is case sensitive >>A=3; >>a=2; A a
MATLAB Variables can use numbers and underscore in variable names >>case 34=6. 45; OK >>case_34=6. 45; names must start with a letter >>34 case=23. 45; results in a syntax error string (text) variables enclosed in single quotes. The variable is stored as array of characters >>title=‘This is the title’;
MATLAB Variables if a variable is defined, typing the variable name returns its value >>a=45. 57; >>a a= 45. 57 Matlab returns the value to clear a variable from memory >>a=4 >>clear a
MATLAB Variables Matlab will “echo” commands unless a semi-colon is used >>a=23. 2; >> >>a=23. 2 a= 23. 2 >> Matlab echoes the command
MATLAB Variables Vectors column vectors >>a=[1; 2; 3]; >>a a= 1 2 3 use semi-colon to separate rows row vectors >>a=[1, 2, 3]; >>a a= 1 2 3 use comma to separate columns
MATLAB Variables Matrices 2 -dimensional matrices >>a=[1, 2, 3; 4, 5, 6]; >>a a= 1 2 3 4 5 6 again, separate columns with commas and rows with semi-colons
MATLAB Variables Indexing Matrix elements A vector is a special type of matrix row vector is a 1 x n matrix, 1 row n columns column vector is a n x 1 matrix, n rows 1 column >>a=[1, 2, 3]; >>a(2) ans = 2 could also reference by a(1, 2) note, a(2, 1) would produce an error because “a” only has one row
MATLAB Variables Indexing Matrix elements more examples >>a=[1, 2, 3; 4, 5, 6]; addressing >>a(2, 3) ans = 6 assigning >>a(2, 2)=9; >>a a= 1 2 3 4 9 6
MATLAB Variables complex-valued numbers Typically, the variable “i” or “j” is used to represent the complex variable; e. g. Then, a complex number is represented as z = a + ib Re(z) = a Im(z) = b
MATLAB Variables complex-valued numbers Unless i or j has been previously defined, Matlab assigns i and j the complex variable value In Matlab, a complex variable is represented in the following format (assuming all variables are cleared) >>z=23+i*56; >>z z= 23. 00 + 56. 00 i >>z=23+j*56; >>z z= 23. 00 + 56. 00 i Matlab always uses the symbol “i” to represent a complex number
MATLAB Variables complex-valued numbers What happens in this case? >>i=3; >> z=23+i*56; >>z z= What happens in this case? >>a=sqrt(-1); >>z=23+a*56; >>z z=
MATLAB Variables complex-valued numbers Note, a real-valued number is a special case of a complex-valued number assigning any element of a matrix as complex-valued makes the entire matrix complex-valued >>a=[1, 2]; >>a a= 1 2 >>a(1)=1+i*5; >>a a= 1. 00+5. 00 i 2. 00+0. 00 i
MATLAB Variables Advanced data types n-dimensional arrays structures cell arrays
MATLAB Operations Basic operations addition subtraction multiplication division right division left division >>a=3; b=4; >>c 1=a/b; >>c 2=ab; + * / c 1=0. 75 c 2=1. 3333…. ? so, be careful!
MATLAB Operations Mixed Real and Complex valued Variables if both variables are real-valued, a real-valued result is obtained if one variable is complex-valued, Matlab recasts the real variable as complex and then performs the operation. The result is complex-valued however, the type casting is done internally, the real-valued variable remains real after the operation
MATLAB Operations Other (Scalar) Operations Math representation Matlab interpretation >>z=y^x; >>y=exp(x); >>y=log 10(x) >>y=sin(x); >>y=asin(x); >>y=cos(x); >>y=acos(x); >>y=tan(x); >>y=atan(x);
MATLAB Operations Examples >>y=x^0. 5; >>y=x^(1/2); >>y=sqrt(x); All variables in the preceding operations can be real or complex, negative or positive for x < 0, y is complex. Matlab assumes you allow complex valued numbers. If y is not to be complex, you must provide error checking.
MATLAB Operations Matrices Only matrices of the same dimension can be added and subtracted For multiplication, the inner dimensions must be the same No error >>D=A+B; >>D=A-B; >>D=A*C; >>D=C*A; Matrix multiplication not commutative Error >>D=A+C; >>D=A*B; >>D=B*A;
MATLAB Operations Left() and Right(/) Matrix “division” Math representation Matlab interpretation >>C=AB; >>C=B/A; Remember, A must be square and full rank (linearly independent rows/columns)
MATLAB Operations Matrix Transpose Math representation Matlab interpretation >>C=A’; For complex-valued matrices, complex conjugate transpose >>B=A’; >>b=a’;
MATLAB m-files Two types of m-files script files collection of commands that Matlab executes when the script is “run” function files collection of commands which together represent a function, a procedure or a method Both types are separate files with a “. m” extension
MATLAB m-files To create an m-file, open the Matlab text editor Click on the “page” icon The Matlab text editor window will open
MATLAB m-files Script Files On the command line In the script file named test. m >>x=3. 0; >>y=x^2; >>y y = 9. 0 >> On the command line >>test y = 9. 0 >>
MATLAB m-files Script Files script files share the workspace memory >>x=5. 0; >>test >>y y = 25. 0 >> test. m script
MATLAB m-files Script Files script files can call other script files inner. m script >>outter y = 36. 0 >> outter. m script
MATLAB m-files Function Files Matlab identifies function files from script files by using the “function” and “return” keywords the name of the function file must be the same name as the function
MATLAB m-files Function Files The function file x 2. m >>r=3; >>d=x 2(r); >>d d = 9. 0 >> >>h=x 2(4. 2); >>h h = 17. 64 >>
MATLAB m-files Function Files Multiple Inputs and Outputs outputs in square brackets, [ ] inputs in parentheses ( )
MATLAB m-files Function Files variables created in the function are not retained in the workspace, except for the output variables the function does not have access to workspace variables, except for the inputs variables passed to the function are “copies” of the workspace variables. Changing their value inside the function has no effect on their value in the workspace.
MATLAB Flow Control The “while” and “if” statements while expression statements end if expression statements 1 else statements 2 end Matlab evaluates expression as logical “true” or “false” equivalent to zero “true” equivalent to any non-zero number statements, any valid Matlab command
MATLAB Flow Control evaluating expression any valid equation a=4; b=5; c=5; if a+b “True” if b-c “False” watch out for round-off and word length error if sin(0) “False” if sin(pi) “True” sin(pi) = 1. 22 e-16 conditional operators == equal to < less than > greater than <= less than or equal to >= greater than or equal to ~= not equal to logical operators & and | or while(3<=a)&(a<=5)
MATLAB Flow Control The “for” statement for index = start : [increment : ] end statements end index, start, increment, and end do not need to be integer valued increment is optional, if increment is not specified increment defaults to 1 index can be incremented positive (increment > 0) or negative (increment < 0) loop stops when index > end (or index < end)
MATLAB Flow Control example script file to cycle through x values function file to generate the y values
MATLAB Plotting Basic 2 D plotting functions plot(x 1, y 1[, x 2, y 2, x 3, y 3. . . ]) xlabel(‘x axis name’) ylabel(‘y axis name’) title(‘graph name’) Additional functions grid on grid off axis([xmin, xmax, ymin, ymax])
MATLAB Plotting example y = sin(t) the “plot” function alone
MATLAB Plotting example y = sin(t) script file to generate a graph of y = sin(t)
MATLAB Plotting example y = sin(t) function file to generate a graph of y = sin(t) >>graphsin >>
MATLAB Plotting Adding a Legend for multiple graphs “legend” remembers the order the graphs were plotted
Simulink Basics click the Simulink button the Simulink window
Simulink Basics click the “new” button create a new model or open an existing one the simulink model window
Simulink Example Best thing to do is to go through an example 2 nd order, constant coefficient, linear differential equation Response to a “step” command
Simulink Example Get an equivalent block diagram for the system use mouse to drag blocks into the model window and to connect blocks with arrows use integrators to get dy/dt and y
Simulink Example add gain and summer blocks
Simulink Example add the step input block
Simulink Example add the output block
Simulink Example Now, double click the blocks to open and set the block’s parameters set gain value set initial condition set variable name set output format to “array”
Simulink Example To set the simulation parameters…. select Simulation -> Simulation Parameters set Start and Stop time (in seconds) set numerical integration type
Simulink Example Time to run the simulation click the “run” button to begin the simulation when the simulation is complete, “Ready” appears at the bottom
Simulink Example Simulink will automatically save a variable named “tout” to the workspace. This variable contains the time values used in the simulation, important for variable time integration types Simulink also will create the output variable(s) you specified
Simulink Example >>plot(tout, yoft) graph of the step response
Simulink Example Another approach to solving the 2 nd order single DOF problem, is to cast it as a 1 st order 2 DOF problem In Matrix (or State Space) form….
Simulink Example 1 st Order State-Space Models
Simulink Example Multi Input Multi Output Systems use Mux and Demux blocks to combine and extract vector signals specify number of signals
- Slides: 59