Math 300 UMKC Lab 1 - Worksheet Spring 2013 Learning Objectives: 1. How to use Matlab as a calculator 2. Learn about Matlab built in functions 3. Matrix and Vector arithmetics 4. MATLAB rref command 5. Row operations on matrices 6. Solving linear systems with Matlab 7. How to import and export data between Excel to Matlab Basic Operations in Matlab a. For Numbers & Matrices we have: + (addition) - (subtraction) * (multiplication) / (division) (power) b. For Vectors we have + (addition) - (subtraction).* (multiplication)./ (division). (power) Symbols pi (p = 3.1416) i (complex number e.g. 2+3i) NaN (not a number e.g. 0/0) inf (infinity) 1. Matlab as a calculator: Matlab can be used as a sophisticated calculator. Please enter the following commands followed by the return key. Important note: use parenthesis to specify the order of operation >> 3 4 5 >> 3 (4 5) >> 18/3 + 2 2 >> 18/(3 + 2 2) 1
2. Built in functions: Matlab has several built-in functions. Type elementary math in Matlab Help to get the list of elementary functions. Examples are: (a) Exponential and logarithmic functions: exp(x), log(x), log10(x), sqrt(x), (b) Trigonometric functions: sin(x), cos(x), tan(x), asin(x), acos(x), atan(x) (c) Hyperbolic functions: sinh(x), cosh(x), tanh(x), asinh(x), acosh(x), atanh(x) >> a = cos(3.14) cos(pi) >> cosh( 4) >> atan(3 pi 4) 3 Vectors & Matrices: Entries of a row vector are separated by spaces or commas. Entries of a column vector are separated by semicolons. For instance, we may enter the following vectors in the command window. >> a = [2 3 4 7] >> b = [2; 3; 4; 7] >> length(a) >> c = a >> d = linspace(0, 1, 12) >> logspace(1, 2, 7) >> x = 1 : 2 : 20 >> mean(x) >> min(x) >> max(x) >> y = x >> z = x. y >> w = x./y For Matrices, each row is separated by semicolon. Example: 2
>> M = [2 3 5; 4 5 9; 0 12 23] >> size(m) >> A = M >> M(2, 2) >> M(:, 2) >> M(3, 1 : 3) >> inv(m) >> K = zeros(3, 4) >> G = ones(4, 4) >> I = eye(4) >> L = I G >> P = L G ones(4, 3) K 4. rref command: We want to solve the following linear using rref command. 5x + 4y + 3z + 3u = 11 2y + z u 4w = 1 3x z 4w = 10 4x + 5y 4w = 3 4x + 12y 4u 4w = 0 Step1: Generate matrix A by entering >> A = [5 4 3 3 0; 0 2 1-1 -4; 3 0-1 0-4; -4 5 0 0-4; -4 12 0-4 -4] Step 2: Generate vector b by entering >> b = [11; -1; 10; -3; 0] Step 3: Form the augmented matrix M and transform it into an rref matrix by entering >> M = [A, b] >> rref(m) 3
Step 4: Find the solution(s) if any. 5. Row operations on matrices: We want to solve the following linear system using row operations. x + 4y + 3z = 11 2x + y z = 1 3x y 4z = 10 a ) First we need form the augmented matrix. Note that in Matlab rows are divided by semicolon. This is done as follows. >> A = [1 4 3; 2 1-1; 3-1 -4] >> b = [11; -1; 10] >> M = [A, b] Alternatively we may enter a matrix just the way it is written. For instance, generates the same matrix A. >> A = [1 4 3 2 1-1 3-1 -4] b ) We need to transform the first column into a unit column. Hence we multiply the first row by -2 and add it to the second row. Also we multiply the first row by -3 and add it to the third row. In MATLAB the ith row of Matrix M is indicated by M(i, :). Enter >> M(2, :) = -2*M(1, :) + M(2, :) >> M(3, :) = -3*M(1, :) + M(3, :) c ) Now, we need to get the leading 1 into the entry (2,2) of the new augmented matrix. This is done by multiplying the second row by -1/7. 4
>> M(2, :) = (-1/7)*M(2, :) d ) To make the entry (3,2) zero, we add 13 times of the second row to the third row. >> M(3, :) = 13*M(2, :)+ M(3, :) e ) The last row indicates that the system does not have any solution and there is no need to continue the row operations. Note: To obtain the row reduced model we may directly use rref command, which stands for row-reduced echelon form. To apply this to the last example, enter >> M = [A, b] >> rref(m) 6. Matlab built-in solver: We may use linsolve(a,b) to solve systems of linear equations. Note that A is the matrix of coefficients and b is a column representing the right hand side of the equations. In other words the linear system of equations is represented by AX=b, where X is the solution vector. Noting that the above system does not have any solution by entering >> linsolve(a,b) As expected, we get Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 9.251859e-018. Exercise 1. Consider the following system of linear equations. 3x 2y + 8z = 9 2x + 2y + z = 3 x + 2y 3z = 8 a ) Form A and b matrices and by entering M = [A, b] and rref(m) obtain the row reduced form of the augmented matrix M. b ) Enter linsolve(a,b) to solve the system. You should get to x = 3, y = 4 and z = 1. 5
Exercise 2. Repeat the last problem for the following system of linear equations. 3x 1 + 6x 2 + 12x 3 9x 4 6x 5 = 3 4x 1 8x 2 10x 3 + 18x 4 + 6x 5 = 16 2x 1 4x 2 5x 3 + 9x 4 + 6x 5 = 11 Note that rref(m) clearly shows that the system has infinitely many solutions and linsolve(a,b) returns only a particular solution. 7. Matlab Data Import and Export: a ) Download the Excel file BankLoan.xls into your desktop. This is a hypothetical data file that contains financial and demographic informationon 700 past and prospective customers. b ) Open the file to see the available information about each costumer. c ) Start Matlab by clicking on the Matlab icon. d ) On the file menu choose import data, change the file types to all files and give specific path of BankLoan.xls. Click on Finish. Matlab generates a Matrix called data with the size 700 9. e ) The first column is the age of costumers. To obtain basic stats of the age enter datastats(data(:,1)) in the command window this will return the following statistical information. Num (The number of data values); Max (The maximum data value); Min (The minimum data value); Mean (The mean value of the data); Median (The median value of the data); Range (The range of the data); Std (The standard deviation of the data). f ) Repeat the last section to obtain stats of column 5 which is annual income of costumers. g ) To export data into an Excel enter xlswrite( newbankloan.xls,data). The file is saved in the current Matlab directory. 6