You are on page 1of 97

MATLAB Environment

Chapter 2

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Objectives

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

After studying this chapter you should be able to


Start the MATLAB program and solve simple problems
in the command window
Understand MATLABs use of matrices
Identify and use the various MATLAB windows
Define and use simple matrices
Name and use variables
Understand the order of operations in MATLAB
Understand the difference between scalar, array and
matrix calculations in MATLAB

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Objectives - continued

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300

After studying this chapter you should be able to


Express numbers in either floating-point or scientific
notation
Adjust the format used to display numbers in the
command window
Save the value of variables used in a MATLAB session
Save a series of commands in an M-file

400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

In this chapter well

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

Get started with MATLAB


Explore the MATLAB windows
Solve some problems using
MATLAB
Learn how to save our work

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Section 2.1
Getting Started

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

In Windows or Apple operating systems


click on the desktop icon
In Unix type
MATLAB

At the shell prompt

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

MATLAB opens to a default


window configuration

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

MATLAB uses a
standard
windows menu
bar
To exit MATLAB
use the close
icon

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Section 2.2
MATLAB Windows

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

MATLAB uses several different


windows to display data,
commands and results.
They are not necessarily all open
at once

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5

Workspace Window

-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Current Folder
Window
Lists files stored in the
current directory
100

Command Window
Enter commands at the prompt

200

MATLAB Windows

300
400
500

100

200

300

400

500

Command
History Window
Records all commands
issued in the command
window including
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
mistakes
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information
regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Lets look at the windows


one at a time

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Command Window

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

Similar to a scratch pad


Commandyou
Window
Once you hit enter,
cant edit any
commands
You can retype them or use the arrow
keys to retrieve commands and edit
them before hitting enter again

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Command History

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Records the commands you issue


in the command window
When you exit the command
window, or when
you issue the clc
Command History
command, the command window
is cleared
But the command history remains
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Command History

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Double click on a command

100

It executes immediately

200
300
400
500

You can transfer commands from


the command history to the
command window

100

200

300

400

500

Click and drag into the command


window
You can edit the command before
executing
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Workspace Window

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Workspace Window

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

When you define variables in


the command window, they
are listed in the workspace
window

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Scalar
100

Vector

200
300
400
500

100

200

300

400

500

2-D
Matrix

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Current Directory/Folder

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

The current folder window is a list


of files
When you try to load information
from a file or try to save
information MATLAB uses the
current folder

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Document Window

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

If you double click on any variable


in the workspace window
MATLAB launches a document
window containing the array
editor
You can edit variables in the array
editor
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

New Variable
Icon

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

The Document Window


displays the variable
editor

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Figure Window

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

When Figures are created a new


window opens
Its extremely easy to create
graphs in MATLAB

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

First create a vector of x


values then a
corresponding vector of y
values

100

200
300
400
500

100

200

300

400

500

Note: The semicolon


suppresses the output from
each command

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Matlab makes it easy to modify


graphs by adding
Titles
Axis labels

100

Legends

200

Other types of annotations

300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Editing Window

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

This window allows you to type


and save a series of commands
without executing them
There are several ways to open
an editing window
From the file menu
With the new file icon
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

Open an editing window


from the file menu or with
the new file icon

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

New file icon

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Save and Run

100

200
300
400
500

100

200

300

400

500

Write your code in the editing window,


then run it using the Save and Run icon

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Section 2.3
Solving Problems with MATLAB

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Weve already solved some


simple problems
We need to understand how
MATLAB works to solve more
complicated problems

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Variables

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

MATLAB allows you to assign a


value to a variable
A=3
Should be read as A is assigned a
value of 3
Use the variables in subsequent
calculations
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Naming Variables

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

All names must start with a letter


They may contain letters,
numbers and the underscore ( _ )
Names are case sensitive
There are certain keywords you
cant use
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Use the iskeyword function


for a list of keywords

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5

iskeyword

-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

ans =
'break'
'case'
'catch'
'classdef'
'continue'
'else'
'elseif'
'end
'for
'function'

'global'
'if'
'otherwise'
'parfor'
'persistent'
'return
spmd
'switch'
'try'
'while'

Keywords are not acceptable variable names


MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

You can reassign function


names

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

MATLAB will let you use built-in


function names as variables but
its a really bad idea
sin = 3 changes sin from a
function to a variable name
clear sin resets sin back to a
function
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Practice Exercise 2.2


Which of these names are allowed
in MATLAB?

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

test
Test
ifx
x
my-book
my_book
Thisisoneverylongnamebutisitstillallowed?x
x
1stgroup
group_one
zzaAbc
z34wAwy?12#
x x
sin
bad
log
idea

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

2.3.2 Matrices in MATLAB


The basic data type

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Matrix with one row and one column

100

200

Vector (One dimensional matrix)

300
400
500

Group of numbers arranged into


rows and columns
Single Value (Scalar)

100

200

300

400

500

One row or one column

Matrix (Two dimensional)


MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Scalar Calculations

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

You can use MATLAB like youd


use a calculator
Command
Prompt

100

200
300

>> 9 + 10
ans=19

400
500

100

200

300

400

500

Result
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Assignment Operator

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

To define a variable a we might


type
a=1+2
which should be read as:
a is assigned a value of 1+2

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

How is the assignment operator


different from an equality?

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

In algebra the equation


x=3+5
means that both sides are the same
In computers when we say
x=3+5
we are telling the machine to store the
value on the right hand side of the
equation in a memory location, and to
name that location x
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Is that really different?

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Yes!!!
In algebra this is not a true statement
x=x+1
In computers (assignment statements)
it means replace the value in the
memory location named x, with a new
value equal to x+1

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Order of Operation

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Same as youve learned in math


class
Same as your calculator
Parentheses first
Exponentiation
Multiplication / division
Addition / subtraction
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Order of Operation

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

5*(3+6)

= 45

5*3+6

= 21

100

White space does not matter!!!

200
300
400
500

100

200

300

400

500

5*3 + 6

= 21

Adding a space around + and signs makes the


expression more readable
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Parentheses

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

Use only ( )
{ } and [ ] mean something
different
MATLAB does not assume
operators

500

5 * (3+4) not 5(3+4)


MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Compute from left to right

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

5*6/6*5 = 25
5*6/(6*5)

= 1

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Heres an example
Find the surface area of a cylinder

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

r = radius
r=5

h = height
h = 10

r2

2 r * h

100

200

r2

300
400
500

100

200

300

400

500

SA 2r 2rh 2r (r h)
2

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Array Operations

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Using MATLAB as a glorified


calculator is OK, but its real
strength is in matrix manipulations

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

To create a row vector, enclose a list of values


in brackets
100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

You may use either a space or a


comma as a delimiter in a row
vector

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

Use a semicolon as a delimiter to create a


new row

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Use a semicolon as a delimiter to create a


new row

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Hint: Its easier to keep track of how many


values youve entered into a matrix, if you
enter each row on a separate line. The
semicolons are optional
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Shortcuts

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

While a complicated matrix might have


to be entered by hand, evenly spaced
matrices can be entered much more
readily. The command
b= 1:5
or the command
b = [1:5]
both return a row matrix
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

The default increment is 1, but if you


want to use a different increment put
it between the first and final values

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

To calculate spacing
between elements use

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

linspace
logspace

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

number of elements in the


array

Initial value in the


array

Final value in the


array

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

number of elements in the


array
Initial value in the
array expressed
as a power of 10

100

200
300
400
500

100

200

300

400

Final value in the


array expressed
as a power of 10

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

It is a common mistake to enter the


initial and final values into the
logspace command, instead of
entering the corresponding power of
10
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Hint

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

You can include mathematical


operations inside a matrix
definition statement.
For example

200
300
400
500

100

200

300

400

500

a = [0: pi/10: pi]


MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Mixed calculations between


scalars and arrays

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Matrices can be used in many


calculations with scalars
There is no confusion when we
perform addition and subtraction
Multiplication and division are a little
different
In matrix mathematics the
multiplication operator (*) has a very
specific meaning
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Addition between arrays is


performed on
corresponding elements
100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Multiplication between
arrays is performed on
corresponding elements if
the .* operator is used
100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

MATLAB interprets * to mean matrix


multiplication. The arrays a and b are
not the correct size for matrix
multiplication in this example

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Array Operations

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Array multiplication
Array division
Array exponentiation

.*
./
.^

100

200
300

In each case the size of the arrays must match

400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

The matrix capability of MATLAB makes it


easy to do repetitive calculations

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

For example, assume you have a


list of angles in degrees that you
would like to convert to radians.
First put the values into a matrix.
Perform the calculation

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Either the * or the .* operator can be


used for this problem, because it is
composed of scalars and a single
matrix
The value of pi is built into MATLAB as
a floating point number, called pi
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

More about pi

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Because pi is an irrational number, it


can not be expressed exactly with a
floating point representation
The MATLAB constant, pi, is really
an approximation.
If you find sin(pi) MATLAB returns a
very small number.

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Transpose

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

The transpose operator changes


rows to columns or vice versa.

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

The transpose operator


makes it easy to create
tables

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

table =[degrees;radians] would have given


the same result

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

The transpose
operator works on both
one dimensional and
two dimensional arrays

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Number Display

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Scientific Notation
Although you can enter any number in
decimal notation, it isnt always the best
way to represent very large or very small
numbers
In MATLAB, values in scientific notation
are designated with an e between the
decimal number and exponent. (Your
calculator probably uses similar notation.)
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

It is important to omit blanks between


the decimal number and the exponent.
For example, MATLAB will interpret
6.022 e23
as two values (6.022 and 1023 )

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Display Format

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Multiple display formats are available


No matter what display format you
choose, MATLAB uses double
precision floating point numbers in its
calculations
MATLAB handles both integers and
decimal numbers as floating point
numbers
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Default

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

The default format is called short


If an integer is entered it is
displayed without trailing zeros
If a floating point number is
entered four decimal digits are
displayed

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Other formats

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Changing the format affects all


subsequent displays
format long results in 14 decimal
digits
format bank results in 2 decimal
digits
format short returns the display to
the default 4 decimal digits
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Really Big and Really Small

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

When numbers become too large or


too small for MATLAB to display using
the default format, it automatically
expresses them in scientific notation
You can force scientific notation with
format short e
format long e

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Common Scale Factor

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

For long and short formats, a


common scale factor is applied to
the entire matrix if some of the
elements become very large, or
very small. This scale factor is
printed along with the scaled
values.
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200

Common Scale
Factor

300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Two other formats

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

format +
format rat

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Spacing in the command


window

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

format compact
format loose (default)

100

200
300
400
500

The format command also allows


us to control how tightly
information is spaced in the
command window

100

200

300

400

500

Notice that the value of A is still


being displayed using the rat
format, because we havent
changed it back to format short

Most of the examples in this


presentation use format compact
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Section 2.4
Saving Your Work

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300

If you save a MATLAB session


performed in the command
window, all that is saved are the
values of the variables you have
named

400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Variables are saved,


not the commands in
the command window

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Save either by using the file menu or...


100

Save with a command in the


command window

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

MATLAB automatically
saves to a .mat file

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

If you want to save to another


format, such as .dat, you need to
explicitly tell the program

100

200

save <file_name> <variable_list> -ascii

300
400
500

100

200

300

400

500

Again Remember that the only things being saved are the
values stored in the workspace window not the commands
from the command window
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Script M-files

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

If you want to save your work,


(the commands you entered)
you need to create an M-file
File->New->M-file
Type your commands in the edit
window that opens

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

The file can be saved into the


current folder/directory
It runs in the command window

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Save the file using


the save icon, or the
file menu

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

You can dock the editing


window with the
MATLAB desktop, by
using the docking arrow

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200

This arrangement is
often easier to use

300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5

I saved this file as


example.m

-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

Notice that it now appears


in the current directory

100

200
300
400
500

100

200

300

400

500

When I
execute the
file, the figure
appears on
top of the
MATLAB
desktop

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

The figure window


can also be docked
onto the MATLAB
desktop, using the
docking arrow

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Notice that the command history


window is hidden underneath the
figure, but can be accessed with
the tab

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Comments

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Be sure to comment your code


Add your name
Date
Section #
Assignment #
Descriptions of what you are doing
and why
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

The % sign identifies comments


You need one on each line

100

200
300
400
500

100

200

300

400

500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Cell Mode

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Enables the user to execute one


section of code at a time
Especially useful as you debug
your code
To use cell mode you must activate
the cell toolbar

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Cell Toolbar

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

Cell Dividers

200
300
400
500

100

200

300

400

500

Be sure to include a space


after the %%

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

Summary

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0

0.5

1.5

2.5

time, hour

3.5

4.5

100

200
300
400
500

100

200

300

400

500

Introduced the MATLAB Windows


Basic matrix definition
Save and retrieve MATLAB data
Create and use script M-files
The use of cell mode

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

You might also like