Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Linear Algebra (Review/Introduction)\n",
"\n",
"Representation of linear equations:\n",
"\n",
"1. $5x_{1}+3x_{2} =1$\n",
"\n",
"2. $x_{1}+2x_{2}+3x_{3} =2$\n",
"\n",
"3. $x_{1}+x_{2}+x_{3} =3$\n",
"\n",
"in matrix form:\n",
"\n",
"$\\left[ \\begin{array}{ccc}\n",
"5 & 3 & 0 \\\\\n",
"1 & 2 & 3 \\\\\n",
"1 & 1 & 1 \\end{array} \\right]\n",
"\\left[\\begin{array}{c} \n",
"x_{1} \\\\ \n",
"x_{2} \\\\\n",
"x_{3}\\end{array}\\right]=\\left[\\begin{array}{c} \n",
"1 \\\\\n",
"2 \\\\\n",
"3\\end{array}\\right]$\n",
"\n",
"$Ax=b$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Vectors \n",
"\n",
"column vector x (length of 3):\n",
"\n",
"$\\left[\\begin{array}{c} \n",
"x_{1} \\\\ \n",
"x_{2} \\\\\n",
"x_{3}\\end{array}\\right]$\n",
"\n",
"row vector y (length of 3):\n",
"\n",
"$\\left[\\begin{array}{ccc} \n",
"y_{1}~ \n",
"y_{2}~ \n",
"y_{3}\\end{array}\\right]$\n",
"\n",
"vector of length N:\n",
"\n",
"$\\left[\\begin{array}{c} \n",
"x_{1} \\\\ \n",
"x_{2} \\\\\n",
"\\vdots \\\\\n",
"x_{N}\\end{array}\\right]$\n",
"\n",
"The $i^{th}$ element of x is $x_{i}$"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x =\n",
"\n",
" 1 2 3 4 5 6 7 8 9 10\n",
"\n"
]
}
],
"source": [
"x=[1:10]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans =\n",
"\n",
" 1\n",
" 2\n",
" 3\n",
" 4\n",
" 5\n",
" 6\n",
" 7\n",
" 8\n",
" 9\n",
" 10\n",
"\n"
]
}
],
"source": [
"x'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Matrices\n",
"\n",
"Matrix A is 3x3:\n",
"\n",
"$A=\\left[ \\begin{array}{ccc}\n",
"5 & 3 & 0 \\\\\n",
"1 & 2 & 3 \\\\\n",
"1 & 1 & 1 \\end{array} \\right]$\n",
"\n",
"elements in the matrix are denoted $A_{row~column}$, $A_{23}=3$\n",
"\n",
"In general, matrix, B, can be any size, $M \\times N$ (M-rows and N-columns):\n",
"\n",
"$B=\\left[ \\begin{array}{cccc}\n",
"B_{11} & B_{12} &...& B_{1N} \\\\\n",
"B_{21} & B_{22} &...& B_{2N} \\\\\n",
"\\vdots & \\vdots &\\ddots& \\vdots \\\\\n",
"B_{M1} & B_{M2} &...& B_{MN}\\end{array} \\right]$"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A =\n",
"\n",
" 5 3 0\n",
" 1 2 3\n",
" 1 1 1\n",
"\n"
]
}
],
"source": [
"A=[5,3,0;1,2,3;1,1,1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Multiplication\n",
"\n",
"A column vector is a $1\\times N$ matrix and a row vector is a $M\\times 1$ matrix\n",
"\n",
"**Multiplying matrices is not commutative**\n",
"\n",
"$A B \\neq B A$\n",
"\n",
"Inner dimensions must agree, output is outer dimensions. \n",
"\n",
"A is $M1 \\times N1$ and B is $M2 \\times N2$\n",
"\n",
"C=AB\n",
"\n",
"Therefore N1=M2 and C is $M1 \\times N2$\n",
"\n",
"If $C'=BA$, then N2=M1 and C is $M2 \\times N1$\n",
"\n",
"\n",
"e.g. \n",
"$A=\\left[ \\begin{array}{cc}\n",
"5 & 3 & 0 \\\\\n",
"1 & 2 & 3 \\end{array} \\right]$\n",
"\n",
"$B=\\left[ \\begin{array}{cccc}\n",
"1 & 2 & 3 & 4 \\\\\n",
"5 & 6 & 7 & 8 \\\\\n",
"9 & 10 & 11 & 12 \\end{array} \\right]$\n",
"\n",
"C=AB\n",
"\n",
"$[2\\times 4] = [2 \\times 3][3 \\times 4]$\n",
"\n",
"The rule for multiplying matrices, A and B, is\n",
"\n",
"$C_{ij} = \\sum_{k=1}^{n}A_{ik}B_{kj}$\n",
"\n",
"In the previous example, \n",
"\n",
"$C_{11} = A_{11}B_{11}+A_{12}B_{21}+A_{13}B_{31} = 5*1+3*5+0*9=20$\n",
"\n",
"\n",
"Multiplication is associative:\n",
"\n",
"$(AB)C = A(BC)$\n",
"\n",
"and distributive:\n",
"\n",
"$A(B+C)=AB+AC$"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A =\n",
"\n",
" 5 3 0\n",
" 1 2 3\n",
"\n",
"B =\n",
"\n",
" 1 2 3 4\n",
" 5 6 7 8\n",
" 9 10 11 12\n",
"\n"
]
}
],
"source": [
"A=[5,3,0;1,2,3] \n",
"B=[1,2,3,4;5,6,7,8;9,10,11,12]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C =\n",
"\n",
" 0 0 0 0\n",
" 0 0 0 0\n",
"\n",
"C =\n",
"\n",
" 20 28 36 44\n",
" 38 44 50 56\n",
"\n"
]
}
],
"source": [
"C=zeros(2,4)\n",
"C(1,1)=A(1,1)*B(1,1)+A(1,2)*B(2,1)+A(1,3)*B(3,1);\n",
"C(1,2)=A(1,1)*B(1,2)+A(1,2)*B(2,2)+A(1,3)*B(3,2);\n",
"\n",
"C=A*B"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"error: operator *: nonconformant arguments (op1 is 3x4, op2 is 2x3)\r\n"
]
}
],
"source": [
"Cp=B*A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Representation of a problem with Matrices and Vectors\n",
"\n",
"If you have a set of known output, $y_{1},~y_{2},~...y_{N}$ and a set of equations that\n",
"relate unknown inputs, $x_{1},~x_{2},~...x_{N}$, then these can be written in a vector\n",
"matrix format as:\n",
"\n",
"$y=Ax=\\left[\\begin{array}{c} \n",
"y_{1} \\\\ \n",
"y_{2} \\\\\n",
"\\vdots \\\\\n",
"y_{N}\\end{array}\\right]\n",
"=\n",
"A\\left[\\begin{array}{c} \n",
"x_{1} \\\\ \n",
"x_{2} \\\\\n",
"\\vdots \\\\\n",
"x_{N}\\end{array}\\right]$\n",
"\n",
"$A=\n",
"\\left[\\begin{array}{cccc} \n",
"| & | & & | \\\\ \n",
"a_{1} & a_{2} & ... & a_{N} \\\\ \n",
"| & | & & | \\end{array}\\right]$\n",
"\n",
"or \n",
"\n",
"$y = a_{1}x_{1} + a_{2}x_{2} +...+a_{N}x_{N}$\n",
"\n",
"where each $a_{i}$ is a column vector and $x_{i}$ is a scalar taken from the $i^{th}$\n",
"element of x.\n",
"\n",
"Consider the following problem, 4 masses are connected in series to 4 springs with K=10 N/m. What are the final positions of the masses? \n",
"\n",
"![Springs-masses](mass_springs.svg)\n",
"\n",
"The masses haves the following amounts, 1, 2, 3, and 4 kg for masses 1-4. Using a FBD for each mass:\n",
"\n",
"$m_{1}g+k(x_{2}-x_{1})-kx_{1}=0$\n",
"\n",
"$m_{2}g+k(x_{3}-x_{2})-k(x_{2}-x_{1})=0$\n",
"\n",
"$m_{3}g+k(x_{4}-x_{3})-k(x_{3}-x_{2})=0$\n",
"\n",
"$m_{4}g-k(x_{4}-x_{3})=0$\n",
"\n",
"in matrix form:\n",
"\n",
"$\\left[ \\begin{array}{cccc}\n",
"2k & -k & 0 & 0 \\\\\n",
"-k & 2k & -k & 0 \\\\\n",
"0 & -k & 2k & -k \\\\\n",
"0 & 0 & -k & k \\end{array} \\right]\n",
"\\left[ \\begin{array}{c}\n",
"x_{1} \\\\\n",
"x_{2} \\\\\n",
"x_{3} \\\\\n",
"x_{4} \\end{array} \\right]=\n",
"\\left[ \\begin{array}{c}\n",
"m_{1}g \\\\\n",
"m_{2}g \\\\\n",
"m_{3}g \\\\\n",
"m_{4}g \\end{array} \\right]$"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"K =\n",
"\n",
" 20 -10 0 0\n",
" -10 20 -10 0\n",
" 0 -10 20 -10\n",
" 0 0 -10 10\n",
"\n",
"y =\n",
"\n",
" 9.8100\n",
" 19.6200\n",
" 29.4300\n",
" 39.2400\n",
"\n",
"x =\n",
"\n",
" 9.8100\n",
" 18.6390\n",
" 25.5060\n",
" 29.4300\n",
"\n"
]
}
],
"source": [
"k=10; % N/m\n",
"m1=1; % kg\n",
"m2=2;\n",
"m3=3;\n",
"m4=4;\n",
"g=9.81; % m/s^2\n",
"K=[2*k -k 0 0; -k 2*k -k 0; 0 -k 2*k -k; 0 0 -k k]\n",
"y=[m1*g;m2*g;m3*g;m4*g]\n",
"\n",
"x=K\\y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Matrix Operations\n",
"\n",
"Identity matrix `eye(M,N)` **Assume M=N unless specfied**\n",
"\n",
"$I_{ij} =1$ if $i=j$ \n",
"\n",
"$I_{ij} =0$ if $i\\neq j$ \n",
"\n",
"AI=A=IA\n",
"\n",
"Diagonal matrix, D, is similar to the identity matrix, but each diagonal element can be any\n",
"number\n",
"\n",
"$D_{ij} =d_{i}$ if $i=j$ \n",
"\n",
"$D_{ij} =0$ if $i\\neq j$ "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Transpose\n",
"\n",
"The transpose of a matrix changes the rows -> columns and columns-> rows\n",
"\n",
"$(A^{T})_{ij} = A_{ji}$\n",
"\n",
"for diagonal matrices, $D^{T}=D$\n",
"\n",
"in general, the transpose has the following qualities:\n",
" \n",
"1. $(A^{T})^{T}=A$\n",
"\n",
"2. $(AB)^{T} = B^{T}A^{T}$\n",
"\n",
"3. $(A+B)^{T} = A^{T} +B^{T}$ \n",
"\n",
"All matrices have a symmetric and antisymmetric part:\n",
"\n",
"$A= 1/2(A+A^{T}) +1/2(A-A^{T})$\n",
"\n",
"If $A=A^{T}$, then A is symmetric, if $A=-A^{T}$, then A is antisymmetric."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans =\n",
"\n",
" 20 38\n",
" 28 44\n",
" 36 50\n",
" 44 56\n",
"\n",
"ans =\n",
"\n",
" 20 38\n",
" 28 44\n",
" 36 50\n",
" 44 56\n",
"\n",
"error: operator *: nonconformant arguments (op1 is 3x2, op2 is 4x3)\n"
]
}
],
"source": [
"(A*B)'\n",
"\n",
"B'*A' % if this wasnt true, then inner dimensions wouldn;t match\n",
"\n",
"A'*B' == (A*B)'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Square matrix operations\n",
"\n",
"### Trace\n",
"\n",
"The trace of a square matrix is the sum of the diagonal elements\n",
"\n",
"$tr~A=\\sum_{i=1}^{N}A_{ii}$\n",
"\n",
"The trace is invariant, meaning that if you change the basis through a rotation, then the\n",
"trace remains the same. \n",
"\n",
"It also has the following properties:\n",
"\n",
"1. $tr~A=tr~A^{T}$\n",
"\n",
"2. $tr~A+tr~B=tr(A+B)$\n",
"\n",
"3. $tr(kA)=k tr~A$\n",
"\n",
"4. $tr(AB)=tr(BA)$"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"id_m =\n",
"\n",
"Diagonal Matrix\n",
"\n",
" 1 0 0\n",
" 0 1 0\n",
" 0 0 1\n",
"\n",
"ans = 3\n"
]
}
],
"source": [
"id_m=eye(3)\n",
"trace(id_m)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Inverse\n",
"\n",
"The inverse of a square matrix, $A^{-1}$ is defined such that\n",
"\n",
"$A^{-1}A=I=AA^{-1}$\n",
"\n",
"Not all square matrices have an inverse, they can be *singular* or *non-invertible*\n",
"\n",
"The inverse has the following properties:\n",
"\n",
"1. $(A^{-1})^{-1}=A$\n",
"\n",
"2. $(AB)^{-1}=B^{-1}A^{-1}$\n",
"\n",
"3. $(A^{-1})^{T}=(A^{T})^{-1}$"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A =\n",
"\n",
" 0.5762106 0.3533174 0.7172134\n",
" 0.7061664 0.4863733 0.9423436\n",
" 0.4255961 0.0016613 0.3561407\n",
"\n"
]
}
],
"source": [
"A=rand(3,3)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ainv =\n",
"\n",
" 41.5613 -30.1783 -3.8467\n",
" 36.2130 -24.2201 -8.8415\n",
" -49.8356 36.1767 7.4460\n",
"\n"
]
}
],
"source": [
"Ainv=inv(A)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"B =\n",
"\n",
" 0.524529 0.470856 0.708116\n",
" 0.084491 0.730986 0.528292\n",
" 0.303545 0.782522 0.389282\n",
"\n"
]
}
],
"source": [
"B=rand(3,3)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans =\n",
"\n",
" -182.185 125.738 40.598\n",
" -133.512 97.116 17.079\n",
" 282.422 -200.333 -46.861\n",
"\n",
"ans =\n",
"\n",
" -182.185 125.738 40.598\n",
" -133.512 97.116 17.079\n",
" 282.422 -200.333 -46.861\n",
"\n",
"ans =\n",
"\n",
" 41.5613 36.2130 -49.8356\n",
" -30.1783 -24.2201 36.1767\n",
" -3.8467 -8.8415 7.4460\n",
"\n",
"ans =\n",
"\n",
" 41.5613 36.2130 -49.8356\n",
" -30.1783 -24.2201 36.1767\n",
" -3.8467 -8.8415 7.4460\n",
"\n"
]
}
],
"source": [
"inv(A*B)\n",
"inv(B)*inv(A)\n",
"\n",
"inv(A')\n",
"\n",
"inv(A)'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Orthogonal Matrices\n",
"\n",
"Vectors are *orthogonal* if $x^{T}$ y=0, and a vector is *normalized* if $||x||_{2}=1$. A\n",
"square matrix is *orthogonal* if all its column vectors are orthogonal to each other and\n",
"normalized. The column vectors are then called *orthonormal* and the following results\n",
"\n",
"$U^{T}U=I=UU^{T}$\n",
"\n",
"and \n",
"\n",
"$||Ux||_{2}=||x||_{2}$\n",
"\n",
"### Determinant\n",
"\n",
"The **determinant** of a matrix has 3 properties\n",
"\n",
"1. The determinant of the identity matrix is one, $|I|=1$\n",
"\n",
"2. If you multiply a single row by scalar $t$ then the determinant is $t|A|$:\n",
"\n",
"$t|A|=\\left[ \\begin{array}{cccc}\n",
"tA_{11} & tA_{12} &...& tA_{1N} \\\\\n",
"A_{21} & A_{22} &...& A_{2N} \\\\\n",
"\\vdots & \\vdots &\\ddots& \\vdots \\\\\n",
"A_{M1} & A_{M2} &...& A_{MN}\\end{array} \\right]$\n",
"\n",
"3. If you switch 2 rows, the determinant changes sign:\n",
"\n",
"\n",
"$-|A|=\\left[ \\begin{array}{cccc}\n",
"A_{21} & A_{22} &...& A_{2N} \\\\\n",
"A_{11} & A_{12} &...& A_{1N} \\\\\n",
"\\vdots & \\vdots &\\ddots& \\vdots \\\\\n",
"A_{M1} & A_{M2} &...& A_{MN}\\end{array} \\right]$\n",
"\n",
"4. inverse of the determinant is the determinant of the inverse:\n",
"\n",
"$|A^{-1}|=\\frac{1}{|A|}=|A|^{-1}$\n",
"\n",
"For a $2\\times2$ matrix, \n",
"\n",
"$|A|=\\left|\\left[ \\begin{array}{cc}\n",
"A_{11} & A_{12} \\\\\n",
"A_{21} & A_{22} \\\\\n",
"\\end{array} \\right]\\right| = A_{11}A_{22}-A_{21}A_{12}$\n",
"\n",
"For a $3\\times3$ matrix,\n",
"\n",
"$|A|=\\left|\\left[ \\begin{array}{ccc}\n",
"A_{11} & A_{12} & A_{13} \\\\\n",
"A_{21} & A_{22} & A_{23} \\\\\n",
"A_{31} & A_{32} & A_{33}\\end{array} \\right]\\right|=$\n",
"\n",
"$A_{11}A_{22}A_{33}+A_{12}A_{23}A_{31}+A_{13}A_{21}A_{32}\n",
"-A_{31}A_{22}A_{13}-A_{32}A_{23}A_{11}-A_{33}A_{21}A_{12}$\n",
"\n",
"For larger matrices, the determinant is \n",
"\n",
"$|A|=\n",
"\\frac{1}{n!}\n",
"\\sum_{i_{r}}^{N}\n",
"\\sum_{j_{r}}^{N}\n",
"\\epsilon_{i_{1}...i_{N}}\n",
"\\epsilon_{j_{1}...j_{N}}\n",
"A_{i_{1}j_{1}}\n",
"A_{i_{2}j_{2}}\n",
"...\n",
"A_{i_{N}j_{N}}$\n",
"\n",
"Where the Levi-Cevita symbol is $\\epsilon_{i_{1}i_{2}...i_{N}} = 1$ if it is an even\n",
"permutation and $\\epsilon_{i_{1}i_{2}...i_{N}} = -1$ if it is an odd permutation and\n",
"$\\epsilon_{i_{1}i_{2}...i_{N}} = 0$ if $i_{n}=i_{m}$ e.g. $i_{1}=i_{7}$\n",
"\n",
"![Levi-Cevita rule for even and odd permutations for an $N\\times N$\n",
"determinant](even_odd_perm.svg)\n",
"\n",
"So a $4\\times4$ matrix determinant,\n",
"\n",
"$|A|=\\left|\\left[ \\begin{array}{cccc}\n",
"A_{11} & A_{12} & A_{13} & A_{14} \\\\\n",
"A_{21} & A_{22} & A_{23} & A_{24} \\\\\n",
"A_{31} & A_{32} & A_{33} & A_{34} \\\\\n",
"A_{41} & A_{42} & A_{43} & A_{44} \\end{array} \\right]\\right|$\n",
"\n",
"$=\\epsilon_{1234}\\epsilon_{1234}A_{11}A_{22}A_{33}A_{44}+\n",
"\\epsilon_{2341}\\epsilon_{1234}A_{21}A_{32}A_{43}A_{14}+\n",
"\\epsilon_{3412}\\epsilon_{1234}A_{31}A_{42}A_{13}A_{24}+\n",
"\\epsilon_{4123}\\epsilon_{1234}A_{41}A_{12}A_{23}A_{34}+...\n",
"\\epsilon_{4321}\\epsilon_{4321}A_{44}A_{33}A_{22}A_{11}$\n",
"\n",
"### Special Case for determinants\n",
"\n",
"The determinant of a diagonal matrix $|D|=D_{11}D_{22}D_{33}...D_{NN}$. \n",
"\n",
"Similarly, if a matrix is upper triangular (so all values of $A_{ij}=0$ when $j<i$), the\n",
"determinant is \n",
"\n",
"$|A|=\\left|\\left[ \\begin{array}{cccc}\n",
"A_{11} & A_{12} &...& A_{1N} \\\\\n",
"0 & A_{22} &...& A_{2N} \\\\\n",
"0 & 0 &\\ddots & \\vdots \\\\\n",
"0 & 0 &...& A_{NN}\\end{array} \\right]\\right|=A_{11}A_{22}A_{33}...A_{NN}$"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"D =\n",
"\n",
" 1 0 0 0\n",
" 0 2 0 0\n",
" 0 0 3 0\n",
" 0 0 0 4\n",
"\n",
"A =\n",
"\n",
" 1 2 3 4\n",
" 0 2 3 4\n",
" 0 0 3 4\n",
" 0 0 0 4\n",
"\n"
]
}
],
"source": [
"D=[1,0,0,0;0,2,0,0;0,0,3,0;0,0,0,4]\n",
"A=[1,2,3,4;0,2,3,4;0,0,3,4;0,0,0,4] % upper triangular matrix (4x4)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans = 24\n",
"ans = 24\n"
]
}
],
"source": [
"det(D)\n",
"det(A)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A =\n",
"\n",
" 2 4 6 8\n",
" 0 2 3 4\n",
" 0 0 3 4\n",
" 0 0 0 4\n",
"\n"
]
}
],
"source": [
"A(1,:)=2*A(1,:)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans = 48\r\n"
]
}
],
"source": [
"det(A)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans = 0.020833\r\n"
]
}
],
"source": [
"det(inv(A))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans = 0.020833\r\n"
]
}
],
"source": [
"1/48"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r1 =\n",
"\n",
" 2 4 6 8\n",
"\n",
"r2 =\n",
"\n",
" 0 2 3 4\n",
"\n",
"A =\n",
"\n",
" 0 2 3 4\n",
" 0 2 3 4\n",
" 0 0 3 4\n",
" 0 0 0 4\n",
"\n",
"A =\n",
"\n",
" 0 2 3 4\n",
" 2 4 6 8\n",
" 0 0 3 4\n",
" 0 0 0 4\n",
"\n"
]
}
],
"source": [
"r1=A(1,:)\n",
"r2=A(2,:)\n",
"A(1,:)=r2\n",
"A(2,:)=r1"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans = -48\r\n"
]
}
],
"source": [
"det(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Working with matrices (or arrays)\n",
"\n",
"When you need a row of your matrix, use `A(1,:)`\n",
"\n",
"When you need a column of your matrix, use `A(:,1)`"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"B =\n",
"\n",
" Columns 1 through 6:\n",
"\n",
" 9.3678e-02 6.6083e-01 8.6163e-01 2.4581e-01 3.2483e-01 6.9693e-01\n",
" 4.5401e-01 5.6867e-01 1.4516e-01 9.5440e-02 6.8525e-01 6.1453e-03\n",
" 5.4096e-01 9.6443e-01 8.2454e-02 1.8644e-01 7.0369e-01 2.9403e-03\n",
" 8.3509e-01 1.1006e-01 5.8880e-01 4.9634e-01 9.3148e-02 8.3110e-01\n",
" 5.9499e-01 8.8513e-01 6.5640e-01 5.3244e-01 2.9278e-01 7.6347e-02\n",
" 1.5456e-01 3.1275e-01 4.5873e-01 7.2297e-01 3.6996e-01 1.2478e-01\n",
" 4.7960e-01 1.8491e-01 6.1816e-01 3.3450e-01 5.9307e-01 8.9796e-01\n",
" 9.2872e-01 3.8687e-01 8.3991e-01 8.8064e-01 6.0058e-01 3.5300e-01\n",
" 7.3063e-01 2.0543e-01 7.0693e-01 5.0973e-01 7.1647e-01 1.3979e-01\n",
" 2.3365e-01 3.5988e-01 4.8453e-01 9.6969e-01 9.8916e-01 2.7608e-02\n",
"\n",
" Columns 7 and 8:\n",
"\n",
" 8.8934e-01 1.7184e-01\n",
" 2.5948e-03 7.2273e-01\n",
" 1.1076e-01 9.3209e-01\n",
" 8.3082e-01 3.6414e-01\n",
" 9.6406e-01 2.4431e-01\n",
" 2.6633e-04 9.9584e-01\n",
" 4.4733e-01 4.2158e-01\n",
" 1.2252e-01 8.7513e-01\n",
" 5.2620e-01 8.7209e-01\n",
" 5.8721e-02 9.7262e-01\n",
"\n"
]
}
],
"source": [
"B=rand(10,8)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans =\n",
"\n",
" 0.861626\n",
" 0.145156\n",
" 0.082454\n",
" 0.588799\n",
" 0.656403\n",
" 0.458729\n",
" 0.618158\n",
" 0.839911\n",
" 0.706933\n",
" 0.484532\n",
"\n"
]
}
],
"source": [
"B(:,3) % print 3 column of matrix B"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans =\n",
"\n",
" Columns 1 through 7:\n",
"\n",
" 0.594987 0.885135 0.656403 0.532440 0.292784 0.076347 0.964064\n",
"\n",
" Column 8:\n",
"\n",
" 0.244310\n",
"\n"
]
}
],
"source": [
"B(5,:)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How to determine, determinant numerically?\n",
"'' , inverse of matrices\n",
"'' , solutions , what is x_1, x_2, ..."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Octave",
"language": "octave",
"name": "octave"
},
"language_info": {
"file_extension": ".m",
"help_links": [
{
"text": "MetaKernel Magics",
"url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
}
],
"mimetype": "text/x-octave",
"name": "octave",
"version": "0.19.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}