Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command...
-
Upload
martina-fisher -
Category
Documents
-
view
226 -
download
2
Transcript of Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command...
Chapter 1 – Matlab OverviewEGR1302
DesktopCommand window
Current Directory window
Command History window
Tabs to toggle between Current Directory & Workspace Windows
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”
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
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
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
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
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
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
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
Variables
MATLAB has predefined variables
Avoid using names of predefined variables as variable names
Mathematical Expressions Order of precedence
Highest order of precedence first Operators of equal precedence from
left to right
Functions
MATLAB mathematical functions Pair of parentheses after function
name encloses function’s argument Can be part of mathematical
expression
Functions
Users can define functions MATLAB commands and
functions for managing the work session
Plotting
Graphics window Appears in
Desktop when a plot is generated
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
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
Script Files
Program files saved with .m extension Use Editor window
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
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
Script Files
Comments Additional comments throughout
program Break program into sections. Describe purpose for calculations
Programming in MATLAB Relational operators
Used to make comparisons
Script Files
Programming in MATLAB (cont) Conditional statements
Allow decision-makingif expression commandselseif expression commandselse commandsend
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