An Introductory on MATLAB and Simulink

Slides:



Advertisements
Similar presentations
Introduction to MATLAB
Advertisements

Introduction to Matlab
-seminar 1 for digital signal processing
1 1 Mechanical Design and Production Dept, Faculty of Engineering, Zagazig University, Egypt. Mechanical Design and Production Dept, Faculty of Engineering,
Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
Lecture 5.
Introduction to MATLAB The language of Technical Computing.
Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
An Introductory Course on MATLAB and Simulink Dr. Nik Rumzi Nik Idris Department of Energy Conversion FKE, UTM.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Math 15 Lecture 7 University of California, Merced Scilab A “Very” Short Introduction.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
INTRODUCTION TO MATLAB LAB# 01
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
A Brief introduction to MatLAB ($50 - $99 for students)
Intro to Matlab 1.Using scalar variables 2.Vectors, matrices, and arithmetic 3.Plotting 4.Solving Systems of Equations Can be found at:
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Scientific Computing Introduction to Matlab Programming.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
An Introductory Course on MATLAB and Simulink Dr. Nik Rumzi Nik Idris Department of Energy Conversion FKE, UTM.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
“Moh’d Sami” AshhabSummer 2008University of Jordan MATLAB By (Mohammed Sami) Ashhab University of Jordan Summer 2008.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
INTRODUCTION TO MatLab and Simulink. MATLAB - matrix laboratory - Developed by a lecturer in 1970’s to help students learn linear algebra. - marketed.
Introduction to MATLAB
Introduction to Matlab
Lecture 25.
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
(Mohammed Sami) Ashhab
MATH 493 Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
Communication and Coding Theory Lab(CS491)
Experiment No. (1) - an introduction to MATLAB
Introduction To MATLAB
Presentation transcript:

An Introductory on MATLAB and Simulink Muhamad Zahim Sujod zahim@kuktem.edu.my Ext : 2312

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 !

Introduction to MATLAB and Simulink Contents Introduction MATLAB Getting Started Vectors and Matrices Built in functions M–files : script and functions Simulink SIMULINK Modeling examples

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.

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 applications and rapid prototyping of control systems.

Getting Started Command window 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

Example of MATLAB Release 13 desktop

Variables – Vectors and Matrices – ALL variables are matrices Variables They are case–sensitive i.e x  X Their names can contain up to 31 characters Must start with a letter e.g. 1 x 1 4 x 1 1 x 4 2 x 4 Variables are stored in workspace

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

How do we assign values to vectors? Vectors and Matrices How do we assign values to vectors? >>> A = [1 2 3 4 5] A = 1 2 3 4 5 >>> A row vector – values are separated by spaces A column vector – values are separated by semi–colon (;) >>> B = [10;12;14;16;18] B = 10 12 14 16 18 >>>

How do we assign values to vectors? 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 Bytes Class c1 1x100 800 double array Grand total is 100 elements using 800 bytes >>>

How do we assign values to vectors? 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 notation >>> c2 = (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 >>>

How do we assign values to matrices ? 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 Rows separated by semi-colon

How do we access elements in a matrix or a vector? Vectors and Matrices How do we access elements in a matrix or a vector? Try the followings: >>> A(2,3) ans = 6 >>> A(:,3) ans = 3 6 9 >>> A(2,:) ans = 4 5 6 >>> A(1,:) ans = 1 2 3

Some special variables Vectors and Matrices Some special variables beep pi () inf (e.g. 1/0) i, j ( ) >>> 1/0 Warning: Divide by zero. ans = Inf >>> pi 3.1416 >>> i 0+ 1.0000i

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

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

Arithmetic operations – Matrices 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] A = 1 2 3 4 5 6 7 8 9 >>> To square every element in A, use the element–wise operator .^ >>> A.^2 ans = 1 4 9 16 25 36 49 64 81 >>> A^2 ans = 30 36 42 66 81 96 102 126 150 A^2 = A * A

Arithmetic operations – Matrices Vectors and Matrices Arithmetic operations – Matrices Performing operations between matrices >>> A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 >>> B=[1 1 1;2 2 2;3 3 3] B = 1 1 1 2 2 2 3 3 3 = A*B = A.*B

? (matrices singular) Arithmetic operations – Matrices Vectors and Matrices Arithmetic operations – Matrices Performing operations between matrices ? (matrices singular) A/B = A./B

Arithmetic operations – Matrices Vectors and Matrices Arithmetic operations – Matrices Performing operations between matrices ??? Error using ==> ^ At least one operand must be scalar A^B = A.^B

Arithmetic operations – Matrices Vectors and Matrices Arithmetic operations – Matrices Example: -j5 j10 10 1.50o 2-90o Solve for V1 and V2

Arithmetic operations – Matrices Vectors and Matrices Arithmetic operations – Matrices Example (cont) (0.1 + j0.2)V1 – j0.2V2 = -j2 - j0.2V1 + j0.1V2 = 1.5 = A x y =

Arithmetic operations – Matrices 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 x = 14.0000+ 8.0000i 28.0000+ 1.0000i >>> * A\B is the matrix division of A into B, which is roughly the same as INV(A)*B *

Arithmetic operations – Matrices 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

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

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

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

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

Built in functions (commands) Matrix functions – perform operations on matrices >>> x*xinv ans = 1.0000 0.0000 0.0000 0.0000 0 1.0000 0 0.0000 0.0000 0 1.0000 0.0000 0 0 0.0000 1.0000 >>> >>> x=rand(4,4) x = 0.9501 0.8913 0.8214 0.9218 0.2311 0.7621 0.4447 0.7382 0.6068 0.4565 0.6154 0.1763 0.4860 0.0185 0.7919 0.4057 >>> xinv=inv(x) xinv = 2.2631 -2.3495 -0.4696 -0.6631 -0.7620 1.2122 1.7041 -1.2146 -2.0408 1.4228 1.5538 1.3730 1.3075 -0.0183 -2.5483 0.6344

Built in functions (commands) From our previous example, = y x A = >>> x=inv(A)*y x = 14.0000+ 8.0000i 28.0000+ 1.0000i

Built in functions (commands) Data visualisation – plotting graphs >>> help graph2d >>> help graph3d e.g. plot polar loglog mesh semilog plotyy surf

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

Built in functions (commands) eg1_plt.m Data visualisation – plotting graphs Example on plot – 2 dimensional plot

Built in functions (commands) eg2_srf.m Data visualisation – plotting graphs Example on mesh and surf – 3 dimensional plot Supposed we want to visualize a function 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);

Built in functions (commands) eg2_srf.m Data visualisation – plotting graphs Example on mesh and surf – 3 dimensional plot

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);

Built in functions (commands) eg2_srf.m Data visualisation – plotting graphs Example on mesh and surf – 3 dimensional plot

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 Executed in sequence when called Saved with extension “.m” User defined commands Normally has input & output Saved with extension “.m”

At Matlab prompt type in edit to invoke M-file editor M-files : script and function files (script) eg1_plt.m At Matlab prompt type in edit to invoke M-file editor Save this file as test1.m

It will be executed provided that the saved file is in the known path 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 …

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

M-files : script and function files (script) Example – RLC circuit Total impedance is given by: When

M-files : script and function files (script) eg4.m eg5_exercise1.m Example – RLC circuit

M-files : script and function files (script) eg6.m 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 , Xc and XL phase angle of the total impedance for 100 <  < 2000

M-files : script and function files (script) eg6.m Example – RLC circuit

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

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

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 will display comments after the header

M-files : script and function files (function) impedance.m Function – 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);

We can now add our function to a script M-file 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));

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 !

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

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 rad/s

How to model our resonant circuit ? Simulink How to model our resonant circuit ? i 10  100 uF + v(t) = 5 sin t – 0.01 H Writing KVL around the loop,

Differentiate wrt time and re-arrange: Simulink Differentiate wrt time and re-arrange: Taking Laplace transform:

Simulink Thus the current can be obtained from the voltage: V I

Start Simulink by typing simulink at Matlab prompt It is where we obtain the blocks to construct our model It is here where we construct our model. Simulink library and untitled windows appear

Constructing the model using Simulink: ‘Drag and drop’ block from the Simulink library window to the untitled window

Constructing the model using Simulink:

We need to vary the frequency and observe the current Simulink eg8_sim.mdl We need to vary the frequency and observe the current …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.

Simulink

Simulink eg9_sim.mdl The waveform can be displayed using scope – similar to the scope in the lab

Reference Internet – search engine Mastering MATLAB 6 (Prentice Hall) Duane Hanselman Bruce Littlefield