ENGR 108
Introduction to Computing for
Engineers and Scientists
LECTURE 2: STARTING OUT IN MATLAB
Objectives
After this lecture, you will be able to:
◦ Use MATLAB to solve simple math problems
◦ Store data in variables
◦ Change the display format of numbers
◦ Save commands in an script and publish the results
◦ Use some of MATLAB’s built-in functions
◦ Get help using the MATLAB references and documentation
◦ Apply MATLAB to solving more complex math problems
2
The MATLAB window
Workspace
The list of
Current variables and
folder their values
Command window
Available files
Use this window to communicate with
MATLAB Command
history
Details A record of all
Information on previous input
selected file
3
Your first calculation
In the command window, type
this input next to the >> prompt:
>> 2+2
and press Enter.
What output do you expect?
4
Practice
Type the following expressions into MATLAB and watch the output:
◦ 5+2
◦ 5*2
◦ 5/2
◦ 5^2 – what does the ^ do?
◦ 3+2*(4+3)
◦ sqrt(5) – what does sqrt do?
◦ cos(pi)
5
Variables
A variable is a named storage
space for data.
Variables can be used in
calculations.
For example, MATLAB stores
the results of the previous
calculation in the variable ans.
6
New variables
The command to make a new
variable is:
variable = value
Try storing the number 5 in the
variable A.
What happens in the
Workspace window?
7
Valid variable names
Variable names must start with a letter.
The only characters allowed in a variable name are letters, numbers
and the underscore (_).
Variable names are case-sensitive: the variable radius is different
than the variable RADIUS.
Some variable names are not available because they are reserved
MATLAB commands.
◦ See p.19 in the book for a list of reserved commands.
MATLAB lets you give variables the same name as other MATLAB
commands, but this is not recommended.
8
Order of operations
MATLAB obeys the arithmetic order of operations.
Evaluate from left to right, starting at the innermost parentheses.
From highest precedence to lowest:
◦ Please (parentheses)
◦ Excuse (exponents)
◦ My Dear (multiplication and division)
◦ Aunt Sally (addition and subtraction)
In MATLAB, add parentheses around any expression that you want to
evaluated first.
Examples
Mathematical expression MATLAB expression Answer
8 + 3(5) 8 + 3 * 5 23
8+3 ⋅5 (8 + 3) * 5 55
3 42 + 5 3 * (4 ^ 2) + 5 53
2
3⋅4 +5 (3 * 4) ^ 2 + 5 149
1
27 3 + 320.2 27 ^ (1 / 3) + 32 ^ 0.2 5
Practice
Calculate the following by hand, then check your work with MATLAB:
10 18
◦ 6 +5 + 5 92
13 7
◦ 6 351 4
+ 140.35
11
Matrices
A matrix is a rectangular
arrangement of data
A matrix's size is its number of
rows by its number of columns
12
Matrices
To enter a matrix in MATLAB:
◦ Surround the matrix with square
brackets
◦ Separate each row with
semicolons (;)
◦ Separate each entry in each row
with a comma or a space
13
Making a list of numbers
The command a:b:c makes a
list of numbers between a and c,
with a step size of b
If b is missing, the step size is 1.
14
Display format
MATLAB displays very small or very large numbers in scientific
notation, using e between the base and the exponent
◦ For example, Avogadro's number 6.022 × 1023 will be displayed in MATLAB
as 6.022e23
15
Display format
Command Description Example
format short Four digits after the decimal 3.1416
format long 16 digits after the decimal 3.141592653589793
format short e Five digits and power of 10 3.1416e+000
format long e 16 digits and power of 10 3.141592653589793e+000
format rat Approximate fraction 355/133
format compact Hides blank lines between >> pi
lines of output ans =
3.1416
format loose Shows blank lines again >> pi
ans =
3.1416
16
Recording your work
To keep a record of all the MATLAB commands you use, type the
command diary filename
Once you are done entering commands, type diary off
You can then open the diary file in any text editor
17
Saving, loading and
clearing variables
To save the variables you've created to a file named filename.mat,
enter the command save filename
◦ If you only want to save some variables, list those variables at the end of the
command: save filename var1 var2
To load the variables back into MATLAB, enter the command load
filename
◦ If you only want to load some variables, list those variables at the end of the
command: load filename var1 var2
To erase all the variables you've made so far, type clear
To clear the command window, type clc
18
Scripts (M-files)
A script is a series of MATLAB
commands stored in a text file
With a script file, you can make
complex series of calculations and
share them with other MATLAB
users
(Older versions of MATLAB call
scripts M-files instead)
19
Scripts (M-files)
To make a new script, go to
File > New > Script
If you want to add a comment
to the script to describe how your
program works, put a percent
sign at the beginning of the line
20
Example
The surface area of a cylinder is
the sum of the area of the circular
bases and the area of the
rectangular surface connecting
them
What is the surface area of a
cylinder of height 20 cm and
radius 5 cm?
21
Example
A script file that could solve this problem would be:
% Define the variables
radius = 5
height = 20
% Calculate the individual areas
base_area = pi*radius^2
rect_area = 2*pi*radius*height
% Add the areas together
surface_area = 2*base_area + rect_area
22
Viewing the output of a script
Save this script with the name "surfacearea", then type the command
surfacearea into the command window
◦ MATLAB shows you the results of the calculations in the command window
In the script editor, go to File > Publish surfacearea
◦ MATLAB makes a formatted file that displays the source code of the script
and its output
23
Cell mode
Cell mode enables you to save
multiple scripts in the same file.
Add Cell
Click the Add Cell button to button
begin using cell mode.
The beginning of each cell is
marked with a double percent
sign.
A cell can be run by itself, or all
cells can be run at once.
24
Publishing a cell mode script
When you publish a cell mode
script, MATLAB generates a
formatted file with a section for
every cell in the script file.
25
What’s a function?
Input Function Output
𝑥 = 10 𝐹(𝑥) = 2𝑥 𝐹(5) = 10
Functions in MATLAB
An example MATLAB function is sqrt, which is used to find the square
root of a number
To find the square root of 81 and store it to the variable a, we use the
command
If the input of a function is a matrix, MATLAB will perform the function
on every element in the matrix
a = sqrt(81)
Output Function Input
27
More inputs or outputs
MULTIPLE INPUTS MULTIPLE OUTPUTS
The function size(a) returns the
number of rows and the number of
columns in an array.
The function rem(a, b) finds
the remainder of a divided by b. >> a = [1, 2; 3, 4]
>> rem(10, 3) >> [r, c] = size(a)
r =
ans =
2
1
c =
2
28
Nesting functions
Functions can be nested inside
each other.
Nested functions are evaluated
from the inside out.
29
Getting help
The Function Browser
◦ This is a categorized list of all
MATLAB functions available to
use
◦ Click the “fx” button on the left
side of the command window
Getting help
The Help Browser
◦ This is a collection of manuals for
MATLAB and its add-ons
◦ Click the question mark button in
the toolbar
Getting help
The help and doc functions
◦ These provide descriptions,
definitions and examples of a
particular function
Getting help
The lookfor function
◦ This searches for any function
whose help contains a key word
Elementary functions
(page 68)
34
Practice
Make a list of numbers from −5 to 5 with a step of 1 and store it in the
variable x, and then
◦ Find the absolute value of every value in x
◦ Find the square root of every value in x
◦ Find the natural logarithm of every value in x
◦ Find ex for every value in x
35
Rounding
(page 73)
36
Practice
A contractor has $2,500 set aside to buy concrete blocks. The
hardware store sells concrete blocks for $2.42 each. How many concrete
blocks can the contractor buy?
37
Discrete math functions
(page 74)
38
Practice
How many prime numbers are less than 100?
Five students are chosen from a class of 12 people. How many
different groups of students can be chosen?
What fraction is approximately equal to 𝑒 𝜋 ?
39
Trigonometry functions
(page 76)
40
Practice
Using MATLAB, find:
◦ sin 2𝜃, 𝜃 = 3𝜋
◦ cos 𝜌 for 𝜌 = 0, 0.2𝜋, 0.4𝜋, … 2𝜋
◦ sin−1 1
◦ cos 45°
◦ the cosecant of 60 degrees
41
Next week
◦ Learning functions for statistics, random numbers and complex numbers
◦ Modifying and retrieving data in matrices
◦ Understanding the limitations of computing with MATLAB
42