Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays...

84
Lecture Notes 2 Introduction To Matlab Introduction To Matlab Introduction to Computational Science , Fall 2010 Marcus Pendergrass Hampden-Sydney College

Transcript of Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays...

Page 1: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Introduction To MatlabIntroduction To MatlabIntroduction to Computational Science , Fall 2010

Marcus PendergrassHampden-Sydney College

Page 2: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Outline

1 Getting Started

2 Example Session

3 Arithmetic Operations

4 Variables And Arrays

5 Basic Plotting

6 Saving Your Work

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 2 / 74

Page 3: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Starting Matlab

I To start Matlab, double-click the “MATLAB R2010a” icon:

I This brings up the Matlab desktop...

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 3 / 74

Page 4: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

The Matlab Desktop

I The Matlab desktop has several useful components...

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 4 / 74

Page 5: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

The Matlab Desktop – Command Window

I You work with Matlab interactively in the Command Window

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 5 / 74

Page 6: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

The Matlab Desktop – Current Folder

I The Current Folder window shows the current directory

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 6 / 74

Page 7: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

The Matlab Desktop – Workspace

I The Workspace window shows the variables you’ve defined

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 7 / 74

Page 8: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

The Matlab Desktop – History

I The History window shows the commands you’ve entered so far

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 8 / 74

Page 9: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Outline

1 Getting Started

2 Example Session

3 Arithmetic Operations

4 Variables And Arrays

5 Basic Plotting

6 Saving Your Work

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 9 / 74

Page 10: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Define A Variable

I Let’s set a variable x equal to 3...

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 10 / 74

Page 11: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Define A Variable

I Pressing return tells Matlab to execute your command.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 11 / 74

Page 12: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Define A Variable

I Matlab echoes the result back to you in the command window.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 12 / 74

Page 13: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Manipulate A Variable

I Manipulate the variable using normal arithmetic operations...

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 13 / 74

Page 14: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Manipulate A Variable

I Matlab stores the result in the variable ans.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 14 / 74

Page 15: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Manipulate A Variable

I ans can be manipulated like any other variable...

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 15 / 74

Page 16: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Manipulate A Variable

I ans can be manipulated like any other variable...

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 16 / 74

Page 17: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Manipulate A Variable

I ...but it’s better to use your own variables to store results.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 17 / 74

Page 18: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Manipulate A Variable

I ...but it’s better to use your own variables to store results.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 18 / 74

Page 19: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Using The History Window

I Double-click on a command in the history window to re-executeit.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 19 / 74

Page 20: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Using The History Window

I Double-click on a command in the history window to re-executeit.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 20 / 74

Page 21: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Clearing The Command Window

I The command clc clears the command window.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 21 / 74

Page 22: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Clearing The Command Window

I clc doesn’t clear the variables themselves, only the display.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 22 / 74

Page 23: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Predefined Constants

I Matlab knows the value of π. It is stored in the pre-definedvariable pi.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 23 / 74

Page 24: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Suppressing Output

I You can suppress Matlab’s command window output byending a command with a semicolon (;).

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 24 / 74

Page 25: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Suppressing Output

I You can suppress Matlab’s command window output byending a command with a semicolon (;).

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 25 / 74

Page 26: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Built-In Functions

I Most common mathematical functions are built into Matlab.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 26 / 74

Page 27: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Built-In Functions

I Most common mathematical functions are built into Matlab.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 27 / 74

Page 28: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

String Variables For Text

I Text can be stored in string variables.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 28 / 74

Page 29: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

String Variables For Text

I You can do lots of manipulations with string variables as well.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 29 / 74

Page 30: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

String Variables For Text

I You can do lots of manipulations with string variables as well.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 30 / 74

Page 31: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

The Help Browser

I The help browser is your friend. Access it from the Help menu(Help → Product Help).

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 31 / 74

Page 32: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

The Help Browser

I You can either browse from the contents pane...

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 32 / 74

Page 33: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

The Help Browser

I ...or search by typing in the search box.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 33 / 74

Page 34: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

The Help Browser

I ...or search by typing in the search box.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 34 / 74

Page 35: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Practice

Practice 2.1

Let x = 3 and y = 5. Perform the following calculations inMatlab’s command window:

1. z = 3x− 4y + 2

2. w = y3x+z

3. α =√100 + z

Practice 2.2

Look up the sprintf command using Matlab’s help browser. Useit to display the string ’My value of alpha is xxxx.’, wherexxxx is replaced by the value of α you calculated in the last problem.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 35 / 74

Page 36: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Outline

1 Getting Started

2 Example Session

3 Arithmetic Operations

4 Variables And Arrays

5 Basic Plotting

6 Saving Your Work

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 36 / 74

Page 37: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Arithmetic Operations

Basic Arithmetic Operators

+ addition

- subtraction

* multiplication

/ division

^ exponentiation

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 37 / 74

Page 38: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Order Of Operations

Order Of Operations

1. Calculations done from left to right

2. Parenthesized items evaluated firstI Nested parentheses evaluated from innermost to outermost

3. function calls evaluated next (e.g. sqrt, cos, etc...)

4. exponents and radicals evaluated next

5. unary negations evaluated next

6. multiplications and divisions evaluated next

7. addition and subtraction evaluated last

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 38 / 74

Page 39: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Example

Example: Order Of Operations

4+24/2^3+6*(5+cos(pi)) = 4+24/2^3+6*(5+-1)

= 4+24/2^3+6*(5-1)

= 4+24/2^3+6*4

= 4+24/8+6*4

= 4+3+6*4

= 4+3+24

= 7+24

= 31

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 39 / 74

Page 40: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Practice

Practice 2.3

Evaluate the following expressions by hand using the correct order ofoperations:

1. 8-6-3/2^2+1-5

2. 4^1/2^3

3. 2/3+2^sin(0)

Practice 2.4

Give the Matlab commands to evaluate the following expressions.

1. x1+x2

2. x+11+x2

3. 2(2x)

4. 4 cos(2πx)

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 40 / 74

Page 41: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Outline

1 Getting Started

2 Example Session

3 Arithmetic Operations

4 Variables And Arrays

5 Basic Plotting

6 Saving Your Work

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 41 / 74

Page 42: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Variables And Variable Names

I To create a variable, just assign a value to a name:

>> a = 3;

>> favMovie1982 = ’Blade Runner’;

I Rules for variable names:I first character must be a letterI other characters must be letters, numbers, or underscores (_).I variable names are case sensitive: myVar is different from myvar.

I Built-in variables (don’t assign to these names):I pi stands for π = 3.1415926....I ans stores the last unassigned value.I Inf stands for positive infinity.I NaN stands for “not a number” (e.g. 0/0).

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 42 / 74

Page 43: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Vectors, Matrices, and Arrays

I A array is a variable that can hold multiple values at the sametime.

I The most common type of array is a matrix, which is atwo-dimensional rectangular array.

I A vector is a one-dimensional rectangular array.

I There are also multidimensional arrays that have more than twodimensions.

I Sometimes we refer to plain everyday numbers as scalars.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 43 / 74

Page 44: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Creating Vectors

I Create a vector by typing entries within square brackets:

>> myVec = [-1 3 pi 2.7183]

myVec =

-1.0000 3.0000 3.1416 2.7183

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 44 / 74

Page 45: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Creating Vectors

I There’s a special syntax to create a vector of evenly-spacedentries:

>> myArray = [0 : 2 : 10]

myArray =

0 2 4 6 8 10

I The general syntax for this is

[startValue : skipValue : endValue]

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 45 / 74

Page 46: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Referencing Vector Entries

I Reference individual entries in a vector by their position:

>> myArray(1)

ans =

0

>> myArray(3)

ans =

4

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 46 / 74

Page 47: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Creating Matrices

I Similarly you can create a matrix by typing in the entriesindividually. Use a semicolon (;) for the row separator.

>> myMatrix = [1 2 3; 4 5 6]

myMatrix =

1 2 3

4 5 6

I This is a 2× 3 matrix: 2 rows, 3 columns.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 47 / 74

Page 48: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Referencing Matrix Entries

I Again, you reference individual entries in a matrix by theirposition, first the row number, then the column number:

>> myMatrix(1,3)

ans =

3

>> myMatrix(2,1)

ans =

4

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 48 / 74

Page 49: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Matrix Creation Commands

I You can create a matrix of all zeros using the zeros command.For example, to create a 2× 4 zero matrix, use

>> M = zeros(2,4)

M =

0 0 0 0

0 0 0 0

I The first argument is the number of rows, the second is thenumber of columns.

I There is a similar command called ones.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 49 / 74

Page 50: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Creating Random Matrices

I The rand command creates a matrix whose entries are randomnumbers uniformly distributed on [0, 1]:

>> rM = rand(3,2)

rM =

0.8147 0.9134

0.9058 0.6324

0.1270 0.0975

I There is a similar command called randn. It creates matriceswhose entries have the standard normal distribution.

I There’s another command called randi. It creates matriceswhose entries are random integers.

I For more info consult the Help Browser.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 50 / 74

Page 51: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Row and Column Vectors

I Notice that a vector is just a matrix with only one row; i.e. a1× n matrix. Sometimes we call these row vectors.

I A n× 1 matrix (i.e. just one column) is called a column vector.

>> colVec = randn(3,1)

colVec =

-0.4336

0.3426

3.5784

I note: Matlab distinguishes between row and column vectors!

I note: The transpose operator (.’) converts row vectors tocolumn vectors and vice versa.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 51 / 74

Page 52: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Operations On Arrays

+, -, *, /, and ^ are defined for array variables, but only in certaincases:

I A+B and A-B are defined so along as A and B are the same size(same number of rows and columns). In this case the sum ordifference is computed entrywise:(

1 2

3 4

)+

(5 6

7 8

)=

(6 8

10 12

)

I The definitions of A*B, A/B, and A^n come from themathematical field of Linear Algebra. We do not need them atpresent.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 52 / 74

Page 53: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Operations On Arrays

+, -, *, /, and ^ are defined for array variables, but only in certaincases:

I A+B and A-B are defined so along as A and B are the same size(same number of rows and columns). In this case the sum ordifference is computed entrywise:(

1 2

3 4

)+

(5 6

7 8

)=

(6 8

10 12

)

I The definitions of A*B, A/B, and A^n come from themathematical field of Linear Algebra. We do not need them atpresent.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 52 / 74

Page 54: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Operations On Arrays

+, -, *, /, and ^ are defined for array variables, but only in certaincases:

I A+B and A-B are defined so along as A and B are the same size(same number of rows and columns). In this case the sum ordifference is computed entrywise:(

1 2

3 4

)+

(5 6

7 8

)=

(6 8

10 12

)

I The definitions of A*B, A/B, and A^n come from themathematical field of Linear Algebra. We do not need them atpresent.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 52 / 74

Page 55: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Vectorized Operations On Arrays

Matlab does define an entrywise product (.*), entrywise quotient(./), and entrywise exponentiation (.^).

I A.*B and A./B compute the entrywise product and quotient ofA and B. A and B must be of the same size.(

1 2

3 4

).*

(5 6

7 8

)=

(5 12

21 32

)

I A.^n raises each entry of A to the nth power:(1 2

3 4

).^3 =

(1 8

27 64

)

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 53 / 74

Page 56: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Vectorized Operations On Arrays

Matlab does define an entrywise product (.*), entrywise quotient(./), and entrywise exponentiation (.^).

I A.*B and A./B compute the entrywise product and quotient ofA and B. A and B must be of the same size.(

1 2

3 4

).*

(5 6

7 8

)=

(5 12

21 32

)

I A.^n raises each entry of A to the nth power:(1 2

3 4

).^3 =

(1 8

27 64

)

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 53 / 74

Page 57: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Vectorized Operations On Arrays

Matlab does define an entrywise product (.*), entrywise quotient(./), and entrywise exponentiation (.^).

I A.*B and A./B compute the entrywise product and quotient ofA and B. A and B must be of the same size.(

1 2

3 4

).*

(5 6

7 8

)=

(5 12

21 32

)

I A.^n raises each entry of A to the nth power:(1 2

3 4

).^3 =

(1 8

27 64

)

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 53 / 74

Page 58: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Vectorized Operations On Arrays

Note: With the exception of exponentiation, these vectorizedoperations require the variables to be the same size. There is oneexception to this: when one of the variables is a scalar (i.e. ordinarynumber)

I 3 + A adds three to every entry of A. Similarly A - 3 subtractsthree from every entry of A.

I 4 * A multiplies every entry of A by four. Similarly A / 4

divides every entry of A by four.

I 2 .^ A computes powers of two using the entries of A.

>> 2 .^ [1 2 3 4]

ans =

2.0000 4.0000 8.0000 16.0000

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 54 / 74

Page 59: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Vectorized Operations On Arrays

Note: With the exception of exponentiation, these vectorizedoperations require the variables to be the same size. There is oneexception to this: when one of the variables is a scalar (i.e. ordinarynumber)

I 3 + A adds three to every entry of A. Similarly A - 3 subtractsthree from every entry of A.

I 4 * A multiplies every entry of A by four. Similarly A / 4

divides every entry of A by four.

I 2 .^ A computes powers of two using the entries of A.

>> 2 .^ [1 2 3 4]

ans =

2.0000 4.0000 8.0000 16.0000

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 54 / 74

Page 60: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Vectorized Operations On Arrays

Note: With the exception of exponentiation, these vectorizedoperations require the variables to be the same size. There is oneexception to this: when one of the variables is a scalar (i.e. ordinarynumber)

I 3 + A adds three to every entry of A. Similarly A - 3 subtractsthree from every entry of A.

I 4 * A multiplies every entry of A by four. Similarly A / 4

divides every entry of A by four.

I 2 .^ A computes powers of two using the entries of A.

>> 2 .^ [1 2 3 4]

ans =

2.0000 4.0000 8.0000 16.0000

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 54 / 74

Page 61: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Vectorized Operations On Arrays

Note: With the exception of exponentiation, these vectorizedoperations require the variables to be the same size. There is oneexception to this: when one of the variables is a scalar (i.e. ordinarynumber)

I 3 + A adds three to every entry of A. Similarly A - 3 subtractsthree from every entry of A.

I 4 * A multiplies every entry of A by four. Similarly A / 4

divides every entry of A by four.

I 2 .^ A computes powers of two using the entries of A.

>> 2 .^ [1 2 3 4]

ans =

2.0000 4.0000 8.0000 16.0000

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 54 / 74

Page 62: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Built-In Functions Are Vectorized

Matlab’s built-in functions are vectorized. If you give them an arrayas input, the function will be applied to each entry in the array.

>> v = [1 4 9 16];

>> sqrt(v)

ans =

1.0000 2.0000 3.0000 4.0000

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 55 / 74

Page 63: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Example

Vectorized Operations

Compute 11+x2 , where x = [-3 : 3].

Solution

>> x = [-3 : 3]

x =

-3.0000 -2.0000 -1.0000 0.0000 1.0000 2.0000 3.0000

>> y = 1 ./ (1 + x.^2)

y =

0.1000 0.2000 0.5000 1.0000 0.5000 0.2000 0.1000

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 56 / 74

Page 64: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Example

Vectorized Operations

Compute 11+x2 , where x = [-3 : 3].

Solution

>> x = [-3 : 3]

x =

-3.0000 -2.0000 -1.0000 0.0000 1.0000 2.0000 3.0000

>> y = 1 ./ (1 + x.^2)

y =

0.1000 0.2000 0.5000 1.0000 0.5000 0.2000 0.1000

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 56 / 74

Page 65: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Practice

Practice 2.5

What do the following Matlab commands do?

1. [1 : 99]

2. var1 = [0 : 5 : 200];

3. var2 = [10 : -1 : 1]

Practice 2.6

Use the Help Browser to look up the randi command. Use randi tocreate a 3× 5 array of random integers, with all entries between 1and 100. What is the position (row number, column number) of themaximum entry in your array?

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 57 / 74

Page 66: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Practice

Practice 2.7

Write a Matlab command to

1. make a row vector containing the odd numbers from 1 to 37.

2. make a 3× 4 matrix containing all 1’s.

3. make a column vector containing five random integers, eachbetween 1 and 6.

Practice 2.8

In three lines of Matlab code, create two random 3× 3 arrays, andcompute the entrywise product of them. Use sprintf to display the(3,2) entry to the command window.

Practice 2.9

In a single line of Matlab code compute the cube roots of the evenintegers between 2 and 20 inclusive.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 58 / 74

Page 67: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Outline

1 Getting Started

2 Example Session

3 Arithmetic Operations

4 Variables And Arrays

5 Basic Plotting

6 Saving Your Work

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 59 / 74

Page 68: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Bar Plots Of Vectors

I The function bar makes a bar plot of a vector.

>> v = randn(1,5);

>> bar(v);

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 60 / 74

Page 69: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Stem Plots Of Vectors

I The function stem makes a “stem plot” of a vector.

>> v = randn(1,5);

>> stem(v);

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 61 / 74

Page 70: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Function Plotting

I To plot a function, put the x-values in one array, the y-values inanother array, and use the plot command.

>> xVals = [0 : 0.1 : 4*pi];

>> yVals = sin(xVals);

>> plot(xVals,yVals);

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 62 / 74

Page 71: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Function Plotting

I Use a “format string” to change the color of the plot. Let’s redothe previous plot in red.

>> xVals = [0 : 0.1 : 4*pi];

>> yVals = sin(xVals);

>> plot(xVals,yVals, ’r’);

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 63 / 74

Page 72: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Function Plotting

I There is a host of commands for adding titles, axis labels,legends, and other decorations to a graph. Here’s a small sampleof the possibilities:

>> xVals = [0 : 0.1 : 4*pi];

>> yVals = sin(xVals);

>> plot(xVals,yVals);

>> xlim([0,4*pi]);

>> ylim([-1.1,1.1]);

>> xlabel(’seconds’);

>> ylabel(’flow (liters/min)’);

>> title(’Flow Rate vs. Time’);

I This gives us...

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 64 / 74

Page 73: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Function Plotting

I You can also use the controls on the figure’s menu and toolbarto edit the graph.

I Play around with these commands, and explore the possibilities...

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 65 / 74

Page 74: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Example

Vectorizing In Plots

Plot y = sin(x)/x for x ∈ [−20, 20].

Solution

>> x = [-20 : 0.1 : 20];>> y = sin(x) ./ x; % note vectorized arithmetic

>> plot(x,y)

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 66 / 74

Page 75: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Example

Vectorizing In Plots

Plot y = sin(x)/x for x ∈ [−20, 20].

Solution

>> x = [-20 : 0.1 : 20];>> y = sin(x) ./ x; % note vectorized arithmetic

>> plot(x,y)

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 66 / 74

Page 76: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Example

Vectorizing In Plots

Plot y = sin(x)/x for x ∈ [−20, 20].

Solution

>> x = [-20 : 0.1 : 20];>> y = sin(x) ./ x; % note vectorized arithmetic

>> plot(x,y)

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 66 / 74

Page 77: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Practice

Practice 2.10

Use the Help Browser to look up the randi command. Use randi tocreate a 1× 20 column vector of random integers, with all entriesbetween 1 and 10. Make a stem plot of this vector.

Practice 2.11

Plot y = ex for x between −1 and 1. Label your axes, and provide atitle for your plot.

Practice 2.12

Plot y = 1−cos(x)x2 for x between −30 and 30. Label your axes, and

provide a title for your plot.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 67 / 74

Page 78: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Outline

1 Getting Started

2 Example Session

3 Arithmetic Operations

4 Variables And Arrays

5 Basic Plotting

6 Saving Your Work

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 68 / 74

Page 79: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

The save And load Commands

Use the save command to save workspace variables to the currentdirectory:

I Save all workspace variables to the file myFile.mat in thecurrent directory:

>> save myFile;

note: Matlab automatically adds the .mat extension.

I Save only the variables x, y, and z to myFile.mat:

>> save myFile x y z;

The load command loads these variables to the workspace:

>> load myFile;

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 69 / 74

Page 80: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Saving Matlab Figures

Saving figures as .fig files enables you to reload them later withouthaving to regenerate all the data and decorations.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 70 / 74

Page 81: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Saving Matlab Figures

Saving figures as .fig files enables you to reload them later withouthaving to regenerate all the data and decorations.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 71 / 74

Page 82: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Saving Matlab Figures

Saving figures as .fig files enables you to reload them later withouthaving to regenerate all the data and decorations.

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 72 / 74

Page 83: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Saving Matlab Figures

Saving figures as .fig files enables you to reload them later withouthaving to regenerate all the data and decorations.

You can also save figures in standard graphics formats, like .png,.jpg, etc. (.png is recommended.)

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 73 / 74

Page 84: Lecture Notes 2 - Hampden-Sydney CollegeExample Session Arithmetic Operations Variables And Arrays Basic Plotting Saving Your Work Practice Practice 2.3 Evaluate the following expressions

Lecture Notes 2

Dr. Pendergrass

Getting Started

Example Session

ArithmeticOperations

Variables AndArrays

Basic Plotting

Saving YourWork

Practice

Practice 2.13

Create variables x = 3, y = [10 : -1 : 1], andz = ’robertFrost’. Save these to a file named foo.mat in thecurrent folder. Look up the help on the clear command, and use itto clear all your workspace variables. Now if you double-click the filefoo.mat from Matlab’s current folder window, what happens?

Practice 2.14

Make an accurate plot of y = tan(x)/x for x ∈ [−2π, 2π]. Save yourplot to a figure file named tan.fig. Kill the existing plot window,then double click on tan.fig in the current folder window. Whathappens?

Dr. Pendergrass (H-SC) Lecture Notes 2 Fall 2010 74 / 74