EECE 360 EECE 360 Matlab Tutorial Matlab Tutorial Jan 29 2008 Jan 29 2008
Nov 15, 2014
EECE 360 EECE 360 Matlab TutorialMatlab Tutorial
Jan 29 2008Jan 29 2008
OutlineOutline
What is Matlab?What is Matlab?Matlab InterfaceMatlab InterfaceBasic SyntaxBasic SyntaxPlotting Graphs Plotting Graphs Control System ToolboxControl System ToolboxExamplesExamplesUseful ResourcesUseful Resources
Overview of MatlabOverview of Matlab
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
Matlab InterfaceMatlab Interface
Command Window
Workspace
CommandHistory
Change the current directory to the location of your Matla
b file at startup
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
Command Window InterfaceCommand Window Interface
Write your commands here
(1) Creating .m-files
File New M-File
(2) Opening .m-filesFile Open select the .m-file
.m-file Interface.m-file Interface
Write your commands here
To run the program, press F5
Workspace InterfaceWorkspace Interface
The workspace provides a summary of the The workspace provides a summary of the variables variables
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
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
Examples on Examples on Matlab Interface Matlab Interface
Basic SyntaxBasic Syntax
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
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
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
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
Examples on Examples on Basic SyntaxBasic Syntax
Plotting GraphsPlotting Graphs
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
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
Example (1)Example (1)
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,’--’)
Examples onExamples onPlotting GraphsPlotting Graphs
Control System ToolboxControl System Toolbox
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
Demo (1)Demo (1)Transfer FunctionTransfer Function
Syntax: Syntax: tf(numerator, denomintor)tf(numerator, denomintor)
Demo (2)Demo (2)
State-space RepresentationState-space Representation Syntax: Syntax: sys_ss = ss(sys)sys_ss = ss(sys)
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)
Demo (4)Demo (4) Closed-Loop SystemClosed-Loop System
Syntax: Syntax: CLsys = feedback(sys1,sys2)CLsys = feedback(sys1,sys2)
System 1
System 2
+
-
Demo (5) Demo (5)
Step ResponseStep ResponseSyntax: Syntax: step(sys)step(sys)
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)
Examples onExamples onControl System ToolboxControl System Toolbox
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