A General Introduction to Matlab

Similar documents
Introduction to Scientific and Engineering Computing, BIL108E. Karaman

Lecturer: Keyvan Dehmamy

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Chapter 1 MATLAB Preliminaries

Arithmetic and Logic Blocks

Lecture 1: What is MATLAB?

A very brief Matlab introduction

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

Chapter 2 (Part 2) MATLAB Basics. dr.dcd.h CS 101 /SJC 5th Edition 1

1 Introduction to Matlab

LAB 1 General MATLAB Information 1

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Computer Programming in MATLAB

Chapter 2. MATLAB Fundamentals

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

MATLAB QUICK START TUTORIAL

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Finding, Starting and Using Matlab

PART 1 PROGRAMMING WITH MATHLAB

Introduction to MatLab. Introduction to MatLab K. Craig 1

Matlab Workshop I. Niloufer Mackey and Lixin Shen

Some elements for Matlab programming

QUICK INTRODUCTION TO MATLAB PART I

Lab #1 Revision to MATLAB

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

Script started on Thu 25 Aug :00:40 PM CDT

Introduction to MATLAB Programming

Introduction to Engineering gii

General MATLAB Information 1

Introduction to GNU-Octave

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Dr Richard Greenaway

2.0 MATLAB Fundamentals

MATLAB Constants, Variables & Expression. 9/12/2015 By: Nafees Ahmed

Dr Richard Greenaway

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Introduction to MATLAB for CSE390

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Introduction to MATLAB

Introduction to MATLAB

PIV Programming. Today s Contents: 1. Matlab Programming 2. An example of PIV in Matlab code 3. EDPIV 4. PIV plugin for ImageJ 5.

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab

Introduction to Matlab

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

A QUICK INTRODUCTION TO MATLAB

MATLAB Workshop Dr. M. T. Mustafa Department of Mathematical Sciences. Introductory remarks

Fundamentals of MATLAB Usage

Math 2250 MATLAB TUTORIAL Fall 2005

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

A. Matrix-wise and element-wise operations

Lab 1 Intro to MATLAB and FreeMat

Chapter 1 Introduction to MATLAB

Introduction to Matlab

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

TUTORIAL 1 Introduction to Matrix Calculation using MATLAB TUTORIAL 1 INTRODUCTION TO MATRIX CALCULATION USING MATLAB

Introduction to MATLAB

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

1. Register an account on: using your Oxford address

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Getting Started with MATLAB

6.094 Introduction to MATLAB January (IAP) 2009

An Introduction to MATLAB II

A Guide to Using Some Basic MATLAB Functions

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

What is Matlab? A software environment for interactive numerical computations

Ebooks Chemical Engineering

Introduction to MATLAB

Scientific Computing Lecture 1: MATLAB fundamentals

Introduction to Matlab

Introduction to Matlab

Getting started with MATLAB

Matlab Tutorial, CDS

Introduction to MATLAB

Scientific Functions Complex Numbers

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

An Introduction to MATLAB and the Control Systems toolbox Aravind Parchuri, Darren Hon and Albert Honein

Laboratory 1 Octave Tutorial

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

HERIOT-WATT UNIVERSITY DEPARTMENT OF COMPUTING AND ELECTRICAL ENGINEERING. B35SD2 Matlab tutorial 1 MATLAB BASICS

To start using Matlab, you only need be concerned with the command window for now.

CHAPTER 3 PART 1 MATLAB FUNDAMENTALS

Introduction to MATLAB

AN INTRODUCTION TO MATLAB

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

MATLAB and Numerical Analysis

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Introduction to MATLAB

University of Alberta

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

Transcription:

Master Degree Course in ELECTRONICS ENGINEERING http://www.dii.unimore.it/~lbiagiotti/systemscontroltheory.html A General Introduction to Matlab e-mail: luigi.biagiotti@unimore.it http://www.dii.unimore.it/~lbiagiotti

Outline The objectives of this lecture are To become familiar with the MATLAB environment To enable you to use some simple MATLAB commands from the Command Window Bibliography: Brian Hahn and Daniel T. Valentine, Essential MATLAB for Engineers and Scientists, Academic Press. MATLAB introduction -- 2

Matlab MATLAB is a powerful computing system for handling scientific and engineering calculations. The name MATLAB stands for Matrix Laboratory, because the system was designed to make matrix computations particularly easy. Matlab is based on a kernel of general purpose functions enhanced with additional tools, the so-called Toolboxes, that help users to solve specific problems, e.g. the Control System Toolbox. A toolbox is a simple collection of matlab functions. MATLAB introduction -- 3

Matlab Desktop Workspace Command window MATLAB introduction -- 4

Command Window MATLAB commands must be inserted by means of the Command Window. Some commands of general use are pwd provides the current directory dir lists the files of current directory clc clears the command window Variables defined in the MATLAB environment are collected in the WorkSpace. who lists the variables in the current workspace. Command clear removes all variables from the workspace. Command help provides the list of all the toolboxes which are installed in the system. By typing in >> help <toolbox name> one obtains the list of the functions composing the toolbox (e.g. help control). The command >> help <command name> provides a description of this command MATLAB introduction -- 5

Variables MATLAB variables are created with an assignment statement >> variable name = a value (or an expression) where expression is a combination of numerical values, mathematical operators, variables, and function calls For example >> x=12;<enter> By omitting the semicolon (;) the name and the value of the variable are printed in the screen. Conversely, the echo of the command is not provided Once a variable has been created, it can be reassigned >> t = 5; >> t = t+1 t = 6 Theintermediateresultisnotshown MATLAB introduction -- 6

Error messages If we enter an expression incorrectly, MATLAB will return an error message. For example >> x = 10; >> 5x??? 5x Error: Unexpected MATLAB expression. MATLAB introduction -- 7

Basic mathematical functions Trigonometric. sin - Sine sinh - Hyperbolic sine. asin - Inverse sine. cos - Cosine. cosh - Hyperbolic cosine. acos - Inverse cosine. tan - Tangent. tanh - Hyperbolic tangent. atan - Inverse tangent. atan2 - Four quadrant inverse tangent. Exponential. exp - Exponential. log - Natural logarithm. log10 - Common (base 10) logarithm. sqrt - Square root. Complex. abs - Absolute value. angle - Phase angle. Rounding and remainder. floor - Round towards minus infinity. ceil - Round towards plus infinity. round - Round towards nearest integer. mod - Modulus (signed remainder after division). rem - Remainder after division. sign - Signum. MATLAB offers many predefined mathematical functions for technical computing. Typing help elfun and help specfun calls up full lists of elementary and special functions that are built into MATLAB.. Note that trigonometric functions work in radiants MATLAB introduction -- 8

Predefined constant values In addition to the elementary functions, MATLAB includes a number of predefined constant values. The most common values are: pi The p number, p =3.14159 i,j Inf NaN The immaginary unit i The infinity, Not a number MATLAB introduction -- 9

Matrices and vectors To type a matrix into MATLAB it is necessary to begin with a square bracket, [ separate elements in a row with spaces or commas (,) use a semicolon (;) to separate rows end the matrix with another square bracket, ]. Example >> A=[1,2,3;4,5,6;7,8,9] A = 1 2 3 4 5 6 7 8 9 MATLAB introduction -- 10

Matrices and vectors A vector is a special case of a matrix >> c=[4;5;6] >> r=[4,5,6] c = 4 5 6 column vector r = 4 5 6 row vector A row vector can be converted to a column vector using the transpose operator, and vice-versa. The transpose operation is denoted by an apostrophe or a single quote ('). The easiest way of defining a vector where the elements (components) increase by the same amount is. >> t=[0:0.1:10] row vector with elements from 0 to 10 with step 0.1 MATLAB introduction -- 11

Matrices and vectors indexing Once we have entered a matrix, we can refer to it simply as matrix A. We can then view a particular element in a matrix by specifying its location >> Element = A(2,1) Element = Note that the indices start from 1 column index row index 4 Correcting any entry is easy through indexing >> A(3,3) = 0 A = 1 2 3 4 5 6 7 8 0 Here we substitute A(3,3)=9 by A(3,3)=0 MATLAB introduction -- 12

The colon operator (:) The colon operator can be used to pick out a certain row or column. For example, the statement A(m:n,k:l) specifies rows m to n and column k to l. Example 1 >> A(1,:) ans = 1 2 3 Example 2 >> A(:,1) ans = 1 4 7 Example 3 >> B=A(2:3,1:2) B = 4 5 7 8 First row of A First column of A Sub-matrix of A MATLAB introduction -- 13

Matrix generators MATLAB provides functions that generate elementary matrices: A=eye(n); A=eye(n,m); A=zeros(m,n); A=ones(m,n); n-by-n identity matrix m-by-n matrix with 1 on the main diagonal m-by-n matrix of zeros m-by-n matrix of ones A=rand(m,n); m-by-n matrix of random numbers A=diag(V); n-by-n matrix with the element of vector V on the main diagonal For a complete list of elementary matrices and matrix manipulations, type help elmat or doc elmat MATLAB introduction -- 14

Concatenating matrices Matrices can be made up of sub-matrices Problem: make up the 6-by-6 matrix where and v 1, v 2 e v 3 are column vetors defined by the user. Solution: >> v1 = [1 2 3]'; >> v2 = rand(3,1); >> v3 = [3; 2; 1]; >> A11=[v1 v2 v3]; >> A22=[v3 v2 v1]; >> A = [A11, eye(3); zeros(3), A22] MATLAB introduction -- 15

Array operations MATLAB allows the following arithmetic operations on matrices: addition + >> A+B subtraction >> A-B valid if A and B are of the same size multiplication * >> A*B (right and left) divisions / \ Exponentiation ^ >> A^2 >> A/B >> A\B Equivalent to A*inv(B) valid if A's number of column equals B's number of rows Equivalent to inv(a)*b valid if A is square and equals A*A MATLAB introduction -- 16

Operations element-by-element Arithmetic operations can be done element-by-element. The period character (.) distinguishes these operations from standard matrix operations. multiplication.* >> A.*B (right and left) divisions./.\ >> A./B >> A.\B A and B are of the same size Exponentiation.^ >> A.^B Example: >> v = [1 2 3].*[1 2 3] v = 1 4 9 MATLAB introduction -- 17

Matrix functions MATLAB provides many matrix functions for various matrix/vector manipulations Dimensions >> [m,n]=size(a) for vectors, see command length Transpose Determinant number of column number of row >>B=A (alternatively >>B=transpose(A) ) >> d=det(a) A must be square MATLAB introduction -- 18

Matrix functions Inverse >>I=inv(A) A must be square For rectangular matrices, see command pinv Rank, i.e. number of linearly independent rows or columns >> r=rank(a) Eigenvalues >> e=eig(a) Vector containing the eigenvalues MATLAB introduction -- 19

Solution of a linear system Problem: solve the system Solution: >> A = [1, 1, 1, -1; 1, 1, -1, 0; 1, -1, 1, 0; 1, 2, -3, 0]; >> b = [1, 2, 0, 2]'; >> x = inv(a)*b; or >> x = A\b MATLAB introduction -- 20

Vectors role The vectors have in MATLAB two fundamental functions: polynomials representation, a polynomial is represented by the vector of its coefficients signals representation, a signal is represented by the sequence of values that it takes during time, therefore by a vector MATLAB introduction -- 21

Operations on polynomials Polynomial pol (= 3 s 2 + 2 s+ 1) can be defined with the statement: >> pol= [3 2 1] pol = 3 2 1 roots: roots computation (pol=0): >> roots(pol) ans = -0.3333 + 0.4714i -0.3333-0.4714i polyval: pointwise evaluation of a polynomial: >> polyval(pol,1) ans = 6 MATLAB introduction -- 22

Operations on polynomials Computation of the residues, poles and direct term of the partial fraction expansion of the ratio of two polynomials: es. >> num = [2 5 3 6]; den = [1 6 11 6]; >> [r,p,k] = residue(num,den) r = -6.0000-4.0000 3.0000 p = -3.0000-2.0000-1.0000 k = 2 MATLAB introduction -- 23

Operations on polynomials Polynomial multiplication ( pol3=(s+1)(s+1) ): >> pol1=[1 1]; pol2=[1 1]; >> pol3=conv(pol1,pol2) pol3 = 1 2 1 Polynomial division ( (s 2 +2s+2)=q(s)(s+1)+r(s) ): >> pol1=[1 2 2]; pol2=[1 1]; >> [q,r]=deconv(pol1,pol2) q = quotient 1 1 r = 0 0 1 remainder MATLAB introduction -- 24

Basic plotting The basic MATLAB graphing procedure, for example in 2D, is to take a vector of x-coordinates, x = (x1; : : : ; xn), and a vector of y- coordinates, y = (y1; : : : ; yn), locate the points (xi; yi), with i = 1; 2; : and then join them by straight lines. This procedure is made by the command plot. Example: >> x = [1 2 3 4 5 6]; >> y = [3-1 2 4 5 1]; >> plot(x,y) x and y must have the same length 5 4 3 2 1 0-1 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 Problem: plot the function sin(x)for x from 0 to 2p MATLAB introduction -- 25

Adding titles, axis labels, and annotations 1 0.8 0.6 0.4 >> x=0:0.1:2*pi; >> plot(x,sin(x)) 0.2 0-0.2-0.4-0.6-0.8-1 0 1 2 3 4 5 6 7 MATLAB enables you to add axis labels, titles, and a grid. Sine of x 1 0.8 0.6 0.4 0.2 0-0.2-0.4-0.6-0.8 Plot of the Sine function >> grid on >> xlabel('x = 0:2\pi') >> ylabel('sine of x') >> title('plot of the Sine function') -1 0 1 2 3 4 5 6 7 x = 0:2 MATLAB introduction -- 26

Specifying line styles and colors It is possible to specify line styles, colors, and markers (e.g., circles, plus signs,... ) using the plot command: plot(x,y,'style_color_marker') where style_color_marker is a triplet of values from the following table Symbol Color Symbol Line Style Symbol Color k Black - Solid + Plus sign r Red -- Dashed o Circle b Blue : Dotted * Asterisk g Green -. Dash-dot. Point c Cyan x Cross m Magenta s Square y Yellow d Diamond MATLAB introduction -- 27

Multiple data sets in one plot The command hold on holds the current plot and all axis properties so that subsequent graphing commands add to the existing graph. 1 0.8 0.6 0.4 0.2 Plot of the Sine function Sine of x 0-0.2-0.4-0.6-0.8-1 0 1 2 3 4 5 6 7 x = 0:2 Plot of the Sine function 1 sin 0.8 cos >> hold on >> plot(x, cos(x),'r--') >> legend('sin', 'cos') 0.6 0.4 0.2 Sine of x 0-0.2-0.4-0.6-0.8-1 0 1 2 3 4 5 6 7 x = 0:2 MATLAB introduction -- 28

Multiple data sets in one plot The command subplot(m,n,p)breaks the Figure window into an m- by-n matrix of small axes, and selects the p-th axes for the current plot >> subplot(2,1,1) >> plot(x,sin(x)) >> ylabel('sin') >> subplot(2,1,2) >> plot(x,cos(x)) >> ylabel('cos ) sin 1 0.5 0-0.5 subplot #1-1 0 1 2 3 4 5 6 7 1 0.5 cos 0-0.5 subplot #2-1 0 1 2 3 4 5 6 7 MATLAB introduction -- 29

Miscellaneous on plot The command figure opens a new figure or can be used to select a figure previously defined (figure(fignum)) The command close(fignum)closes a specific figure. The command close all closes all the figure defined in the MATLAB session The command print can be use to produce jpeg or eps images from the current figure >> print depsc FileName It produces the file FileName.eps >> print djpeg FileName It produces the file FileName.jpg MATLAB introduction -- 30

Master Degree Course in ELECTRONICS ENGINEERING http://www.dii.unimore.it/~lbiagiotti/systemscontroltheory.html A General Introduction to Matlab e-mail: luigi.biagiotti@unimore.it http://www.dii.unimore.it/~lbiagiotti