40
EECE 360/460 EECE 360/460 Matlab Tutorial Matlab Tutorial Jan 2011 Jan 2011

EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Embed Size (px)

Citation preview

Page 1: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

EECE 360/460 EECE 360/460 Matlab TutorialMatlab Tutorial

Jan 2011Jan 2011

Page 2: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

OutlineOutline

What is Matlab?What is Matlab?Matlab InterfaceMatlab InterfaceBasic SyntaxBasic SyntaxPlotting Graphs Plotting Graphs Control System ToolboxControl System ToolboxExamplesExamplesUseful ResourcesUseful Resources

Page 3: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Overview of MatlabOverview of Matlab

Page 4: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

What is Matlab?What is Matlab?

Language for technical computingLanguage for technical computing Supports mathematical computation, Supports mathematical computation,

visualization, and programmingvisualization, and programming Composed using high-level Composed using high-level

programming language programming language similar to syntax of Csimilar to syntax of C

For EECE 360 For EECE 360 Use Matlab in the assignmentsUse Matlab in the assignments

Page 5: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Matlab InterfaceMatlab Interface

Page 6: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Command Window

Workspace

CommandHistory

Change the current directory to the location of your

Matlab file at startup

Page 7: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Matlab ProgrammingMatlab Programming

Two ApproachesTwo ApproachesCoding in Command WindowCoding in Command Window

Good for short programsGood for short programsCommands must be re-entered each time you run Commands must be re-entered each time you run

a simulationa simulation

Coding with .m-filesCoding with .m-filesGood for long programsGood for long programsAllows users to save all the commands written in Allows users to save all the commands written in

the .m-filesthe .m-files

Page 8: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Command Window InterfaceCommand Window Interface

Write your commands here

Page 9: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

(1) Creating .m-files

File New M-File

(2) Opening .m-filesFile Open select the .m-file

Page 10: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

.m-file Interface.m-file Interface

Write your commands here

To run the program, press F5

Page 11: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Workspace InterfaceWorkspace Interface

The workspace provides a summary of the The workspace provides a summary of the variables variables

Page 12: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Command History InterfaceCommand History InterfaceThe Command History stores all the commands The Command History stores all the commands

entered in the entered in the Command WindowCommand Window previously previously No history of .m-files is shown hereNo history of .m-files is shown here

Page 13: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Syntax ErrorsSyntax Errors All errors are given in the All errors are given in the Command Window Command Window in in

redred Nature of error is explained Nature of error is explained

Page 14: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Examples on Examples on Matlab Interface Matlab Interface

Page 15: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Basic SyntaxBasic Syntax

Page 16: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Basic SyntaxBasic Syntax

Assignment of VariablesAssignment of VariablesSyntax: Syntax: Variable = ExpressionVariable = ExpressionExample: Example: A = 1;A = 1;

Use of semi-colonsUse of semi-colonsWith semi-colon – suppress outputWith semi-colon – suppress outputWithout semi-colon – displays output in Without semi-colon – displays output in

command windowcommand windowAdding commentsAdding comments

Add Add % % before command linebefore command lineExample:Example: % This is a demo % This is a demo

Page 17: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Basic Syntax (contBasic Syntax (cont’’d)d) Step incrementStep increment

Example: Example: k = 1:0.1:10k = 1:0.1:10 k is a row-matrix from 1 to 10 with step size = 0.1k is a row-matrix from 1 to 10 with step size = 0.1

Matrix ExpressionMatrix Expression

Hence, for a 2x2 matrix, the syntax isHence, for a 2x2 matrix, the syntax is A = [1 2; 3 4]A = [1 2; 3 4]

Row-matrixRow-matrix Column-matrixColumn-matrix

Page 18: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Basic Syntax (contBasic Syntax (cont’’d)d) Mathematical (Array) OperatorsMathematical (Array) Operators

Note the dimension of matrix in matrix computationNote the dimension of matrix in matrix computation Example: A: 2x1 matrix, B: 2x2 matrixExample: A: 2x1 matrix, B: 2x2 matrix

A*B A*B invalid operation invalid operation

B*A B*A valid operation valid operation

++ AdditionAddition

-- SubtractionSubtraction

** MultiplicationMultiplication

// DivisionDivision

^̂ PowerPower

Mathematical

++ AdditionAddition

-- SubtractionSubtraction

.*.* MultiplicationMultiplication

././ DivisionDivision

.^.^ PowerPower

Mathematical Array

Page 19: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Basic Syntax (contBasic Syntax (cont’’d)d)

Common Built-in Math FunctionsCommon Built-in Math Functions sin(x)sin(x) - sine - sine cos(x)cos(x) - cosine - cosine tan(x)tan(x) - tangent - tangent exp(x)exp(x) - exponential - exponential log10(x)log10(x) – base10 logarithm – base10 logarithm sqrt(x)sqrt(x) – square root – square root abs(x)abs(x) – absolute value – absolute value

The complete list can be found on pp. 833 of textThe complete list can be found on pp. 833 of text

Page 20: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Examples on Examples on Basic SyntaxBasic Syntax

Page 21: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Plotting GraphsPlotting Graphs

Page 22: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Plotting GraphsPlotting Graphs

Matlab allows graphical displays Matlab allows graphical displays By using the command By using the command plotplot Syntax: Syntax: plot(x)plot(x)

Types of plots Types of plots

Syntax: Syntax: plot(x,plot(x,’’--’’))

-- Solid lineSolid line

---- Dashed lineDashed line

:: Dotted lineDotted line

-.-. Dashdot lineDashdot line

Page 23: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Plotting Graphs (contPlotting Graphs (cont’’d)d)

Labeling GraphsLabeling GraphsCommonly used Syntax:Commonly used Syntax:

ylabel(ylabel(‘‘texttext’’)) – label y-axis – label y-axisxlabel(xlabel(‘‘texttext’’)) – label x-axis – label x-axistitle(title(‘‘texttext’’)) – label title – label titlelegend(legend(‘‘legend1legend1’’, , ‘‘length2length2’’,…),…) – puts legend on – puts legend on

current plotcurrent plotgrid on (grid off)grid on (grid off) – add (remove) grid lines to figure – add (remove) grid lines to figuresubplotsubplot – subdivides graphic window – subdivides graphic windowhold onhold on – hold the previous plot on the graphic – hold the previous plot on the graphic

windowwindow

Page 24: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Example (1)Example (1)

Page 25: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs
Page 26: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Example (2)Example (2)

subplot(2,2,1)plot(x,’r’),hold onplot(y,’--’)

subplot(2,2,2)plot(x,’r’),hold onplot(y,’--’)

subplot(2,2,3)plot(x,’r’),hold onplot(y,’--’)

subplot(2,2,4)plot(x,’r’),hold onplot(y,’--’)

Page 27: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Examples onExamples onPlotting GraphsPlotting Graphs

Page 28: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Control System ToolboxControl System Toolbox

Page 29: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Overview to the ToolboxOverview to the ToolboxModeled control systems asModeled control systems as

(1) transfer functions (1) transfer functions (2) zero-pole-gain (2) zero-pole-gain (3) state-space form(3) state-space form

Can manipulate both Can manipulate both (1) continuous-time system(1) continuous-time system(2) discrete-time system(2) discrete-time system

Can compute and graphCan compute and graph(1) time response (1) time response (2) frequency response(2) frequency response

Page 30: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Demo (1)Demo (1)Transfer FunctionTransfer Function

Syntax: Syntax: tf(numerator, denomintor)tf(numerator, denomintor)

Page 31: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Demo (2)Demo (2)

State-space RepresentationState-space Representation Syntax: Syntax: sys_ss = ss(sys)sys_ss = ss(sys)

Page 32: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Demo (3)Demo (3)

Finding Poles and ZerosFinding Poles and ZerosSyntax: Syntax: z = zero(sys)z = zero(sys)

p = pole(sys)p = pole(sys)

Pole-Zero DiagramPole-Zero DiagramSyntax: Syntax: zplane(z,p)zplane(z,p)

Page 33: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs
Page 34: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Demo (4)Demo (4) Closed-Loop SystemClosed-Loop System

Syntax: Syntax: CLsys = feedback(sys1,sys2)CLsys = feedback(sys1,sys2)

System 1

System 2

+

-

Page 35: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Demo (5) Demo (5)

Step ResponseStep ResponseSyntax: Syntax: step(sys)step(sys)

Page 36: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs
Page 37: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Demo (6)Demo (6)Pole-zero cancellationPole-zero cancellation

Syntax: Syntax: sysr = minreal(sys);sysr = minreal(sys); Impulse ResponseImpulse Response

Syntax: Syntax: impulse(sys)impulse(sys)

Page 38: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs
Page 39: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Examples onExamples onControl System ToolboxControl System Toolbox

Page 40: EECE 360/460 Matlab Tutorial Jan 2011. Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs

Useful ResourcesUseful Resources

Appendix A of Textbook Appendix A of Textbook

End of Chapter – Matlab DemoEnd of Chapter – Matlab Demo

http://www.mathworks.comhttp://www.mathworks.com