Академический Документы
Профессиональный Документы
Культура Документы
Default View
Command Window main window to enter commands and run programs
Current Folder Window shows MATLAB m-files in the current folder
Workspace Window provides information about currently used variables
Command History Window log of commands entered in the command window
Chapter 1 Slide 1
MATLAB Windows
Additional Windows
Figure Window displays graphs from graphics commands automatically upon execution of the
graphics command
Editor Window provides capability for writing and editing programs and is opened under the
File New Script menu or
button
Help Window provides help information interactively and is accessed via the Help menu or F1
Chapter 1 Slide 2
MATLAB Windows
Configuring MATLAB
Close an individual window within MATLAB by clicking the
Desktop Layout menu provides options for opening and closing various MATLAB windows or
combinations of MATLAB windows
Undock a window by clicking
Redock by toggling
corner of the window
and then
Chapter 1 Slide 3
Command Window
Entering commands once a command is entered it cannot be modified and reexecuted
>> denotes the command line prompt
Edit commands before they are executed by using backspace, left-arrow, right-arrow and delete keys
Select and edit previous commands entered using the or
Smart recall retrieves previously entered commands based on the first few characters of the
command and the up-arrow key
Tab completion allows automatic completion of a previously defined name based on the first few
letters of the name
Semicolon (;) prevents the value of a command from being written
Commas (,) or semicolons (;) allow several commands on the same line
Ellipse () may be used to continue a long command onto another line
% symbol denotes a comment line that is not executed (documenting)
Chapter 1 Slide 4
Exponential ( 5 ^ 3 or a ^ b)
Order of Precedence
Parentheses starting from the innermost
Exponentiation
Multiplication and division
Addition and subtraction
Chapter 1 Slide 5
Answer Display
Format Command - changes the display format of numbers
Style
Result
short (default)
Short fixed decimal format, with 4 digits after the decimal point. If you are displaying a matrix with a
wide range of values, consider using shortG
3.1416
long
Long fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after
the decimal point for single values.
3.141592653589793
shortE
Short scientific notation, with 4 digits after the decimal point. Integer-valued floating-point numbers
with a maximum of 9 digits do not display in scientific notation.
3.1416e+00
longE
Long scientific notation, with 15 digits after the decimal point for double values, and 7 digits after
the decimal point for single values. Integer-valued floating-point numbers with a maximum of 9
digits do not display in scientific notation.
3.141592653589793e+00
shortG
The more compact of short fixed decimal or scientific notation, with 5 digits.
3.1416
longG
The more compact of long fixed decimal or scientific notation, with 15 digits for double values, and
7 digits for single values.
3.14159265358979
shortEng
Short engineering notation, with 4 digits after the decimal point, and an exponent that is a multiple
of 3.
3.1416e+000
longEng
bank
Example
Long engineering notation, with 15 significant digits, and an exponent that is a multiple of 3.
Currency format, with 2 digits after the decimal point.
3.14159265358979e+000
3.14
Chapter 1 Slide 6
Answer Display
Format Command - changes the display format of line spacing
Style
Result
Example
Suppresses excess line feeds to show more output in a single screen. Contrast with loose.
theta = pi/2
theta =
1.5708
theta = pi/2
compact
loose
theta =
1.5708
Chapter 1 Slide 7
Functions
Mathematical Functions
Absolute value (abs)
Sine (sin or sind), cosine (cos or cosd), tangent (tan or tand), arcsine (asin), arccosine (acos),
arctangent (atan or atan2)
Round (round), ceiling (ceil), floor (floor)
Square root (sqrt)
Natural logarithm (log), logarithm base 10 (log10)
Many others use shift+F1 to browse available functions
General Functions
Clock provides a vector as [year month day hour minute second]
Date provides the current date as dd-mm-yyyy
Calendar provides a calendar for the current month or specified month and year
Many others
Chapter 1 Slide 8
Scalar Number
Constants
pi ()
eps represents the smallest difference between two numbers ( 2.2204e-16)
i or j represents the imaginary number
Indefinite numbers
Infinity is a valid input and is designated by Inf
Division by zero gives a warning and returns Inf
Chapter 1 Slide 9
Variables
Variables - fundamental to programming and are actually pointers to allocated memory space
for storage
Name starts with a letter character
Name may only have letters, underscore, and the digits 0-9
Should not conflict with exiting variables or functions built into MATLAB
It is possible to reassign Inf for example but it may become confusing
Often i or j are reassigned as loop variables when a program does not deal with complex numbers
Cannot be defined as a MATLAB keyword such as (for, if, else, break, etc.)
If defined as an existing MATLAB function, function cannot be used
ans variable of the last expression calculated not assigned to a variable
Chapter 1 Slide 10
Chapter 1 Slide 11
Script Files
File with a list of commands that are executed sequentially in the same manner as the command
window
Chapter 1 Slide 12
Saving
File name requirements are the same as variable names
From Editor Window save your M-file by one of the following:
File Save as menu
Save File button
Ctrl + S
Chapter 1 Slide 13
Chapter 1 Slide 14
Chapter 1 Slide 15
Examples
Problem 1
43 145 30 ln 50
3
5
30
5e 4
6!
ln 90
Problem 2
Define the variables a and b as: a=14, b= 21 then evaluate:
ab
a
sin e a b cos a
a log10 b
b
Problem 3
Define the variables x as x =12 degrees and verify the identity is correct:
1
sin x 3sin x sin 3 x
4
3
Chapter 1 Slide 16
Examples
Problem 4
The prices of an oak tree and a pine tree are $54.95 and $39.95, respectively. Change the display format to
bank, and calculate the following:
(a) the total cost of 16 oak trees and 20 pine trees.
(b) the same as part (a), and add 6.25% sale tax.
(c) the same as part (b) and round the total cost to the nearest dollar.
Chapter 1 Slide 17