 ## Slide #2.

Introduction to MATLAB and Simulink What can you gain from the course ? Know what MATLAB/Simulink is Know how to get started with MATLAB/Simulink Know basics of MATLAB/Simulink – know how to solve simple problems Be able to explore MATLAB/Simulink on your own !
More slides like this ## Slide #3.

Introduction to MATLAB and Simulink Contents Introduction Getting Started Vectors and Matrices Built in functions M–files : script and functions Simulink Modeling examples MATLA B SIMULINK
More slides like this ## Slide #4.

Introduction MATLAB – MATrix LABoratory – Initially developed by a lecturer in 1970’s to help students learn linear – – – – – algebra. It was later marketed and further developed under MathWorks Inc. (founded in 1984) – www.mathworks.com Matlab is a software package which can be used to perform analysis and solve mathematical and engineering problems. It has excellent programming features and graphics capability – easy to learn and flexible. Available in many operating systems – Windows, Macintosh, Unix, DOS It has several tooboxes to solve specific problems.
More slides like this ## Slide #5.

Introduction Simulink – Used to model, analyze and simulate dynamic systems using block diagrams. – Fully integrated with MATLAB , easy and fast to learn and flexible. – It has comprehensive block library which can be used to simulate linear, non–linear or discrete systems – excellent research tools. – C codes can be generated from Simulink models for embedded
More slides like this ## Slide #6.

Getting Started Run MATLAB from Start  Programs  MATLAB Depending on version used, several windows appear • For example in Release 13 (Ver 6), there are several windows – command history, command, workspace, etc • For Matlab Student – only command window Command window • Main window – where commands are entered
More slides like this ## Slide #8.

Variables – Vectors and Matrices – ALL variables are matrices e.g. 1 xVariables 1 4x1 1x4 2x4 3 case–sensitive 2 x1  5X  3 2 1 7 i.e  4 •They are   6  2 •Theirnames can contain 9up 3to 2 4 31 characters  9  •Must start with a letter 3   Variables are stored in workspace
More slides like this ## Slide #9.

Vectors and Matrices  How do we assign a value to a variable? >>> v1=3 v1 = 3 >>> whos Name Size Bytes Class R 1x1 8 double array >>> i1=4 i1 1x1 8 double array i1 = v1 1x1 8 double array 4 Grand total is 3 elements using 24 bytes >>> R=v1/i1 >>> who R= Your variables are: 0.7500 >>> R >>> i1 v1
More slides like this ## Slide #10.

Vectors and Matrices  How do we assign values to vectors? >>> A = [1 2 3 4 5] A = 1 2 3 4 5 >>> >>> B = [10;12;14;16;18] B = 10 12 14 16 18 >>> A row vector – A  1are 2 3 values separated by spaces   10  12   A column B –14 vector values are  separated  16by  semi–colon  18 (;) 4 5
More slides like this ## Slide #11.

Vectors and Matrices  How do we assign values to vectors? If we want to construct a vector of, say, 100 elements between 0 and 2 – linspace >>> c1 = linspace(0,(2*pi),100); >>> whos Name Size c1 1x100 Bytes 800 Class double array Grand total is 100 elements using 800 bytes >>>
More slides like this ## Slide #12.

Vectors and Matrices  How do we assign values to vectors? If we want to construct an array of, say, 100 elements between 0 and 2 – colon >>> c2 = notation (0:0.0201:2)*pi; >>> whos Name Size Bytes Class c1 1x100 800 double array c2 1x100 800 double array Grand total is 200 elements using 1600 bytes >>>
More slides like this ## Slide #13.

Vectors and Matrices  How do we assign values to matrices ? >>> A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 >>> Columns separated by space or a comma  1 2 3  4 5 6    7 8 9 Rows separated by semi-colon
More slides like this ## Slide #14.

Vectors and Matrices  How do we access elements in a matrix or a vector? Try the followings: >>> A(2,3) ans = 6 >>> A(1,:) ans = 1 2 >>> A(:,3) ans = 3 6 9 3 >>> A(2,:) ans = 4 5 6
More slides like this ## Slide #15.

Vectors and Matrices  Some special variables >>> 1/0 Warning: Divide by zero. beep ans = pi () Inf inf (e.g. 1/0) >>> pi i, j ( ans = 1 ) 3.1416 >>> i ans = 0+ 1.0000i
More slides like this ## Slide #16.

Vectors and Matrices  Arithmetic operations – Matrices Performing operations to every entry in a matrix >>> A=[1 2 3;4 5 6;7 8 Add and 9] >>>subtract A+3 A = 1 4 7 >>> 2 5 8 3 6 9 ans = 4 7 10 >>> A-2 ans = -1 2 5 5 8 11 6 9 12 0 3 6 1 4 7
More slides like this ## Slide #17.

Vectors and Matrices  Arithmetic operations – Matrices Performing operations to every entry in a matrix >>> A=[1 2 3;4 5 6;7 8 9] Multiply and A = >>>divide A*2 1 2 3 4 7 >>> 5 8 6 9 ans = 2 8 14 >>> A/3 ans = 0.3333 1.3333 2.3333 4 10 16 6 12 18 0.6667 1.6667 2.6667 1.0000 2.0000 3.0000
More slides like this ## Slide #18.

Vectors and Matrices  Arithmetic operations – Matrices Performing operations to every entry in a matrix Power >>> A=[1 2 3;4 5 6;7 8 9] To square every element A= 1 2 3 in A, use the element– 4 5 6 wise .^ 7 8 9 >>> operator A.^2 >>> A^2 = A * A ans = 1 16 49 >>> A^2 ans = 30 66 102 4 25 64 9 36 81 36 81 126 42 96 150
More slides like this ## Slide #19.

Vectors and Matrices  Arithmetic operations – Matrices Performing operations between >>> B=[1 1 1;2 2 2;3 3 >>>matrices A=[1 2 3;4 5 6;7 8 9] 3] B = A = 1 4 7 2 5 8 A*B A.* B 3 6 9 1 2 3  1 2 3  1 1 1   4 5 6   2 2 2      7 8 9  3 3 3  1x1 2x1 3x1   4 x 2 5x 2 6 x 2     7 x 3 8x 3 9x 3 1 2 3 1 2 3 =  14 14 14   32 32 32    50 50 50 = 1 2 3  8 10 12     21 24 27 
More slides like this ## Slide #20.

Vectors and Matrices  Arithmetic operations – Matrices Performing operations between matrices A/B A./ B ? (matrices singular)  1/1 2/1 3/1  4/ 2 5/ 2 6/ 2    7/ 3 8/ 3 9/ 3 =  1.0000 2.0000 3.0000   2.0000 2.5000 3.0000     2.3333 2.6667 3.0000 
More slides like this ## Slide #21.

Vectors and Matrices  Arithmetic operations – Matrices Performing operations between matrices A^ B A.^ B ??? Error using ==> ^ At least one operand must be scalar  11 21 31   2 2 2 4 5 6    73 83 93    = 2 3   1  16 25 36     343 512 729
More slides like this ## Slide #22.

Vectors and Matrices  Arithmetic operations – Matrices Example: -j5 2-90o 10 Solve for V1 and V2 j10 1.50o
More slides like this ## Slide #23.

Vectors and Matrices  Arithmetic operations – Matrices Example (cont) (0.1 + j0.2)V1 – j0.2V2 -j2 - j0.2V1 + j0.1V2 1.5  0.1 j0.2  j0.2  V1   j2 =     j0.2   j0.1   V2 1 . 5    A x = y = =
More slides like this ## Slide #24.

Vectors and Matrices  Arithmetic operations – Matrices Example (cont) >>> A=[(0.1+0.2j) -0.2j;-0.2j 0.1j] A = 0.1000+ 0.2000i 0- 0.2000i 0- 0.2000i 0+ 0.1000i >>> y=[-2j;1.5] y = 0- 2.0000i 1.5000 * >>> x=A\y A\B is the matrix division of A into B, w x = hich is roughly the same as INV(A)*B 14.0000+ 8.0000i * 28.0000+ 1.0000i >>>
More slides like this ## Slide #25.

Vectors and Matrices  Arithmetic operations – Matrices Example (cont) >>> V1= abs(x(1,:)) V1 = 16.1245 >>> V1ang= angle(x(1,:)) V1ang = 0.5191 V1 = 16.1229.7o V
More slides like this ## Slide #26.

Built in functions (commands) Scalar functions – used for scalars and operate element-wise when applied to a matrix or vector e.g. sin cos tan atan asin log abs angle sqrt round floor At any time you can use the command help to get help e.g. >>>help sin
More slides like this ## Slide #27.

Built in functions (commands) >>> a=linspace(0,(2*pi),10) a = Columns 1 through 7 0 4.1888 0.6981 1.3963 2.0944 2.7925 3.4907 0.8660 0.3420 -0.3420 Columns 8 through 10 4.8869 5.5851 6.2832 >>> b=sin(a) b = Columns 1 through 7 0 -0.8660 0.6428 0.9848 Columns 8 through 10 -0.9848 >>> -0.6428 0.0000
More slides like this ## Slide #28.

Built in functions (commands) Vector functions – operate on vectors returning scalar value e.g. max min mean prod sum >>> max(b) length >>> a=linspace(0,(2*pi),10); ans = >>> b=sin(a); 0.9848 >>> max(a) ans = 6.2832 >>> length(a) ans = 10 >>>
More slides like this ## Slide #29.

Built in functions (commands) Matrix functions – perform operations on matrices >>> help elmat >>> help matfun e.g. eye size inv det eig At any time you can use the command help to get help
More slides like this ## Slide #30.

Built in functions (commands) Matrix functions – perform operations on matrices >>> x*xinv >>> x=rand(4,4) x= 0.9501 0.9218 0.8913 0.8214 0.2311 0.7382 0.7621 0.4447 0.6068 0.1763 0.4565 0.6154 0.4860 0.4057 0.0185 0.7919 ans = 1.0000 0.0000 0.0000 0.0000 0 1.0000 0 0.0000 0.0000 0 1.0000 0.0000 0 0.0000 0 1.0000 >>> xinv=inv(x) >>> xinv = 2.2631 -2.3495 -0.4696 -0.6631 -0.7620 1.2122 1.7041
More slides like this ## Slide #31.

Built in functions (commands) From our previous example,  0.1 j0.2  j0.2  V1   j2 =     j0.2   V j 0 . 1 1 . 5    2   A >>> x=inv(A)*y x = 14.0000+ 8.0000i 28.0000+ 1.0000i x = y
More slides like this ## Slide #32.

Built in functions (commands) Data visualisation – plotting graphs >>> help graph2d >>> help graph3d e.g. plot polarloglog semilog plotyy mesh surf
More slides like this ## Slide #33.

Built in functions (commands) eg1_plt.m Data visualisation – plotting graphs on plot – 2 Example dimensional plot Example on plot – 2 dimensional plot >>> x=linspace(0,(2*pi),100); >>> y1=sin(x); >>> y2=cos(x); >>> plot(x,y1,'r-') >>> hold Current plot held >>> plot(x,y2,'g--') >>> Add title, labels and legend title xlabel ylabel legend Use ‘copy’ and ‘paste’ to add to your window–based document, e.g. MSword
More slides like this ## Slide #34.

Built in functions (commands) eg1_plt.m Data visualisation – plotting graphs on plot – 2 Example dimensional plot Example on plot 1 sin(x) cos(x) 0.8 0.6 0.4 y1 and y2 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 1 2 3 4 angular frequency (rad/s) 5 6 7
More slides like this ## Slide #35.

Built in functions (commands) eg2_srf.m Data visualisation – plotting graphs on mesh and surf – 3 Example Supposed we want plot to visualize a function dimensional Z = 10e(–0.4a) sin (2ft) for f = 2 when a and t are varied from 0.1 to 7 and 0.1 to 2, respectively >>> [t,a] = meshgrid(0.1:.01:2, 0.1:0.5:7); >>> f=2; >>> Z = 10.*exp(-a.*0.4).*sin(2*pi.*t.*f); >>> surf(Z); >>> figure(2); >>> mesh(Z);
More slides like this ## Slide #36.

Built in functions (commands) Data visualisation – plotting graphs Example on mesh and surf – 3 dimensional plot eg2_srf.m
More slides like this ## Slide #37.

Built in functions (commands) eg3_srf.m Data visualisation – plotting graphs Example on mesh and surf – 3 dimensional plot >>> [x,y] = meshgrid(-3:.1:3,-3:.1:3); >>> z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2); >>> surf(z);
More slides like this ## Slide #38.

Built in functions (commands) Data visualisation – plotting graphs Example on mesh and surf – 3 dimensional plot eg2_srf.m
More slides like this ## Slide #39.

M-files : Script and function files When problems become complicated and require re–evaluation, entering command at MATLAB prompt is not practical Solution : use M-files Script Function Collections of commands User defined commands Executed in sequence when called Normally has input & output Saved with extension “.m” Saved with extension “.m”
More slides like this ## Slide #40.

M-files : script and function files (script) eg1_plt.m At Matlab prompt type in edit to invoke Mfile editor Save this file as test1.m
More slides like this ## Slide #41.

M-files : script and function files (script) To run the M-file, type in the name of the file at the prompt e.g. >>> test1 It will be executed provided that the saved file is in the known path Type in matlabpath to check the list of directories listed in the path Use path editor to add the path: File  Set path …
More slides like this ## Slide #42.

M-files : script and function files (script) eg4.m eg5_exercise1.m Example – RLC circuit R = 10 C + V L – Exercise 1: Write an m–file to plot Z, Xc and XLversus frequency for R =10, C = 100 uF, L = 0.01 H.
More slides like this ## Slide #43.

M-files : script and function files (script) Example – RLC circuit Total impedance is given by: 1  Z R  j L   C   When XC  XL ZR 1 o  LC
More slides like this ## Slide #44.

M-files : script and function files (script) eg4.m eg5_exercise1.m Example – RLC circuit 120 Z Xc Xl 100 80 60 40 20 0 0 200 400 600 800 1000 1200 1400 1600 1800 2000
More slides like this ## Slide #45.

eg6.m M-files : script and function files (script) Example – RLC circuit R = 10 C + V L – For a given values of C and L, plot the following versus the frequency a) the total impedance , b) Xc and XL c) phase angle of the total impedance
More slides like this ## Slide #46.

M-files : script and function files (script) eg6.m Example – RLC circuit Magnitude 100 Mag imp Xc Xl 80 60 40 20 0 0 200 400 600 800 1000 1200 1400 1600 1800 2000 1200 1400 1600 1800 2000 Phase 100 50 0 -50 -100 0 200 400 600 800 1000
More slides like this ## Slide #47.

M-files : script and function files (function)  Function is a ‘black box’ that communicates with workspace through input and output variables. INPUT FUNCTION – Commands – Functions – Intermediate variables OUTPUT
More slides like this ## Slide #48.

M-files : script and function files (function) Every function must begin with a header: function output=function_name(inputs) Output variable Must match the file name input variable
More slides like this ## Slide #49.

M-files : script and function files (function)  Function – a simple example function y=react_C(c,f) %react_C calculates the reactance of a capacitor. %The inputs are: capacitor value and frequency in hz %The output is 1/(wC) and angular frequency in rad/s y(1)=2*pi*f; w=y(1); y(2)=1/(w*c); File must be saved to a known path with filename the same as the function name and with an extension ‘.m’ Call function by its name and arguments help react_C header will display comments after the
More slides like this ## Slide #50.

M-files : script and function files (function)  Function impedance.m – a more realistic example function x=impedance(r,c,l,w) %IMPEDANCE calculates Xc,Xl and Z(magnitude) and %Z(angle) of the RLC connected in series %IMPEDANCE(R,C,L,W) returns Xc, Xl and Z (mag) and %Z(angle) at W rad/s %Used as an example for IEEE student, UTM %introductory course on MATLAB if nargin <4 error('not enough input arguments') end; x(1) = 1/(w*c); x(2) = w*l; Zt = r + (x(2) - x(1))*i; x(3) = abs(Zt); x(4)= angle(Zt);
More slides like this ## Slide #51.

M-files : script and function files (function) eg7_fun.m We can now add our function to a script M-file R=input('Enter R: '); C=input('Enter C: '); L=input('Enter L: '); w=input('Enter w: '); y=impedance(R,C,L,w); fprintf('\n The magnitude of the impedance at %.1f rad/s is %.3f ohm\n', w,y(3)); fprintf('\n The angle of the impedance at %.1f rad/s is %.3f degrees\n\n', w,y(4));
More slides like this ## Slide #52.

Simulink Used to model, analyze and simulate dynamic systems using block diagrams. Provides a graphical user interface for constructing block diagram of a system – therefore is easy to use. However modeling a system is not necessarily easy !
More slides like this ## Slide #53.

Simulink Model – simplified representation of a system – e.g. using mathematical equation We simulate a model to study the behavior of a system – need to verify that our model is correct – expect results Knowing how to use Simulink or MATLAB does not mean that you know how to model a system
More slides like this ## Slide #54.

Simulink Problem: We need to simulate the resonant circuit and display the current waveform as we change the frequency dynamically. 10  100 uF i Varies  from 0 to 2000 rad/s + v(t) = 5 sin t 0.01 H – Observe the current. What do we expect ? The amplitude of the current waveform will become maximum at resonant frequency, i.e. at  = 1000
More slides like this ## Slide #55.

Simulink How to model our resonant circuit ? i 10  100 uF + v(t) = 5 sin t 0.01 H – Writing KVL around the loop, di 1 v iR L  idt dt C 
More slides like this ## Slide #56.

Simulink Differentiate wrt time and re-arrange: 2 1 dv diR d i i   2 L dt dtL dt LC Taking Laplace transform: sV R I 2  sI s I  L L LC sV  2 R 1 I  s  s   L L LC 
More slides like this ## Slide #57.

Simulink hus the current can be obtained from the voltage:    s(1/L)  I V  1 2 R  s  s  L LC  V s(1/L) 1 2 R s  s L LC I
More slides like this ## Slide #58.

Simulink Start Simulink by typing simulink at Matlab prompt Simulink library and untitled windows appear It is where we obtain the blocks to construct our model It is here where we construct our model.
More slides like this ## Slide #59.

Simulink nstructing the model using Simulink: ‘Drag and drop’ block from the Simulink library window to the untitled window 1 s+1 Sine Wave Transfer Fcn simout To Workspace
More slides like this ## Slide #60.

Simulink nstructing the model using Simulink: s(1/L) 1 2 R s  s L LC s(100 ) 2 6 s  1000 s  110 100s s2+1000s+1e6 Sine Wave Transfer Fcn v To Workspace1 i To Workspace
More slides like this ## Slide #61.

Simulink eg8_sim.mdl eed to vary the frequency and observe the cu 5 Amplitude Ramp v To Workspace3 w To Workspace2 1 1000 Constant s Integrator Dot Product3 100s s2+1000s+1e6 sin Elementary Math Dot Product2 Transfer Fcn1 i To Workspace …From initial problem definition, the input is 5sin(ωt). You should be able to decipher why the input works, but you do not need to create your own input subsystems of this form.
More slides like this ## Slide #62.

Simulink 1 0.5 0 -0.5 -1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 5 0 -5
More slides like this ## Slide #63.

Simulink eg9_sim.mdl The waveform can be displayed using scope – similar to the scope in the lab 5 Constant1 2000 Constant 0.802 Slider Gain 1 100s sin s Dot Product2 Integrator Elementary Math s2+1000s+1e6 Scope Transfer Fcn
More slides like this ## Slide #64.

Reference  Internet – search engine  Mastering MATLAB 6 (Prentice Hall) – Duane Hanselman – Bruce Littlefield
More slides like this