{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Matrices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creation" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "matrix1 =\n", " 1 2\n", " 3 4\n" ] } ], "source": [ "% create a matrix just like vectors - use semicolons to start new row\n", "format compact\n", "matrix1 = [1 2;3 4]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "matrix2 =\n", " 1 2 3\n", " 4 5 6\n", " 7 8 9\n" ] } ], "source": [ "% another example - 3x3 this time\n", "matrix2 = [1 2 3; 4 5 6; 7 8 9]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 6\n" ] } ], "source": [ "% to access elements, use matrix(m,n)\n", "matrix2(2,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Extraction" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "matrix3 =\n", " 3 7 4 2\n", " 6 5 1 8\n", " 4 7 9 10\n", " 23 6 45 89\n" ] } ], "source": [ "clear all\n", "matrix3 = [3 7 4 2; 6 5 1 8; 4 7 9 10; 23 6 45 89]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 9 10\n", " 45 89\n" ] } ], "source": [ "% extract a specific section of matrix with matrix(row:row, col:col)\n", "% here, we want a 2x2 matrix from the bottom right corner\n", "matrix3(3:4,3:4)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 7 9 10\n" ] } ], "source": [ "% if we want a specific range from one row, ex. row 3 cols 2-4\n", "matrix3(3,2:4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Addition/Subtraction" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c =\n", " 6 8\n", " 11 5\n" ] } ], "source": [ "% must be the same size for addition\n", "clear all\n", "a = [1 2; 3 4];\n", "b = [5 6; 8 1];\n", "c = a + b" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c =\n", " -4 -4\n", " -5 3\n" ] } ], "source": [ "c = a - b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiplication" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 4 8\n", " 12 16\n" ] } ], "source": [ "% to multiply by a constant\n", "a * 4" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1 2\n", " 3 4\n" ] } ], "source": [ "% divide by constant\n", "ans / 4" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 21 8\n", " 47 22\n" ] } ], "source": [ "% basic matrix multiplication\n", "a * b" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a =\n", " 1 0\n", " 0 1\n", "b =\n", " 2 0\n", " 0 2\n", "ans =\n", " 2 0\n", " 0 2\n" ] } ], "source": [ "% element-by-element multiplication - use the dot/period\n", "a = [1 0; 0 1]\n", "b = [2 0; 0 2]\n", "a.*b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sum, Min, Max, Size, etc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Sum" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a =\n", " 1 2 3\n", " 4 5 6\n", " 7 8 9\n", "ans =\n", " 12 15 18\n" ] } ], "source": [ "% when we use sum(matrix) the columns are added - the result is a vector\n", "clear all\n", "a = [1 2 3; 4 5 6; 7 8 9]\n", "sum(a)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 45\n" ] } ], "source": [ "% if we want the sum of all elements in a matrix, we can nest the operations\n", "sum(sum(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Min" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a =\n", " -5 78 345 90\n", " 578 -300 189 3\n", " -4 89 989 1000\n", "ans =\n", " -5 -300 189 3\n" ] } ], "source": [ "% much like sum(matrix), the min(matrix) function works the same way\n", "% the minimum value of each COLUMN is retured as a vector\n", "clear all\n", "a = [-5 78 345 90; 578 -300 189 3; -4 89 989 1000]\n", "min(a)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " -300\n" ] } ], "source": [ "% again, by nesting min(min()), we can get the smallest value for a given matrix\n", "min(min(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Max" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 578 89 989 1000\n" ] } ], "source": [ "% max(matrix) works exactly the same\n", "max(a)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1000\n" ] } ], "source": [ "% nesting max\n", "max(max(a))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#### Size" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a =\n", " 1 2 3 4 5\n", " 6 7 8 9 10\n", "ans =\n", " 2 5\n" ] } ], "source": [ "% calling size(matrix) will return the NxM (rows x cols) dimensions\n", "clear all\n", "a = [1 2 3 4 5; 6 7 8 9 10]\n", "size(a)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 10\n" ] } ], "source": [ "% there is another function that will return the number of elements - numel(matrix)\n", "numel(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Augmentation" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Dimensions of matrices being concatenated are not consistent.\n" ] } ], "source": [ "% we can combine multiple matrices, but they must be of the same dimensions\n", "clear all\n", "a = [1 2 3 4 5; 6 7 8 9 10]\n", "b = [11 12 13; 14 15 16]\n", "c = [a;b]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b =\n", " 11 12 13 14 15\n", " 16 17 18 19 20\n", "c =\n", " 1 2 3 4 5\n", " 6 7 8 9 10\n", " 11 12 13 14 15\n", " 16 17 18 19 20\n" ] } ], "source": [ "% correct the dimensions for b and it works\n", "b = [11 12 13 14 15; 16 17 18 19 20]\n", "c = [a;b]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Important Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calling functions such sin(x), log(x), etc. on a variable will operate the function as expected. But with matrices, many functions can be applied to the entire matrix or to each element." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### sqrt() and sqrtm()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", " 1 4 9\n", " 16 25 36\n", " 49 64 81\n", "ans =\n", " 1 2 3\n", " 4 5 6\n", " 7 8 9\n" ] } ], "source": [ "% if we call sqrt(matrix) we get the square root of each element\n", "A = [1 4 9; 16 25 36; 49 64 81]\n", "sqrt(A)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "R =\n", " 0.6344 + 1.3620i 0.3688 + 0.7235i 0.7983 - 0.4388i\n", " 1.4489 + 1.1717i 2.7697 + 0.6224i 3.2141 - 0.3775i\n", " 4.3578 - 1.6237i 5.7110 - 0.8625i 7.7767 + 0.5231i\n" ] } ], "source": [ "% however, using sqrtm(matrix) will give us the matrix R that if multipled by R would give us A\n", "R = sqrtm(A)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1.0000 - 0.0000i 4.0000 - 0.0000i 9.0000 - 0.0000i\n", " 16.0000 - 0.0000i 25.0000 - 0.0000i 36.0000 - 0.0000i\n", " 49.0000 + 0.0000i 64.0000 + 0.0000i 81.0000 + 0.0000i\n" ] } ], "source": [ "R*R" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1 4 9\n", " 16 25 36\n", " 49 64 81\n" ] } ], "source": [ "% the result is still in complex, but notice that the imaginary coefficients are all zero\n", "% we can get the output back to the way it was....\n", "round(real(R*R))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Expected input to be a square matrix.\n" ] } ], "source": [ "% NOTE: sqrtm(matrix) can only be performed on MxM (rows=cols) matrices!\n", "B = [1 2 3; 4 5 6];\n", "sqrtm(B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### exp() and expm()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1.0e+35 *\n", " 0.0000 0.0000 0.0000\n", " 0.0000 0.0000 0.0000\n", " 0.0000 0.0000 1.5061\n" ] } ], "source": [ "% exponential\n", "exp(A)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1.0e+48 *\n", " 0.4442 0.6130 0.8149\n", " 1.9817 2.7349 3.6359\n", " 4.6459 6.4116 8.5240\n" ] } ], "source": [ "% note the difference in output\n", "expm(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### log() and logm()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 0 1.3863 2.1972\n", " 2.7726 3.2189 3.5835\n", " 3.8918 4.1589 4.3944\n" ] } ], "source": [ "% log\n", "log(A)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\bWarning: Principal matrix logarithm is not defined for A with nonpositive real\n", "eigenvalues. A non-principal matrix logarithm is returned.]\b \n", "[\b> In logm (line 78)\n", " In pymat_eval (line 31)\n", " In matlabserver (line 24)]\b \n", "ans =\n", " 0.6794 + 1.7064i 1.1840 + 0.9064i -0.1180 - 0.5498i\n", " 2.4176 + 1.4680i 0.9442 + 0.7798i 1.3825 - 0.4730i\n", " 0.3878 - 2.0343i 2.2002 - 1.0806i 3.7517 + 0.6554i\n" ] } ], "source": [ "% again, the output is NOT the same\n", "logm(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Matrices and Powers " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are notable differences when using matrices and powers. Given matrix A and any number n, we can perform the following operations:\n", "1. $n^{A}$ — n is raised to the power of A \n", "2. $n^{A(i,j)}$ — n is raised to the power of each element in A\n", "3. $A^{n}$ — A is raised to the power of n\n", "4. $A(i,j)^{n}$ — each element in A is raised to the power of n " ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", " 1 2 3\n", " 4 5 6\n", " 7 8 9\n", "ans =\n", " 1.0e+04 *\n", " 0.7962 0.9782 1.1603\n", " 1.8029 2.2154 2.6276\n", " 2.8097 3.4523 4.0950\n" ] } ], "source": [ "% scenario 1: raising 2 to the power of A\n", "A = [1 2 3; 4 5 6; 7 8 9]\n", "2^A" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 2 4 8\n", " 16 32 64\n", " 128 256 512\n" ] } ], "source": [ "% scenario 2: raising n to the power of each element in A — note the dot/period \n", "2.^A" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 30 36 42\n", " 66 81 96\n", " 102 126 150\n" ] } ], "source": [ "% scenario 3: raising A to the power of 2\n", "A^2" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1 4 9\n", " 16 25 36\n", " 49 64 81\n" ] } ], "source": [ "% scenario 4: raising each element in A to the power of 2 - note the dot/period\n", "A.^2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Special Matrices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many matrix calculations require the use of special matrices populated with specific values. Some special-purpose matrices include all zeros, all ones, and diagonal ones (identity). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### zeros() and ones()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Z =\n", " 0 0 0\n", " 0 0 0\n", " 0 0 0\n" ] } ], "source": [ "% zeros(N) will produce an NxN matrix populated with zeros\n", "Z = zeros(3)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "O =\n", " 1 1 1 1 1\n", " 1 1 1 1 1\n", " 1 1 1 1 1\n", " 1 1 1 1 1\n", " 1 1 1 1 1\n" ] } ], "source": [ "% the same applies to creating an NxN matix of ones\n", "O = ones(5)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "O =\n", " 1 1 1 1 1\n", " 1 1 1 1 1\n", "Z =\n", " 0 0\n", " 0 0\n", " 0 0\n", " 0 0\n" ] } ], "source": [ "% to specify an NxM matrix, we call ones(N,M) and zeros(N,M) where N = rows and M = cols\n", "O = ones(2,5)\n", "Z = zeros(4,2)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#### eye()" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1 0 0\n", " 0 1 0\n", " 0 0 1\n" ] } ], "source": [ "% to create the identity matrix (diagonal ones) we use eye(N)/eye(N,M)\n", "format compact\n", "eye(3)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1 0 0 0\n", " 0 1 0 0\n", " 0 0 1 0\n" ] } ], "source": [ "% note that if the parameters are not NxN, MATLAB still tries to do it\n", "eye(3,4)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", " 1 2 3\n", " 4 5 6\n", " 7 8 9\n", "ans =\n", " 1 2 3\n", " 4 5 6\n", " 7 8 9\n" ] } ], "source": [ "% multiplying A by identity gives A\n", "A = [1 2 3; 4 5 6; 7 8 9]\n", "A*eye(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### magic()" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 8 1 6\n", " 3 5 7\n", " 4 9 2\n" ] } ], "source": [ "% magic(N) creates a NxN matrix where the sums of each row and each column are equal\n", "magic(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Transpose" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "V =\n", " 1 2 3 4 5\n", "ans =\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n" ] } ], "source": [ "% recall that for vectors, which are actually matrices, we use the single-quote to create transpose\n", "V = [1 2 3 4 5]\n", "V'" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", " 1 2 3 4\n", " 5 6 7 8\n", "ans =\n", " 1 5\n", " 2 6\n", " 3 7\n", " 4 8\n" ] } ], "source": [ "% creating a transpose of an NxM matrix will return a MxN transposed matrix\n", "A = [1 2 3 4; 5 6 7 8]\n", "A'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### diag()" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", " 1 2 3 4\n", " 5 6 7 8\n", " 9 10 11 12\n", "ans =\n", " 1\n", " 6\n", " 11\n", "ans =\n", " 1 6 11\n" ] } ], "source": [ "% to extract the diagonal of an NxN matrix, we use diag(A)\n", "A = [1 2 3 4; 5 6 7 8; 9 10 11 12]\n", "diag(A)\n", "diag(A)'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### triu() and tril()" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", " 1 2 3 4\n", " 5 6 7 8\n", " 9 10 11 12\n", " 13 14 15 16\n", "ans =\n", " 1 2 3 4\n", " 0 6 7 8\n", " 0 0 11 12\n", " 0 0 0 16\n" ] } ], "source": [ "% we use these to zero-out the portion we are not interested in above/below the diagonal\n", "% if we want the upper portion of A\n", "A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]\n", "triu(A)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1 0 0 0\n", " 5 6 0 0\n", " 9 10 11 0\n", " 13 14 15 16\n" ] } ], "source": [ "% the same goes for the lower half\n", "tril(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solving Systems of Equations with Matrices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recall that systems of equations can be solved using matrices. Given the following system, we can model it as a 2x3 matrix.\n", "\\begin{equation}x+4y=-3\\end{equation}\n", "\\begin{equation}6x+2y=8\\end{equation}" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", " 1 4 -3\n", " 6 2 8\n" ] } ], "source": [ "% remember to align each variable in the same cols - here, x is col 1, y is col 2 and numeric solution col 3\n", "A = [1 4 -3; 6 2 8]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To solve the system, we use reduced-row echelon form **rref()**" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 1.0000 0 1.7273\n", " 0 1.0000 -1.1818\n" ] } ], "source": [ "% pass the system matrix to rref()\n", "rref(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the result, there is a coeffecient of 1 for x and y; in the answer column we have numeric results. So the solution to the system is\\begin{equation}x=1.7273\\end{equation}\\begin{equation}y=-1.1818\\end{equation}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Trace, Rank, Determinant, Inverse" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", " 1 2 3\n", " 4 5 6\n", " 7 8 9\n", "ans =\n", " 15\n" ] } ], "source": [ "% the trace is the sum of the diagonal\n", "A = [1 2 3; 4 5 6; 7 8 9]\n", "trace(A)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 2\n" ] } ], "source": [ "% rank is called by rank(A)\n", "rank(A)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 6.6613e-16\n" ] } ], "source": [ "% the determinant is another useful function provided by MATLAB\n", "det(A)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", " 1 2 3\n", " 4 5 6\n", " 6 -1 0\n", "ans =\n", " -0.2500 0.1250 0.1250\n", " -1.5000 0.7500 -0.2500\n", " 1.4167 -0.5417 0.1250\n" ] } ], "source": [ "% again, the inverse is often calculated for matrices\n", "A = [1 2 3; 4 5 6; 6 -1 0]\n", "inv(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Symbolics and Matrices" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", "[ 2 - x, 3*x^3]\n", "[ 4*x^2 - 3*x, 9*x^3 + 2]\n", "B =\n", "[ - x^3 + 2*x, 8*x + 1]\n", "[ - x^3 + x, 8*x^3 + 5*x]\n" ] } ], "source": [ "% clear and start some symbolic variables \n", "clear all\n", "x = sym('x'); % longer workaround - Matlab kernel in Jupyter notebook not allowing syms() function\n", "A = sym('A');\n", "B = sym('B');\n", "A = [2-x 3*x^3; 4*x^2-3*x 9*x^3+2]\n", "B = [2*x-x^3 8*x+1; x-x^3 5*x+8*x^3]" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/ 3 \\\n", "| 2 - x, 3 x |\n", "| |\n", "| 2 3 |\n", "\\ 4 x - 3 x, 9 x + 2 /\n", "\n", "/ 3 \\\n", "| - x + 2 x, 8 x + 1 |\n", "| |\n", "| 3 3 |\n", "\\ - x + x, 8 x + 5 x /\n" ] } ], "source": [ "% lets clean it up!\n", "pretty(A)\n", "pretty(B)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/ 3 3 \\\n", "| 9 x + 2 3 x |\n", "| - --------, ---- |\n", "| #1 #1 |\n", "| |\n", "| 2 |\n", "| - 4 x + 3 x x - 2 |\n", "| - ------------, ----- |\n", "\\ #1 #1 /\n", "\n", "where\n", "\n", " 5 3\n", " #1 == (6 x - 9 x + x - 2) 2\n" ] } ], "source": [ "% we can perform many of the previous functions on symbolics\n", "pretty(inv(A))" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/ 3 \\\n", "| - x + 2 x |\n", "| |\n", "| 3 |\n", "\\ 8 x + 5 x /\n" ] } ], "source": [ "pretty(diag(B))" ] } ], "metadata": { "kernelspec": { "display_name": "Matlab", "language": "matlab", "name": "matlab" }, "language_info": { "codemirror_mode": "octave", "file_extension": ".m", "help_links": [ { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-matlab", "name": "matlab", "version": "0.8.0" } }, "nbformat": 4, "nbformat_minor": 0 }