chapter 1 – matlab overview egr1302. desktop command window current directory window command...

23
Chapter 1 – Matlab Overview EGR1302

Upload: martina-fisher

Post on 05-Jan-2016

235 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Chapter 1 – Matlab OverviewEGR1302

Page 2: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

DesktopCommand window

Current Directory window

Command History window

Tabs to toggle between Current Directory & Workspace Windows

Page 3: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Desktop

Default appearance Command window

Communicates with MATLAB program Type commands Receive response to your commands

Displays “>>” prompt MATLAB is ready to receive instructions User types command, presses “Enter” to

execute “;” at end of command suppresses

response Retains previous keystrokes

Use “↑” key to scroll backward Use “↓” key to scroll forward Edit with “←”, “→”, “Backspace”, “Delete”

Page 4: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Desktop

Default appearance Command History window

Shows all previous keystrokes Copies previous command to

Command window Left-click and drag to Command window Double-click to re-execute command

Page 5: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Desktop

Default appearance Current Directory window

Serves as file manager window Double-click on .m file to open Editor

window

Workspace window Lists variables created in Command

window Double-click on variable name to open

Array Editor window

Page 6: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Desktop

Default appearance Row of menu names

Each window type has its own menu bar

Toolbar Shortcut buttons

Current directory box Change to directory in which you save

your files Provides path for Current Directory window

Page 7: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Variables

A symbol used to contain a value Used to write mathematical

expressions Variable naming convention

Must begin with a letter Must contain < 32 characters Is case sensitive May contain letters, digits, &

underscore Scalar variable - a single number Array variable - a collection of

numbers Can be handled by MATLAB as a single

variable

Page 8: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Variables

Assigning values to variables Use “=“ as assignment operator

Variable name on left-hand side is replaced by value generated by right-hand side

Right-hand side must have computable value

Array assignment “[]” contains numbers being assigned Commas separate elements in row Semi-colons separate rows

Page 9: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Variables Array assignment (cont)

Special format for assigning row array with regularly spaced numbers

Type first number, last number, with spacing in middle

All numbers separated by colon “:”

10:1.0:0u

Page 10: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Variables

Array addressing Use just variable name in

expressions Operation is performed on every

element in the array IF matrix operation is “legal”

Use variable name & array index in expressions

Point to a particular element in the array

Use if operation to be performed is not “legal” matrix operation

)7(u

Page 11: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Variables

MATLAB has predefined variables

Avoid using names of predefined variables as variable names

Page 12: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Mathematical Expressions Order of precedence

Highest order of precedence first Operators of equal precedence from

left to right

Page 13: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Functions

MATLAB mathematical functions Pair of parentheses after function

name encloses function’s argument Can be part of mathematical

expression

Page 14: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Functions

Users can define functions MATLAB commands and

functions for managing the work session

Page 15: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Plotting

Graphics window Appears in

Desktop when a plot is generated

Page 16: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Plotting

Simple rectilinear plot Function plot(x,y)

Generates a plot with x-values on the horizontal axis and y-values on the vertical axis

Function xlabel(‘xxxx’) Places text within single quotes as

label for horizontal axis Function ylabel(‘yyyy’)

Places text within single quotes as label for vertical axis

Page 17: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Plotting

Simple rectilinear plot (cont) Function title(‘text’)

Places text at top of plot Overlay plots

Include another set or sets of values in plot function

Page 18: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Script Files

Program files saved with .m extension Use Editor window

Page 19: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Script Files

Naming convention Same as for naming variables Not the same name as a variable or

a MATLAB function Use exist(‘name’) function to verify

that a prospective filename is not already in use

Page 20: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Script Files

Comments “%” - Not executed by MATLAB Comments section – at beginning of

program 1st line - name of the program,

keywords 2nd line - date created and

programmer’s name 3rd line – description of program’s

purpose Variable definitions

Input Output Internal to program

Page 21: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Script Files

Comments Additional comments throughout

program Break program into sections. Describe purpose for calculations

Programming in MATLAB Relational operators

Used to make comparisons

Page 22: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Script Files

Programming in MATLAB (cont) Conditional statements

Allow decision-makingif expression commandselseif expression commandselse commandsend

Page 23: Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &

Script Files

Programming in MATLAB (cont) Loops

Repeated calculations For Number of passes is known ahead of

time While Looping terminates when a

condition is met

for k=1:10 commandsend

while condition commandsend