Professional Documents
Culture Documents
Presentation
Department of Engineering Education
ENGR 1181
Today’s Topics
1. Introduction to the MATLAB interface
2. Order of precedence
3. Variables
4. fprintf
5. Script files
6. Basic for loop
7. Modeling & simulation
8. COAM scenarios
3
Department of Engineering Education
ENGR 1181
What is MATLAB?
• Stands for “MATrix LABoratory”
• Powerful programming language
• It’s used for computation, modeling,
visualization and much more
4
Department of Engineering Education
ENGR 1181
MATLAB Display
1. Ribbon: useful
operations 1
2. Current Directory:
list of files
3. Command Window:
main window, 4
contains command
prompt (>>) 2 3
4. Workspace: defined
variable values
5. Command History:
displays a list of 5
previously typed
commands
6
Department of Engineering Education
ENGR 1181
7
Department of Engineering Education
ENGR 1181
Order of Precedence
• Higher-precedence operations are executed before lower-
precedence operations
• Two operations with same precedence are executed L
R
Parentheses Multiplication Addition
(innermost
pair first)
> Exponentiation
> &
Division
> &
Subtracti
on
First Third Fourth
Second
8
Department of Engineering Education
ENGR 1181
Order of Precedence
Example: Find the average of two numbers, 10 and 12
16 11
9
Department of Engineering Education
ENGR 1181
Variables – Definition
• You can assign variables to an explicit value.
x = 5 % defining x and initializing it with a
value
10
Department of Engineering Education
ENGR 1181
Variables – Naming
1. Cannot exceed 63 characters
2. Must begin with a letter
3. May contain letters, numbers and _
− NO spaces allowed
4. Case sensitive! (var1 is different from Var1)
5. Avoid naming variables with currently defined MATLAB
functions
− Such as: exp, sin, cos, sqrt, length, mean, max, min,
etc.
11
Department of Engineering Education
ENGR 1181
Variables – Calculations
• Variables defined previously can be used to do calculations
Define: a = 5; b = 15;
>> a + b
ans =
20
>> a * b^2
ans =
1125
12
Department of Engineering Education
ENGR 1181
13
Department of Engineering Education
ENGR 1181
fprintf ( ) – Value
Display
Conversion
conversion
Start of
specification
(required)
% 3.2 f character
(required)
Field width Precision
(optional) (optional)
Example Code:
x = pi*(1/2)^2;
fprintf(‘\nThe area of this circle is %3.2f’,x)
Will display:
Script Files
Using a script file is convenient because it can be:
• Edited (corrected and/or changed)
• Executed many times with reliable results
• Saved, then opened at a later date
• Sent to others to use
16
Department of Engineering Education
ENGR 1181
2
6. Editor Window:
the commands of
the script file are 5
typed out line- 3
by- line.
17
Department of Engineering Education
ENGR 1181
19
Department of Engineering Education
ENGR 1181
Loops – Overview
A loop allows a group of commands in a program to be
repeated.
• Each repetition of the loop is called a pass
• The loop terminates:
− After a fixed number of passes OR
− After some condition is satisfied
20
Department of Engineering Education
ENGR 1181
for-end Loop –
Structure
Loop index
The value The increment of k
after each pass
of k in
variable the
first pass k=k+step
(used to control
the looping The stopping
process) for k = start:step:stop condition. The
........ loop continues
until
........ A group
MATLAB k+step > stop
........ of commands
end
21
Department of Engineering Education
ENGR 1181
for-end Loop –
Example
for k = 1 : 1 : 5 The loop will repeat itself until the
k loop index variable, k, equals the
last value, 5, for a total of 5 passes.
end k equals 5 after the loop ends.
22
Department of Engineering Education
ENGR 1181
Important Takeaways
Programming is a useful and powerful tool for problem
solving in any engineering discipline.
• MATLAB is a tool to solve these types of problems
• Basics of MATLAB interface
• Variable naming and rules
• fprintf
• Script files
• Basic for-end loop structure
23
Department of Engineering Education
ENGR 1181
24
Department of Engineering Education
ENGR 1181
Looking Ahead
Class 14: Data Analysis and Input
25