Top Banner
06/15/22 1 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161
31

10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

Jan 13, 2016

Download

Documents

Rafe Stevenson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 1

Chapter 2 Review: MATLAB Environment

Introduction to MATLAB 7Engineering 161

Page 2: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 2

Review Topics

MATLAB variables Working with Matrices Scalar Operations Precedence of Arithmetic Operations Array Operations Saving your work Script M-files

Page 3: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

MATLAB Windows I Command Window- similar to a scratch pad Command History Window- records all commands entered

for future reference Workspace Window- keeps track of your variables and

their current values Current Directory Window- points to your current open

directory of MATLAB files Document Window- lets you edit data values easily (also

called the array editor) Graphics Window- this window appears automatically

whenever you do plotting Edit Window- lets you compose and edit programs (M-

files)

04/21/23 3

Page 4: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

MATLAB Windows II

As you use MATLAB you will become very familiar with all these resources and be able to move back and forth easily. Once you enter MATLAB you rarely have to leave it, compose program, run programs, edit programs, review results, save programs all within the MATLAB framework.

04/21/23 4

Page 5: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 5

MATLAB Variables Variable names must start with a

letter, may contain only letters, numbers and the underscore.

Variable names are case sensitive, “time”, “Time”, and “TIME” are all different variable names.

Variable names can be up to 63 characters long.

Page 6: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 6

MATLAB Variables II

Stay away from using MATLAB key words as variable names, “pi”, “i” or “j”, and MATLAB function names like “sqrt”, “sin”, “mean”, “max”, etc.

Use the built in functions “isvarname”, “iskeyword”, and “which” if in doubt.

Page 7: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

MATLAB Variables III

Hint: Use variable names that mean something relevant to the problem. For example, use time rather than t or use distance rather than d and so on. It’s a good idea to self document your problem helping you to remember what the program does.

04/21/23 7

Page 8: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 8

Working with Matrices

MATLAB’s versatility and power come from its ability to work with matrices.

Engineering problems can be set up using matrix notation

MATLAB simplifies matrix manipulations and hence simplifies writing programs to solve engineering problems.

Page 9: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 9

Working with Matrices II Matrices can be

Row vectors, [ 1, 2, 6, 15, 3, 6] Rectangular matrices, n rows and m

columns Column vectors, [1; 5; 7; 11]

Matrices can be assigned variable names, x = [1, 2, 7, 3]

“ ' ” is the transpose operator in MATLAB

Page 10: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 10

Working with Matrices III

Observation

At first, working with matrices and formulating problems this way will seem unnatural, but soon you will catch on and it will become second nature for you.

Page 11: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 11

Scalar Operations Although matrices are key, we will

continue to need to work with scalars and particularly with scalars and matrices together.

Scalar operations Addition and Subtraction ( + and - ) Multiplication and division ( * and / ) Exponentiation ( ^ , 3^2 = 9

)

Page 12: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 12

Scalar Operations II Some examples of scalar operations

a = 3b = a + 1c = 2 * bd = a/c + 5x = 1x = x + 1

' = ' is called the assignment operator. The right_hand_side is evaluated and its value is assigned to the left_hand_side

Page 13: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 13

Precedence of Arithmetic Operations

MATLAB evaluates expressions from left to right in the following operator order or precedence

Precedence Operation

1 Parentheses, innermost first 2 Exponentiation, left to right 3 Multiplication and division, l to

r 4 Addition and subtraction, l to r

Page 14: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 14

Precedence of Arithmetic Operations II Look at the following ( a=1, b=2, c=4, d=5 )

a + b/c + d evaluates to 6.50

(a + b)/c + d 5.75

(a + b)/(c+d) 0.333

a + b/(c+d) 1.222

Page 15: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 15

Precedence of Arithmetic Operations III All four expressions are valid and

correct depending upon the problem you are solving.

Observation To avoid confusion when entering long

expressions, break them up into smaller statements and then combine the results. If in doubt use parentheses to enforce your ordering.

Page 16: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 16

Array Operation MATLAB defines the operators

( .*, ./, and .^ ) to perform “element by element” operations on matrices, row or column vectors. For example,x = [1, 5, 2, 8]y = [2, 1, 3, 2]x .* y yields the result [ 2, 5, 6, 16 ]x ./ y yields the result [ .5, 5, .666, 4 ]x .^ y yields the result [ 1, 5, 8, 64 ]

Page 17: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 17

Array Operations II The colon operator : is used to generate

row vectors, column vectors and matrices.B = 0 : 4 results in B = [ 0, 1, 2, 3, 4 ]A = 1 : 0.5 : 3 A = [ 1, 1.5, 2, 2.5, 3 ]

The linspace function can also be used to generate row vectors.time = linspace (1, 10, 10) yields

time = [1,2,3,4,5,6,7,8,9,10] The logspace function e = logspace(1,3,3) would generate

the vector e = [10, 100, 1000].

Page 18: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 18

Array Operations III

Operations between scalars and matrices, and between scalars and row and column vectors also makes sense, let A be a row vector.A + 5 adds 5 to each element of AA - 3 similarly subtracts 3 from each element of Api* A multiples each element of A by piA / 2 divides each element of A by 2

Page 19: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 19

Array Operations IV Consider our problem of calculating the

distance of a free falling body for a number of values of t, t = time in seconds.

d = ½ * g * t2

>>g = 9.8; % acceleration due to gravity>>t = 0 : 1 : 10; % create t vector with times>>d = 0.5 * g * t.^2;% find d for all values of t>>[t', d'] % output table of t and d

Page 20: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

Supressing Echoes

Note on the previous slide, each line except the last one is followed by a semi-colon. MATLAB will echo the result of each line in the Command Window unless you request to suppress it. Use the semi-colon to suppress the output except for the results that you specifically want to have output.

04/21/23 20

Page 21: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

Display Formats I

MATLAB provides a number of ways to format results that are listed in Table 2.2 on page 40. In all cases, MATLAB uses double precision (64 bits) floating point numbers in calculations. Enter a couple numbers and try the different formats to see what happens.

04/21/23 21

Page 22: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

Display Formats II

Scientific notation is a useful way to enter very large and very small numbers. For example x = 6.023 x 10-6 is entered as x = 6.023e-6, all on one line. Note that spaces generally allowed in MATLAB are not permitted when using scientific notation.

04/21/23 22

Page 23: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 23

Saving your Work At times you may want to save all or

some of the variables and their values that you’ve created during a session. Later when you return, you would like to return the Workspace Window to its previous values and continue your work. To do this you would use the MATLAB “save” and “load” commands in the Command Window.

Page 24: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 24

Saving your Work II First you need to have created a folder in

blank or blank or blank for all your MATLAB work. I call it MyMatlabfiles.

Then browse and set the Current Directory to point to this folder.

Then use “save file_name” command to save your Workspace in the MyMatlabfiles folder

Use “load file_name” to return the Workspace.

file_name needs to be a legal MATLAB name

Page 25: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 25

Saving your Work III Rather than save all your variables

and their values, you can selectively save and restore by>> save file_name A B Cto save only the variables A, B, and C.>> load file_name will restore the variables and values A, B, and C to the Workspace Window

Page 26: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 26

Script M-files An M-file is an ASCII text file containing

MATLAB statements in the form of a program or function.

You use the MATLAB editor to create, modify, and save your programs in your MyMatlabfiles folder with file name file_name.

M-files support MATLAB’s programming environment

Page 27: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 27

Script M-files II Once you have completed writing and

saving your program, you can run it in the Command Window by simply entering the name of the file where it is stored following the Command Window prompt.>> file_namecause your program to be run and the results generated. (You can also run programs directly from the Edit Window.)

Page 28: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 28

Script M-files III

Use the MATLAB “what” command to get a listing of all your MATLAB M-files

Use % followed by a comment generously to document your program

Comments at the beginning of your program are echoed in response to the command >> help file_name

Page 29: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

Cell Mode using the Editor This utility lets you divide a MATLAB program into

cells, you can then execute each cell individually or together with all the cells.

Use %%_ (_ means space) to identify the beginning of each cell. Usually following %%_ is the cell name.

Highlight the cell by moving the cursor to the cell. Click on Cell in the Editor tool bar to select a cell

to perform a function. Cell Mode is useful when debugging larger

programs.

04/21/23 29

Page 30: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

Problem 2.13(b): radians to degrees conversion table

% Convert radians to degrees between 0 and pi%angle_radians = 0:0.1*pi:pi;%angle_degrees = angle_radians * 180/pi;%table = [angle_radians', angle_degrees']

04/21/23 30

Page 31: 10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.

04/21/23 31

Final Observation Chapter 2 introduces us to a number of

MATLAB concepts. The assignments 2.9, 2.11, 2.13, and 2.17/2.18

This give us some practice with these concepts.

Don’t be afraid to try things to see what happens. When you discover some good stuff, send a note to me for our Hints and Help folder on our website.