BIL108E : Introduction to the Scientific and Engineering Computing with MATLAB CRN : 23610 LECTURE NOTES – 1 INTRODUCTION TO MATLAB The name MATLAB stands for MATrix LABoratory. MATLAB is a highperformance language for technical computing. It integrates computation, visualization, and programming environment. Furthermore, MATLAB is a modern programming language environment: it has sophisticated data structures, contains built-in editing and debugging tools, and supports objectoriented programming. These factors make MATLAB an excellent tool for teaching and research. MATLAB is a computer program that can be very helpful in solving the sorts of mathematical problems you will frequently encounter throughout your engineering or technology coursework. MATLAB’s built-in features can be used to solve a wide variety of numerical problems, from the very basic, such as a system of 2 equations with 2 unknowns to the more complex, such as Factoring polynomials, Fitting curves to data points, Making calculations using matrices, Performing signal processing operations such as Fourier transforms, Building and training neural networks. Starting And Quitting WINDOWS Start: click Matlab icon Stop: File/Exit Matlab Command Line Type: matlab If MATLAB is successfully invoked, prompt becomes ">>". Type: quit 1 Figure 1. MATLAB workspace (Houcque,2005) Customization MATLAB allows you to personalize the workspace. To do so; “File Preferences” Conversing MATLAB who MATLAB replies with the variables in your workspace what MATLAB replies with the current directory and MATLAB files in the directory help The most important function for learning MATLAB on your own 2 CREATING MATLAB VARIABLES Defining MATLAB variables is a simple procedure. MATLAB variables are created with an assignment statement. variable name = a value (or an expression) To create a variable, simply assign a value to a name: >>var1=3.14 >>myString=‘hello world’ For example, >> x = expression where expression is a combination of numerical values, mathematical operators, variables, and function calls. On other words, expression can involve: • manual entry • built-in functions • user-defined functions Following rules need to be considered while assigning a variable; First character must be a letter After that, any combination of letters and numbers or “_” can be used Case sensitive, so that Var1 and var1 different from each other Built-In Variables 3 Figure 2. Some Built-In Variable examples for MATLAB SYNTAX A scalar is simply just a fancy word for a number (a single value (1*1)). A variable can be given a value explicitly >>a = 85 shows up in workspace! Or as a function of explicit values and existing variables >>c = 1.6*47-2*a To suppress output, end the line with a semicolon »cool= 18/5; A vector is an ordered list of numbers (one-dimensional). In MATLAB they can be represented as a row-vector or a column-vector (1*n) or (n*1). Row vector: comma or space separated values between brackets >>row = [1 2 5.4 -6.6]; >>row = [1, 2, 5.4, -6.6]; Column vector: semicolon separated values between brackets >>column = [4;2;7;4]; 4 Figure 3. Display of row and column vector A matrix is a rectangular array of numbers (multidimensional).In MATLAB, a two-dimensional matrix is defined by its number of rows and columns (n*m) or (m*n). Figure 4. Matrix Formation SAVE/CLEAR/LOAD Use save to save variables to a file >>save myfile a b saves variables a and b to the file myfile.mat myfile.mat file in the current directory Default working directory is >>\MATLAB\work Use clear to remove variables from environment >>clear a b look at workspace, the variables a and b are gone Use load to load variable bindings into the environment 5 >>load myfile OPERATORS MATLAB allows arithmetic operations: +, −, ∗, and ˆ to be carried out on matrices. Thus, A+B or B+A A*B A^2 α*A or A*α is valid if A and B are of the same size is valid if A’s number of column equals B’s number of rows is valid if A is square and equals A*A multiplies each element of A by α On the other hand, array arithmetic operations or array operations for short, are done element-by-element. The period character, ., distinguishes the array operations from the matrix operations. However, since the matrix and array operations are the same for addition (+) and subtraction (−), the character pairs (.+) and (.−) are not used. The list of array operators is shown below in Table 3.2. If A and B are two matrices of the same size with elements A = [aij] and B = [bij], then the command .* ./ .^ Element-by-element multiplication Element-by-element division Element-by-element exponentiation Table 1. Array Operators To simplify, let’s consider two vectors U and V with elements U = [ui] and V = [vj]. U. ∗ V produces [u1v1 u2v2 . . . unvn] U./V produces [u1/v1 u2/v2 . . . un/vn] U.ˆV produces [u1v1 u2v2 . . . unvn ] BUILT-IN FUNCTIONS MATLAB offers many predefined mathematical functions for technical computing which contains a large set of mathematical functions. 6 Table 2. Elementary Functions GETTING HELP Use on-line help to request info on a specific function >> help sqrt In the current version (MATLAB version 7), the doc function opens the on-line version of the help manual. This is very helpful for more complex commands. >> doc plot Use “lookfor” to find functions by keywords. The general form is >> lookfor FunctionName MATLAB FILES The Workspace can be saved to a data file extension is .mat (ex: hello.mat) binary form .mat files can be reloaded by using open from file menu MATLAB offers a wealth of built-in math functions that can be quite helpful for many computational problems Elementary MATLAB functions (help elfun) Trigonometric functions Exponential functions Complex functions Rounding and remainder functions Specialized MATLAB functions (help specfun) Specialized math functions Number theoretic functions Coordinate transformations 7 8