Presentation is loading. Please wait.

Presentation is loading. Please wait.

BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.

Similar presentations


Presentation on theme: "BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists."— Presentation transcript:

1 BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists

2 Ch.2: MATLAB Fundamentals The objective of this chapter is to introduce some of the fundamentals of MATLAB programming, including: - variables, operators and expressions. - arrays (including vectors and matrices). - basic input and output. - repetition (for ). - decisions (if ).

3 2.1 Variables and the workspace 2.1.1 Variables A variable name must comply with the following two rules: - It may consist only of the letters a –z, the digits 0 –9 and the underscore ( _ ). - It must start with a letter. Examples of valid variable names: r2d2, pay_day. Examples of invalid names: pay-day, 2a. The official MATLAB documentation refers to all variables as arrays, whether they are single-valued (scalars) or multi- valued (vectors or matrices). In other words, a scalar is a 1x1 array.

4 2.1.2 Case sensitivity MATLAB is case sensitive. balance, BALANCE and BaLance are three different variables. Command and function names are also case sensitive. You must not use capitals when running functions and commands!. Note that when you use the command line help, function names are given in capitals, e.g. help CLC.

5 2.1.4 Adding commonly used constants to the workspace If you often use the same physical or mathematical constants in your MATLAB sessions, you can save them in an M-file and run it at the start of a session. For example, the following statements could be saved in myconst.m : >> g = 9.8; % acceleration due to gravity >> avo = 6.023e23; % Avogadro’s number >> e = 2.718281828459045; % base of natural log If you run myconst at the start of a session these three variables will be part of the workspace and will be available for the rest of the session, or until you clear them.

6 2.2 Arrays: vectors and matrices A matrix is a rectangular object consisting of rows and columns. A vector is a special type of matrix, having only one row, or one column. As mentioned before, MATLAB refers to scalars, vectors and matrices generally as arrays.

7 2.2.1 Initializing vectors: explicit lists Enter a statement like : x = [1 3 0 -1 5] - Can you see that you have created a vector (list) with five elements ? Enter the command “disp(x)” to see how MATLAB displays a vector? Enter the command “whos”. You can put commas instead of spaces between vector elements if you like. E.g. x = [1, 3, 0, -1, 5]

8 Cont. You can use one vector in a list for another one, e.g. type in the following: >> a = [1 2 3]; >> b = [4 5]; >> c = [a -b]; - Can you work out what c will look like before displaying it? Try the following: >> a = [1 3 7]; >> a = [a 0 -1];

9 Cont. Enter the following: >> x = [ ] Note in the Workspace browser that the size of x is given as 0-by-0 because x is empty. This means x is defined, and can be used where an array is appropriate without causing an error; however, it has no size or value. Making x empty is not the same as saying x = 0 (in the latter case x has size 1-by-1), or clear x (which removes x from the workspace, making it undefined). An empty array may be used to remove elements from an array(Which will be discussed later).

10 Cont. Remember the following important rules: - Elements in the list must be enclosed in square not round brackets. - Elements in the list must be separated either by spaces or by commas.


Download ppt "BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists."

Similar presentations


Ads by Google