matlab tutorial for eece 360

40
EECE 360 EECE 360 Matlab Tutorial Matlab Tutorial Jan 29 2008 Jan 29 2008

Upload: kararc

Post on 15-Nov-2014

133 views

Category:

Documents


10 download

TRANSCRIPT

Page 1: Matlab Tutorial for EECE 360

EECE 360 EECE 360 Matlab TutorialMatlab Tutorial

Jan 29 2008Jan 29 2008

Page 2: Matlab Tutorial for EECE 360

OutlineOutline

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

Page 3: Matlab Tutorial for EECE 360

Overview of MatlabOverview of Matlab

Page 4: Matlab Tutorial for EECE 360

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 programComposed using high-level program

ming language ming language similar to syntax of Csimilar to syntax of C

For EECE 360 For EECE 360 Use Matlab in the Use Matlab in the assignmentsassignments

Page 5: Matlab Tutorial for EECE 360

Matlab InterfaceMatlab Interface

Page 6: Matlab Tutorial for EECE 360

Command Window

Workspace

CommandHistory

Change the current directory to the location of your Matla

b file at startup

Page 7: Matlab Tutorial for EECE 360

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: Matlab Tutorial for EECE 360

Command Window InterfaceCommand Window Interface

Write your commands here

Page 9: Matlab Tutorial for EECE 360

(1) Creating .m-files

File New M-File

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

Page 10: Matlab Tutorial for EECE 360

.m-file Interface.m-file Interface

Write your commands here

To run the program, press F5

Page 11: Matlab Tutorial for EECE 360

Workspace InterfaceWorkspace Interface

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

Page 12: Matlab Tutorial for EECE 360

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: Matlab Tutorial for EECE 360

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: Matlab Tutorial for EECE 360

Examples on Examples on Matlab Interface Matlab Interface

Page 15: Matlab Tutorial for EECE 360

Basic SyntaxBasic Syntax

Page 16: Matlab Tutorial for EECE 360

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: Matlab Tutorial for EECE 360

Basic Syntax (cont’d)Basic Syntax (cont’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: Matlab Tutorial for EECE 360

Basic Syntax (cont’d)Basic Syntax (cont’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: Matlab Tutorial for EECE 360

Basic Syntax (cont’d)Basic Syntax (cont’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: Matlab Tutorial for EECE 360

Examples on Examples on Basic SyntaxBasic Syntax

Page 21: Matlab Tutorial for EECE 360

Plotting GraphsPlotting Graphs

Page 22: Matlab Tutorial for EECE 360

Plotting GraphsPlotting Graphs

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

TTypes of plots ypes of plots

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

-- Solid lineSolid line

---- Dashed lineDashed line

:: Dotted lineDotted line

-.-. Dashdot lineDashdot line

Page 23: Matlab Tutorial for EECE 360

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

Labeling GraphsLabeling GraphsCommonly used Syntax:Commonly used Syntax:

ylabel(‘text’)ylabel(‘text’) – label y-axis – label y-axisxlabel(‘text’)xlabel(‘text’) – label x-axis – label x-axistitle(‘text’)title(‘text’) – label title – label titlelegend(‘legend1’, ‘length2’,…)legend(‘legend1’, ‘length2’,…) – puts legend on cur – puts legend on cur

rent plotrent 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 win – hold the previous plot on the graphic win

dowdow

Page 24: Matlab Tutorial for EECE 360

Example (1)Example (1)

Page 25: Matlab Tutorial for EECE 360
Page 26: Matlab Tutorial for EECE 360

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: Matlab Tutorial for EECE 360

Examples onExamples onPlotting GraphsPlotting Graphs

Page 28: Matlab Tutorial for EECE 360

Control System ToolboxControl System Toolbox

Page 29: Matlab Tutorial for EECE 360

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: Matlab Tutorial for EECE 360

Demo (1)Demo (1)Transfer FunctionTransfer Function

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

Page 31: Matlab Tutorial for EECE 360

Demo (2)Demo (2)

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

Page 32: Matlab Tutorial for EECE 360

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: Matlab Tutorial for EECE 360
Page 34: Matlab Tutorial for EECE 360

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: Matlab Tutorial for EECE 360

Demo (5) Demo (5)

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

Page 36: Matlab Tutorial for EECE 360
Page 37: Matlab Tutorial for EECE 360

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: Matlab Tutorial for EECE 360
Page 39: Matlab Tutorial for EECE 360

Examples onExamples onControl System ToolboxControl System Toolbox

Page 40: Matlab Tutorial for EECE 360

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