Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161."— Presentation transcript:

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

2 10/24/20152 Review Topics MATLAB variables Working with Matrices Scalar Operations Precedence of Arithmetic Operations Array Operations Saving your work Script M-files

3 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) 10/24/20153

4 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. 10/24/20154

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

6 10/24/20156 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.

7 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. 10/24/20157

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

9 10/24/20159 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

10 10/24/201510 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.

11 10/24/201511 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 )

12 10/24/201512 Scalar Operations II Some examples of scalar operations a = 3 b = a + 1 c = 2 * b d = a/c + 5 x = 1 x = x + 1 ' = ' is called the assignment operator. The right_hand_side is evaluated and its value is assigned to the left_hand_side

13 10/24/201513 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

14 10/24/201514 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 + d5.75 (a + b)/(c+d)0.333 a + b/(c+d)1.222

15 10/24/201515 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.

16 10/24/201516 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 ]

17 10/24/201517 Array Operations II The colon operator : is used to generate row vectors, column vectors and matrices. B = 0 : 4results inB = [ 0, 1, 2, 3, 4 ] A = 1 : 0.5 : 3A = [ 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].

18 10/24/201518 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 A A - 3 similarly subtracts 3 from each element of A pi* A multiples each element of A by pi A / 2 divides each element of A by 2

19 10/24/201519 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 * t 2 >>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

20 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. 10/24/201520

21 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. 10/24/201521

22 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. 10/24/201522

23 10/24/201523 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.

24 10/24/201524 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

25 10/24/201525 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 C to 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

26 10/24/201526 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

27 10/24/201527 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_name cause your program to be run and the results generated. (You can also run programs directly from the Edit Window.)

28 10/24/201528 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

29 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. 10/24/201529

30 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'] 10/24/201530

31 10/24/201531 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.


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

Similar presentations


Ads by Google