You are on page 1of 25

Class 11: Introduction to MATLAB

Presentation
Department of Engineering Education
ENGR 1181

Today’s Learning Objectives


Students will be introduced to:
1. Proper usage of basic MATLAB features.
2. The need to translate mathematical notation into
proper MATLAB syntax.
3. The basic structure of a for-end loop.
4. Advanced capabilities of MATLAB in various
engineering fields.
5. Clear expectations for MATLAB
assignments (COAM). 2
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

How to open MATLAB


The following
CLICK on the prompt should
shortcut icon on
desktop appear in the
command window
after a lengthy
initialization
Start  process:
All programs 
MATLAB >>
5
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

MATLAB’s Working Directory


• Current working directory is where files are saved and run
from
• When you first start MATLAB, it is a good practice to change
the working directory to your S: drive or USB device using
the browse icon :

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

>> 10+12/2 >> (10+12)/2


ans = ans =

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

• It creates a 1x1 matrix named x and stores the value 5 in


its element.
• Please note, the value of x can be replaced with a new
value based on its previously defined value:
x = x + 2 % this results in x = 7

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

fprintf ( ) – Basic Format


Allows more information in a single line
It can display 1 line of text:
fprintf(‘\nText and more text.’)

It can show text and variable output(s) on the same line:


fprintf(‘\nText and %value_display and text.’, variable)
fprintf(‘\nText %value_display1 and %value_display2.’,var1,var2)

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:

The area of this circle is 0.79 14


Department of Engineering Education
ENGR 1181

Script Files – Overview


• Script files are:
− a collection of commands executed in sequence
− written in the MATLAB editor
− saved as MATLAB files (.m extension  m-file)

• Script files can be created from the command window by


typing:
>> edit NewNameofScript.m
• Or by clicking
15
Department of Engineering Education
ENGR 1181

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

MATLAB Display + Editor Window


1. Ribbon 1
2. Current Directory
3. Command Window
4. Workspace
4
5. Command History 6

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

Script Files – How to Save


Once the script file is completed, it must be saved:
• In our class, use Save As to your Z:\ or USB drive.
• This should be the same as the working directory you
specified when you started MATLAB
The name of the script file follows the rules for names of
variables in MATLAB:
• Can be up to 63 characters long.
• Must begin with a letter.
• Can only contain letters, digits, and _
• Case sensitive!
18
Department of Engineering Education
ENGR 1181

Script Files – How to Run


• A script file can be executed from the Ribbon by clicking
on the Run icon (this saves the m-file before running)

• A script file can also be executed from the Command


Window by typing its name and pressing Enter.

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.

Pass 1 Pass 2 Pass 3 Pass 4 Pass 5


k=1 k=2 k=3 k=4 k=5
(=1+1) (=2+1) (=3+1) (=4+1)

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

HW Assignments Due Sunday, September 30th


Before next class, make sure you have a full understanding of:
• MATLAB interface
• Calculations
• Variables
• How to create a basic for-end loop

24
Department of Engineering Education
ENGR 1181

Looking Ahead
Class 14: Data Analysis and Input

You will learn about:


• Plotting
• Loading data into MATLAB
• The input() command

25

You might also like