Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views125 pages

Ch02 Environment 3rd Ed

The document provides an overview of MATLAB, focusing on its environment and functionalities for engineers. It outlines objectives for learning MATLAB, including starting the program, using matrices, and understanding different types of calculations. Additionally, it describes various windows in MATLAB, such as the Command Window and Command History, which facilitate user interaction and command tracking.

Uploaded by

Beyza Erdem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views125 pages

Ch02 Environment 3rd Ed

The document provides an overview of MATLAB, focusing on its environment and functionalities for engineers. It outlines objectives for learning MATLAB, including starting the program, using matrices, and understanding different types of calculations. Additionally, it describes various windows in MATLAB, such as the Command Window and Command History, which facilitate user interaction and command tracking.

Uploaded by

Beyza Erdem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 125

MATLAB Environment

-5
Rate of Change

Chapter 2
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 inform ation regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Objectives
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

After studying this chapter you should be able to


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• Start the MATLAB program and solve simple problems


in the command window
• Understand MATLAB’s use of matrices
• Identify and use the various MATLAB windows
• Define and use simple matrices
100

200

300
• Name and use variables
• Understand the order of operations in MATLAB
400

500
100 200 300 400 500

• 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.
-5
Rate of Change Objectives - continued
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

After studying this chapter you should be able to


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• Express numbers in either floating-point or scientific


notation
• Adjust the format used to display numbers in the
command window
100
• Save the value of variables used in a MATLAB session
• Save a series of commands in an M-file
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.
-5
Rate of Change In this chapter we’ll…
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Get started with MATLAB


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• Explore the MATLAB windows


• Solve some problems using
100

200
MATLAB
• Learn how to save our work
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.
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

• In Windows or Apple operating systems


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

click on the desktop icon


• In Unix type
MATLAB
100 At the shell prompt
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• MATLAB uses a
standard
100
windows menu
200
bar
300

400

500
• To exit MATLAB
100 200 300 400 500

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

• MATLAB uses several different


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

windows to display data,


commands and results.
100

200
• They are not necessarily all open
300

400
at once
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
Workspace Window
-0.5

0
0 0.5 1 1.5 2 2.5
time, hour
3 3.5 4 4.5 5 Workspace Window
Current Folder
Current Folder
Window
Window Command WindowWindow
Lists files stored in the Command
Listscurrent
files stored in the
directory Enter commands at the prompt
Enter commands at the prompt
100 current directory
200

300

400 MATLAB
MATLABWindows
Windows
500
100 200 300 400 500

Command
History Window
Records all commands
issued in the command
MATLAB for Engineers 3E, by Holly Moore. © 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

window
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information – including
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
regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
mistakes
Let’s 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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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.
-5
Rate of Change Command Window
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Similar to a scratch pad


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• Once you hit enter, you can’t edit any


commands
100
• You can retype them or use the arrow
Command Window
200

300
keys to retrieve commands and edit
400

500
them before hitting enter again
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.
-5
Rate of Change Command History
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Records the commands you issue


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

in the command window


• When you exit the command
100
window, or when you issue the clc
200
Command History
300

400
command, the command window
500
100 200 300 400 500
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.
-5
Rate of Change Command History
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

• You can transfer commands from


-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

the command history to the


command window
– Double click on a command
• It executes immediately
100

200

– Click and drag into the command


300

400

500
100 200 300 400 500
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.
-5
Rate of Change Workspace Window
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

Workspace Window
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

When you define variables


in the command window,
100

200

300
they are listed in the
400

500
100 200 300 400 500
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Scalar

100

200

300 Vector
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.
-5
Rate of Change Current Directory/Folder
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• The current folder window is a list


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

of files
• When you try to load information
100
from a file or try to save
information – MATLAB uses the
200

300

400

500
100 200 300 400 500
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.
-5
Rate of Change Document Window
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• If you double click on any variable


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

in the workspace window


MATLAB launches a document
100
window containing the array
editor
200

300

400

500
100 200 300 400 500
• 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.
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

New Variable
Icon

100

200
The Document
300
Window displays
the variable editor
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.
-5
Rate of Change Figure Window
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• When Figures are created a new


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

window opens
• It’s extremely easy to create
100

200
graphs in MATLAB
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

First create a vector of x


values – then a
corresponding vector of
100
y values
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Matlab makes it easy to modify


graphs by adding
•Titles
•Axis labels
•Legends
100

200

300
•Other types of annotations
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.
-5
Rate of Change Editing Window
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• This window allows you to type


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

and save a series of commands


without executing them
100

200
• There are several ways to open
300

400
an editing window
500
100 200 300 400 500
– 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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Open an editing window


100

200
from the file menu or with
300
the new file icon
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5
time, hour
3 3.5 4 4.5 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
-5
Rate of Change

Solving Problems with MATLAB


Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• We’ve already solved some


simple problems
100

200
• We need to understand how
300

400
MATLAB works to solve more
500
100 200 300 400 500

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.
-5
Rate of Change Variables
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• MATLAB allows you to assign a


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

value to a variable
• A=3
100

200
• Should be read as A is assigned a
300

400
value of 3
• Use the variables in subsequent
500
100 200 300 400 500

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.
-5
Rate of Change Naming Variables
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• All names must start with a letter


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• They may contain letters,


numbers and the underscore ( _ )
100

200
• Names are case sensitive
• There are certain keywords you
300

400

can’t use
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.
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

-2

-1.5

-1
iskeyword
-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

ans =
'break' 'global'
'case' 'if'
'catch' 'otherwise'
'classdef' 'parfor'
100
'continue' 'persistent'
200
'else' 'return‘
300

'elseif' ‘spmd’
400

'end‘ 'switch'
500
100 200 300 400 500

'for‘ 'try'
'function' '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

• MATLAB will let you use built-in


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

function names as variables – but


it’s a really bad idea
100

200
• sin = 3 changes sin from a
300

400
function to a variable
500
100 200 300 400 500
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
-5
Rate of Change

in MATLAB?
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• test
• Test
• if
x
• x
my-book
100
• my_book
200

• x
Thisisoneverylongnamebutisitstillallowed?

300

400
1stgroup
x
500
100 200 300 400 500
• 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

• Group of numbers arranged into


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

rows and columns


• Single Value (Scalar)
100

200
– Matrix with one row and one column
• Vector (One dimensional matrix)
300

400

– One row or one column


500
100 200 300 400 500

• 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.
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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.
-5
Rate of Change Scalar Calculations
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• You can use MATLAB like you’d


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

use a calculator

100 Command
200
Prompt
300

400

500
100 200 300 400 500
>> 9 + 10
ans=19
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.
Arithmetic operations
between two scalars
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

Operation Algebraic syntax Matlab syntax


0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Addition a+b a+b


Subtraction a-b a-b
Multiplication a×b a*b
100

200
Division a/b
300

400
Exponentiation a^b
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.
-5
Rate of Change Assignment Operator
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• To define a variable a we might


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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

300

400

• A single equals sign ( = ) is called


500
100 200 300 400 500

an assignment operator 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.
How is the assignment operator
-5
Rate of Change
different from an equality?
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

• In algebra the equation


-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

x=3+5
means that both sides are the same
• In computers when we say
100
x=3+5
200

300
we are telling the machine to store the
400
value on the right hand side of the
500
100 200 300 400 500
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.
-5
Rate of Change Is that really different?
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Yes!!!
-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• In algebra this is not a true statement


x=x+1
100
• In computers (assignment statements)
200

300
it means replace the value in the
400

500
memory location named x, with a new
value equal to x+1
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.
-5
Rate of Change Example for assignment
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

result of the calculation on the right hand side


stored in a computer memory location named x.
Since x is assigned a value of 8, if you enter the
variable name in to MATLAB, you get following
result:

100

200

300

400 Tells the computer to replace the current


500
value of x stored in memory with a new value
100 200 300 400 500
that is equal to old x plus 1. since the value
stored in x was originally 8 , the statement
returns 9.

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.
-5
Rate of Change Order of Operation
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Same as you’ve learned in math


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

class
• Same as your calculator
100

200
– Parentheses first
300

400
– Exponentiation
– Multiplication / division
500
100 200 300 400 500

– 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.
-5
Rate of Change Order of Operation
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

5*(3+6) = 45

5*3+6 = 21
100

200
White space does not matter!!!
300

400

500

5*3 + 6 = 21
100 200 300 400 500

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.
-5
Rate of Change Parentheses
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Use only ( )
-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• { } and [ ] mean something


different
100

200
• MATLAB does not assume
300

400
operators
500
100 200 300 400 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.
-5
Rate of Change Compute from left to right
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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.
Here’s 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
r = radius h = height
-0.5

0
0 0.5 1 1.5 2 2.5
time, hour
3 3.5 4 4.5 5
r=5 h = 10

π r2
2π r * h
100

π r2
200

300

400

500
100 200 300 400 500

SA  2r  2rh  2r (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.
MATLAB first performs the exponentiation,
Rate of Change
-5

raising the radius to second power, it then works


Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3
from left to right, calculating the first product and
-2.5

-2

-1.5
then the second product, finally it adds the two
-1

-0.5 products together.


0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

in this case, MATLAB first finds the sum of


the radius and height and then performs
the multiplications, working from left to
right.
100

200 If you forget to include the parentheses,


300 program first calculates the product of 2*pi*r*r
400 and adds height, which is wrong answer.
500
100 200 300 400 500

If you omit multiplication operator, MATLAB


produces error message because it does not
assume any operator

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.
-5
Rate of Change exp function
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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.
-5
Rate of Change example
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200
Hint: break the equation into several pieces to minimize making error.
300
poly = a*x^2 + b*x + c;
denom = 4*pi*x^2+cos(x – 2)*poly;
400

Combine these components into a final equation


f = (log(poly)-sin(poly))/denom
500
100 200 300 400 500

The result is
f=
0.0044

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.
EXAMPLE 2.1 SCALAR 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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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.
-5
Rate of Change Array Operations
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Using MATLAB as a glorified


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

You may use either a space or a


comma as a “delimiter” in a row
100
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300 Use a semicolon as a delimiter to create a


400
new row
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300 Hint: It’s easier to keep track of how many


400
values you’ve entered into a matrix, if you
500
100 200 300 400 500
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.
-5
Rate of Change Shortcuts
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• While a complicated matrix might have


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

to be entered by hand, evenly spaced


matrices can be entered much more
readily. The command
100

200 b= 1:5
300

400

500
or the command
100 200 300 400 500

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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

• linspace (evenly space)


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• logspace (logarithmically space)

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

number of elements in
the array
Initial value in the
array Final value in the
100

array
200

300

400

500
100 200 300 400 500 Returns a vector with
three values, evenly
spaced between 1 and 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.
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

number of elements in
the array
100

200
Initial value in the Final value in the
300

array expressed array expressed


400

as a power of 10 as a power of 10
500
100 200 300 400 500

Returns a
logarithmically
spaced vector

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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.
-5
Rate of Change Hint
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• You can include mathematical


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

operations inside a matrix


definition statement.
100

200
• For example
300

400

500

a = [0: pi/10: pi]


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.
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

• Matrices can be used in many


-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

calculations with scalars


• There is no confusion when we
perform addition and subtraction
100

200
• Multiplication and division are a little
300
different
• In matrix mathematics the
400

500
100 200 300 400 500

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Addition between arrays


(vectors) is performed on
100 corresponding elements
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Multiplication between
arrays is performed on
corresponding elements if
100
the .* operator is used
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

MATLAB interprets * to mean matrix


multiplication. The arrays a and b are
100
not the correct size for matrix
200
multiplication in this example
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.
-5
Rate of Change Array Operations
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Array (dot) multiplication


-1

-0.5

0
0 0.5 1 1.5 2 2.5
time, hour
3 3.5 4 4.5 5

.*
• Array (dot) division ./
• Array (dot) exponentiation .^
100

200

300

400 In each case the size of the arrays must match


500
100 200 300 400 500

These operators indicate element-by-element


multiplication, division or exponentiation

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
-5
Rate of Change

easy to do repetitive calculations


Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• For example, assume you have a


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

list of angles in degrees that you


would like to convert to radians.
100 – First put the values into a matrix.
– Perform the calculation
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100
Either the * or the .* operator can be
used for this problem, because it is
200

300

400
composed of scalars and a single
500
100 200 300 400 500
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.
-5
Rate of Change More about pi
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Because pi is an irrational number, it


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

can not be expressed exactly with a


floating point representation
100
• The MATLAB constant, pi, is really
200 an approximation.
300

400

500
• If you find sin(pi) MATLAB returns a
very small number.
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.
-5
Rate of Change Transpose
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• The transpose operator changes


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300
The transpose operator
makes it easy to create
400

500
100 200 300 400 500

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

The transpose
100
operator works on
200
both one dimensional
300 and two dimensional
400
arrays
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.
EXAMPLE 2.2 MATRIX CALCULATIONS WITH SCALARS

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

Common
time, hour

Scale Factor

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.
EXAMPLE 2.3 CALCULATING DRAG

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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.
-5
Rate of Change Number Display
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Scientific Notation
-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

– Although you can enter any number in


decimal notation, it isn’t always the best
way to represent very large or very small
100 numbers
– In MATLAB, values in scientific notation
200

300

400

500
are designated with an e between the
100 200 300 400 500

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100
It is important to omit blanks between
200
the decimal number and the exponent.
300 For example, MATLAB will interpret
400
6.022 e23
500
100 200 300 400 500
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.
-5
Rate of Change Display Format
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Multiple display formats are available


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• No matter what display format you


choose, MATLAB uses double
100
precision floating point numbers in its
200 calculations
300

400

500
• MATLAB handles both integers and
decimal numbers as floating point
100 200 300 400 500

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.
-5
Rate of Change Default
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• The default format is called short


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

• If an integer is entered it is
displayed without trailing zeros
100

200
• If a floating point number is
300

400
entered four decimal digits are
displayed
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.
-5
Rate of Change Other formats
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Changing the format affects all


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

subsequent displays
– format long results in 14 decimal
100
digits
– format bank results in 2 decimal
200

300

digits
400

500
100 200 300 400 500

– 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.
-5
Rate of Change
Really Big and Really Small
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• When numbers become too large or


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

too small for MATLAB to display using


the default format, it automatically
expresses them in scientific notation
• You can force scientific notation with
100

200

– format short e
300

400

– format long e
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.
-5
Rate of Change Common Scale Factor
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• For long and short formats, a


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

common scale factor is applied to


the entire matrix if some of the
100
elements become very large, or
very small. This scale factor is
200

300

printed along with the scaled


400

500
100 200 300 400 500

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

We used .^ operator because we


intended that each value in the
matrix V be squared, not that the
entire matrix V be multiplied by itself
100

200

300
Common Scale
Factor
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.
-5
Rate of Change Two other formats
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• format +
-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

– Allows us to view a matrix in terms of its signs


– if a value is positive plus sign will be displayed, if a
value is negative minus sign will be displayed if a
100
value is zero, nothing will be displayed
200

300
• format rat
– Displays numbers as rational numbers (i.e., as
400

500
100 200 300 400 500

fractions)

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

• The format command also allows


-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

us to control how tightly


information is spaced in the
command window
– format compact
100

200

– format loose – (default)


300

400

Notice that the value of A is still


• Most of the examples in this
500
100 200 300 400 500

being displayed using the rat


format, because we haven’t
presentation use format compact changed it back to format short

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

• If you save a MATLAB session


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

performed in the command


window, all that is saved are the
100
values of the variables you have
named
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100
Variables are saved,
200

300 not the commands in


400

500
the command window
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Save either by using the file


menu or...
100

200

300

400

500
100 200 300 400 500
Save with a command in 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.
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

• If you want to save to another


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

format, such as .dat, you need to


explicitly tell the program
100

200

300 save <file_name> <variable_list> -ascii


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.
-5
Rate of Change Script M-files
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• If you want to save your work,


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

(the commands you entered)


you need to create an M-file
100

200
• File->New->M-file
300

400
• Type your commands in the edit
window that opens
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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

• The file can be saved into the


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Save the file using


the save icon, or the
100

file menu
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

This arrangement is
100
often easier to use
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
I saved this file as
example.m
time, hour

Notice that it now appears


in the current directory

100
When I
200
execute the
300

400
file, the
500
100 200 300 400 500
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

The figure window


can also be docked
onto the MATLAB
MATLAB for Engineers 3E, by Holly Moore. © 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
desktop, using the
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:

docking arrow 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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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.
-5
Rate of Change Comments
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

• Be sure to comment your code


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

– Add your name


– Date
100
– Section #
200

300

400
– Assignment #
500
100 200 300 400 500
– 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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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.
EXAMPLE 2.4 CREATING AN M-FILE TO CALCULATE THE ACCELERATION
OF A SPACECRAFT
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

M-file code
300

400

500
100 200 300 400 500

Results are
reported in 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.
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 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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.
-5
Rate of Change Cell Mode
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

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

• Enables the user to execute


time, hour

Cell
menu
one
section of code at a time
• Especially useful as you debug
100
your code
•To use cell mode you must activate
200

300

the cell toolbar


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.
-5
Rate of Change Cell Toolbar
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200
Cell Dividers
300

400 Be sure to include a space


500
100 200 300 400 500 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.
-5
Rate of Change Summary
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5


-1

-0.5

0
0 0.5 1 1.5 2 2.5
time, hour
3 3.5 4 4.5 5

Introduced the MATLAB Windows


• Basic matrix definition
• Save and retrieve MATLAB data

100

200 Create and use script M-files


300

400

500
100 200 300 400 500
• 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