25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB - Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2. MATLAB windows. 3. Arithmetic operations with scalars. 4. Working in the command window. 5. Defining variables (scalars). 1-3 MATLAB is a software for numerical computations, visualization and programming. The name MATLAB stands for MATrix LABoratory. Its basic data element is an array (explained later). With MATLAB you can: Use it as a calculator. Define variables and use them in calculations. Use built-in functions (sin, cos, max, min, etc.). Plot graphs. Write and run computer programs. Do symbolic calculations. 1
MATLAB windows: 6 Command Window: Main window that opens when MATLAB is started. It has the command prompt». All commands can be typed in this window. Used for running programs written by user. Figure Window: Contains graphs created by graphics commands. This window opens automatically. Editor Window: Used for writing and editing programs. This window is opened from the File menu in the Command Window. Help Window: Contains help information. This window is opened from the Help menu in any of the previous windows. Command Window (The Command Window opens when MATLAB is started) 6 Command prompt p Figure Window (The Figure Window opens automatically by the plot command) 7 2
Editor Window (The Editor Window is opened from the file menu in the Command Window) 7 Help Window (The Help Window can be opened from the Help menu of any of MATLAB windows) 8 WORKING IN THE COMMAND WINDOW 8-9 To type a command the cursor must be placed after the command prompt (>>). Once a command is typed, and the Enter keyis pressed, the command is executed. (Only the last command is executed. Everything executed before is unchanged) It is not possible to go back to a previous line and make a correction. A previously typed command can be recalled to the command prompt with the up-arrow key. 3
ARITHMETIC OPERATIONS WITH SCALARS 10 Operation Symbol Example Addition + 5+3 Subtraction - 5-3 Multiplication * 5*3 Right Division / 5/3 Left Division \ 5\3=3/5 Exponentiation ^ 5^3 NOTE: For scalars the arithmetic operations are the usual ones. For vectors and matrices the arithmetic operations can either follows matrix algebra rules, or can be performed on element-by-element basis (discussed in the next lectures). ORDER Of PRECEDENCE (The order in which operations are executed by the computer) 10 Higher-precedence operations are executed before lowerprecedence operations. If two operations have the same precedence, then the expression is executed from left to right. PRECEDENCE First Second Third Fourth OPERATION Parentheses, starting with the innermost pair. Exponentiation. Multiplication and division (equal precedence). Addition and subtraction (equal precedence). USING MATLAB AS A CALCULATOR 11 Using numbers: >> 7+8/2 11 8/2 is executed first >> (7+8)/2 7.5000 7+8 is executed first >> 4+5/3+2 7.6667 5/3 is executed first 4
5^3/2 62.5000>> 5^3 is executed first, /2 is executed next. 11 >> 27^(1/3)+32^0.2 5 1/3 is executed first, ^ is executed next, + is executed last. >> 27^1/3+32^0.2 11 27^1 and 32^0.2 is executed first, /3 is executed next, + is executed last. DISPLAY FORMATS The format command controls how output numbers appear on the screen. Input numbers can be written in any format. 12-13 format short (the default) 41.4286 Fixed-point with 4 decimal digits. format long 41.42857142857143 Fixed-point with 14 decimal digits. format short e 4.1429e+001 Scientific notation with 4 decimal digits. format long e 4.142857142857143e+001 Scientific notation with 15 decimal digits. Format bank 41.43 Two decimal digits. MATLAB has several other formats in which numbers can be displayed. MATLAB BUILT-IN MATH FUNCTIONS In addition to arithmetic operations, MATLAB can be used to calculate elementary math functions. The most common ones are: sin(x) x in radians exp(x) exponential cos(x) x in radians log(x) natural logarithm tan(x) x in radians cot(x) x in radians log10(x) base 10 logarithm sqrt(x) square root The inverse is: asin(x), i ( ) abs(x) absolute value acos(x), etc. Examples: >> sin(0.78539) >> sqrt(169) >> log10(10000) 0.7071 13 4 MATLAB has hundreds of built-in functions (this will be discussed in future lectures). 13-15 5
THE ASSIGNMENT OPERATOR 15-17 In MATLAB, the = sign is called the ASSIGNMENT OPERATOR. The ASSIGNMENT OPERATOR assigns a value to a variable. Variable = A value, or a computable value The left hand side can only be one variable. The right hand side can be a specific value, or a computable expression (an expression that includes values and/or previously defined variables). THE ASSIGNMENT OPERATOR For example, if you type: >> x = 3 MATLAB assigns the value of 3 to x. x = 3 If then you type: >> x = x + 5 x = 8 MATLAB assigns a new value to x, which is the old value 3 plus 5. (In mathematics this expression has no meaning since it implies: 0 = 5.) For example, the statement: x + 4 = 30 is not valid. MATLAB does not solve for x, but the statement: x = 30 4 is valid (the number 26 is assigned to x.) 15-17 15- DEFINING VARIABLES A variable is defined by typing a variable name followed by the assignment operator (equal sign) and then a value, or a mathematical expression. >> a=8 a = 8 >> B=12 B = 12 Once a variable is defined, the computer remembers and stores its value. The variable can then be used in further calculations. >> a+b 20 >> a/b 0.6667 >> B/a 1.5000 >> B^a 429981696 6
15- Variables can also be used to define new variables >> d=a*b d = 96 Once in existence, variables can be used in functions >> sqrt(d) 9.7980 A previously defined variable can be redefined and reassigned a new value. RULES ABOUT VARIABLES NAMES Variable names can be up to 63 characters long. Variable name can contain letters, digits, and the underscore character. Variable name must begin with a letter. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. For example, A and a are not the same variable. AVOID USING NAMES OF FUNCTIONS FOR VARIABLES. Once a function name is used to define a variable, the function can not be used. This means that variables should not be named sin, cos, exp, tan, sqrt,., etc. OR: max, min, sum, det,., etc. 7
PREDEFINED VARIABLES MATLAB has several variables that are predefined. These variables can be redefined to have any other value. It is probably better not to use the predefined variables as variable names. Some of the predefined variables are: pi ( ), eps (the smallest difference between two numbers) inf (infinity) i (square root of 1) j (square root of 1) ans (the value of the most recent calculation) Typing these variables gives: >> pi 3.1416 >> sin(pi/4) 0.7071 >> eps 2.2204e-016 >> inf Inf >> i 0 + 1.0000i SOME USEFUL COMMANDS 9, 19 When these commands are typed in the Command Window they either provide information, or perform a task. ; When a semicolon is typed at the end of a line, the computer does not displays the output. >> abc=37 >> def=23; abc = >> 37 clc Clears the command window. clear Removes all variables from memory. clear x y z Clears only variables x, y and z. who Lists the variables currently in memory. clf Clears the Figure Window. % typing % at the beginning of a line designates the line as a comment, which is not executed. 8