Skip to content
Permalink
master
Go to file
 
 
Cannot retrieve contributors at this time
699 lines (699 sloc) 29.2 KB
{
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%plot --format svg"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"setdefaults"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is the determinant of A?\n",
"\n",
"$A=\\left[ \\begin{array}{ccc}\n",
"10 & 2 & 1 \\\\\n",
"0 & 1 & 1 \\\\\n",
"0 & 0 & 10\\end{array} \\right]$\n",
"\n",
"![responses to determinant of A](det_A.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LU Decomposition\n",
"### efficient storage of matrices for solutions\n",
"\n",
"Considering the same solution set:\n",
"\n",
"$y=Ax$\n",
"\n",
"Assume that we can perform Gauss elimination and achieve this formula:\n",
"\n",
"$Ux=d$ \n",
"\n",
"Where, $U$ is an upper triangular matrix that we derived from Gauss elimination and $d$ is the set of dependent variables after Gauss elimination. \n",
"\n",
"Assume there is a lower triangular matrix, $L$, with ones on the diagonal and same dimensions of $U$ and the following is true:\n",
"\n",
"$L(Ux-d)=Ax-y=0$\n",
"\n",
"Now, $Ax=LUx$, so $A=LU$, and $y=Ld$.\n",
"\n",
"$2x_{1}+x_{2}=1$\n",
"\n",
"$x_{1}+3x_{2}=1$\n",
"\n",
"\n",
"$\\left[ \\begin{array}{cc}\n",
"2 & 1 \\\\\n",
"1 & 3 \\end{array} \\right]\n",
"\\left[\\begin{array}{c} \n",
"x_{1} \\\\ \n",
"x_{2} \\end{array}\\right]=\n",
"\\left[\\begin{array}{c} \n",
"1 \\\\\n",
"1\\end{array}\\right]$\n",
"\n",
"f21=0.5\n",
"\n",
"A(2,1)=1-1 = 0 \n",
"\n",
"A(2,2)=3-0.5=2.5\n",
"\n",
"y(2)=1-0.5=0.5\n",
"\n",
"$L(Ux-d)=\n",
"\\left[ \\begin{array}{cc}\n",
"1 & 0 \\\\\n",
"0.5 & 1 \\end{array} \\right]\n",
"\\left(\\left[ \\begin{array}{cc}\n",
"2 & 1 \\\\\n",
"0 & 2.5 \\end{array} \\right]\n",
"\\left[\\begin{array}{c} \n",
"x_{1} \\\\ \n",
"x_{2} \\end{array}\\right]-\n",
"\\left[\\begin{array}{c} \n",
"1 \\\\\n",
"0.5\\end{array}\\right]\\right)=0$\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A =\n",
"\n",
" 2 1\n",
" 1 3\n",
"\n",
"L =\n",
"\n",
" 1.00000 0.00000\n",
" 0.50000 1.00000\n",
"\n",
"U =\n",
"\n",
" 2.00000 1.00000\n",
" 0.00000 2.50000\n",
"\n",
"ans =\n",
"\n",
" 2 1\n",
" 1 3\n",
"\n",
"d =\n",
"\n",
" 1.00000\n",
" 0.50000\n",
"\n",
"y =\n",
"\n",
" 1\n",
" 1\n",
"\n"
]
}
],
"source": [
"A=[2,1;1,3]\n",
"L=[1,0;0.5,1]\n",
"U=[2,1;0,2.5]\n",
"L*U\n",
"\n",
"d=[1;0.5]\n",
"y=L*d"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pivoting for LU factorization\n",
"\n",
"LU factorization uses the same method as Gauss elimination so it is also necessary to perform partial pivoting when creating the lower and upper triangular matrices. \n",
"\n",
"Matlab and Octave use pivoting in the command \n",
"\n",
"`[L,U,P]=lu(A)`\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'lu' is a built-in function from the file libinterp/corefcn/lu.cc\n",
"\n",
" -- Built-in Function: [L, U] = lu (A)\n",
" -- Built-in Function: [L, U, P] = lu (A)\n",
" -- Built-in Function: [L, U, P, Q] = lu (S)\n",
" -- Built-in Function: [L, U, P, Q, R] = lu (S)\n",
" -- Built-in Function: [...] = lu (S, THRES)\n",
" -- Built-in Function: Y = lu (...)\n",
" -- Built-in Function: [...] = lu (..., \"vector\")\n",
" Compute the LU decomposition of A.\n",
"\n",
" If A is full subroutines from LAPACK are used and if A is sparse\n",
" then UMFPACK is used.\n",
"\n",
" The result is returned in a permuted form, according to the\n",
" optional return value P. For example, given the matrix 'a = [1, 2;\n",
" 3, 4]',\n",
"\n",
" [l, u, p] = lu (A)\n",
"\n",
" returns\n",
"\n",
" l =\n",
"\n",
" 1.00000 0.00000\n",
" 0.33333 1.00000\n",
"\n",
" u =\n",
"\n",
" 3.00000 4.00000\n",
" 0.00000 0.66667\n",
"\n",
" p =\n",
"\n",
" 0 1\n",
" 1 0\n",
"\n",
" The matrix is not required to be square.\n",
"\n",
" When called with two or three output arguments and a spare input\n",
" matrix, 'lu' does not attempt to perform sparsity preserving column\n",
" permutations. Called with a fourth output argument, the sparsity\n",
" preserving column transformation Q is returned, such that 'P * A *\n",
" Q = L * U'.\n",
"\n",
" Called with a fifth output argument and a sparse input matrix, 'lu'\n",
" attempts to use a scaling factor R on the input matrix such that 'P\n",
" * (R \\ A) * Q = L * U'. This typically leads to a sparser and more\n",
" stable factorization.\n",
"\n",
" An additional input argument THRES, that defines the pivoting\n",
" threshold can be given. THRES can be a scalar, in which case it\n",
" defines the UMFPACK pivoting tolerance for both symmetric and\n",
" unsymmetric cases. If THRES is a 2-element vector, then the first\n",
" element defines the pivoting tolerance for the unsymmetric UMFPACK\n",
" pivoting strategy and the second for the symmetric strategy. By\n",
" default, the values defined by 'spparms' are used ([0.1, 0.001]).\n",
"\n",
" Given the string argument \"vector\", 'lu' returns the values of P\n",
" and Q as vector values, such that for full matrix, 'A (P,:) = L *\n",
" U', and 'R(P,:) * A (:, Q) = L * U'.\n",
"\n",
" With two output arguments, returns the permuted forms of the upper\n",
" and lower triangular matrices, such that 'A = L * U'. With one\n",
" output argument Y, then the matrix returned by the LAPACK routines\n",
" is returned. If the input matrix is sparse then the matrix L is\n",
" embedded into U to give a return value similar to the full case.\n",
" For both full and sparse matrices, 'lu' loses the permutation\n",
" information.\n",
"\n",
" See also: luupdate, ilu, chol, hess, qr, qz, schur, svd.\n",
"\n",
"Additional help for built-in functions and operators is\n",
"available in the online version of the manual. Use the command\n",
"'doc <topic>' to search the manual index.\n",
"\n",
"Help and information about Octave is also available on the WWW\n",
"at http://www.octave.org and via the help@octave.org\n",
"mailing list.\n"
]
}
],
"source": [
"help lu"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg height=\"420px\" viewBox=\"0 0 560 420\" width=\"560px\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"\n",
"<title>Gnuplot</title>\n",
"<desc>Produced by GNUPLOT 5.0 patchlevel 3 </desc>\n",
"\n",
"<g id=\"gnuplot_canvas\">\n",
"\n",
"<rect fill=\"none\" height=\"420\" width=\"560\" x=\"0\" y=\"0\"/>\n",
"<defs>\n",
"\n",
"\t<circle id=\"gpDot\" r=\"0.5\" stroke-width=\"0.5\"/>\n",
"\t<path d=\"M-1,0 h2 M0,-1 v2\" id=\"gpPt0\" stroke=\"currentColor\" stroke-width=\"0.222\"/>\n",
"\t<path d=\"M-1,-1 L1,1 M1,-1 L-1,1\" id=\"gpPt1\" stroke=\"currentColor\" stroke-width=\"0.222\"/>\n",
"\t<path d=\"M-1,0 L1,0 M0,-1 L0,1 M-1,-1 L1,1 M-1,1 L1,-1\" id=\"gpPt2\" stroke=\"currentColor\" stroke-width=\"0.222\"/>\n",
"\t<rect height=\"2\" id=\"gpPt3\" stroke=\"currentColor\" stroke-width=\"0.222\" width=\"2\" x=\"-1\" y=\"-1\"/>\n",
"\t<rect fill=\"currentColor\" height=\"2\" id=\"gpPt4\" stroke=\"currentColor\" stroke-width=\"0.222\" width=\"2\" x=\"-1\" y=\"-1\"/>\n",
"\t<circle cx=\"0\" cy=\"0\" id=\"gpPt5\" r=\"1\" stroke=\"currentColor\" stroke-width=\"0.222\"/>\n",
"\t<use fill=\"currentColor\" id=\"gpPt6\" stroke=\"none\" xlink:href=\"#gpPt5\"/>\n",
"\t<path d=\"M0,-1.33 L-1.33,0.67 L1.33,0.67 z\" id=\"gpPt7\" stroke=\"currentColor\" stroke-width=\"0.222\"/>\n",
"\t<use fill=\"currentColor\" id=\"gpPt8\" stroke=\"none\" xlink:href=\"#gpPt7\"/>\n",
"\t<use id=\"gpPt9\" stroke=\"currentColor\" transform=\"rotate(180)\" xlink:href=\"#gpPt7\"/>\n",
"\t<use fill=\"currentColor\" id=\"gpPt10\" stroke=\"none\" xlink:href=\"#gpPt9\"/>\n",
"\t<use id=\"gpPt11\" stroke=\"currentColor\" transform=\"rotate(45)\" xlink:href=\"#gpPt3\"/>\n",
"\t<use fill=\"currentColor\" id=\"gpPt12\" stroke=\"none\" xlink:href=\"#gpPt11\"/>\n",
"\t<path d=\"M0,1.330 L1.265,0.411 L0.782,-1.067 L-0.782,-1.076 L-1.265,0.411 z\" id=\"gpPt13\" stroke=\"currentColor\" stroke-width=\"0.222\"/>\n",
"\t<use fill=\"currentColor\" id=\"gpPt14\" stroke=\"none\" xlink:href=\"#gpPt13\"/>\n",
"\t<filter filterUnits=\"objectBoundingBox\" height=\"1\" id=\"textbox\" width=\"1\" x=\"0\" y=\"0\">\n",
"\t <feFlood flood-color=\"white\" flood-opacity=\"1\" result=\"bgnd\"/>\n",
"\t <feComposite in=\"SourceGraphic\" in2=\"bgnd\" operator=\"atop\"/>\n",
"\t</filter>\n",
"\t<filter filterUnits=\"objectBoundingBox\" height=\"1\" id=\"greybox\" width=\"1\" x=\"0\" y=\"0\">\n",
"\t <feFlood flood-color=\"lightgrey\" flood-opacity=\"1\" result=\"grey\"/>\n",
"\t <feComposite in=\"SourceGraphic\" in2=\"grey\" operator=\"atop\"/>\n",
"\t</filter>\n",
"</defs>\n",
"<g color=\"white\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"1.00\">\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"1.00\">\n",
"\t<g shape-rendering=\"crispEdges\" stroke=\"none\">\n",
"\t\t<polygon fill=\"rgb(255, 255, 255)\" points=\"78.8,384.0 534.9,384.0 534.9,16.8 78.8,16.8 \"/>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"rgb(255, 255, 255)\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M78.8,384.0 L91.3,384.0 M535.0,384.0 L522.5,384.0 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"end\" transform=\"translate(70.5,390.0)\">\n",
"\t\t<text><tspan font-family=\"{}\">0</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M78.8,322.8 L91.3,322.8 M535.0,322.8 L522.5,322.8 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"end\" transform=\"translate(70.5,328.8)\">\n",
"\t\t<text><tspan font-family=\"{}\">5e-05</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M78.8,261.6 L91.3,261.6 M535.0,261.6 L522.5,261.6 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"end\" transform=\"translate(70.5,267.6)\">\n",
"\t\t<text><tspan font-family=\"{}\">0.0001</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M78.8,200.3 L91.3,200.3 M535.0,200.3 L522.5,200.3 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"end\" transform=\"translate(70.5,206.3)\">\n",
"\t\t<text><tspan font-family=\"{}\">0.00015</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M78.8,139.1 L91.3,139.1 M535.0,139.1 L522.5,139.1 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"end\" transform=\"translate(70.5,145.1)\">\n",
"\t\t<text><tspan font-family=\"{}\">0.0002</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M78.8,77.9 L91.3,77.9 M535.0,77.9 L522.5,77.9 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"end\" transform=\"translate(70.5,83.9)\">\n",
"\t\t<text><tspan font-family=\"{}\">0.00025</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M78.8,16.7 L91.3,16.7 M535.0,16.7 L522.5,16.7 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"end\" transform=\"translate(70.5,22.7)\">\n",
"\t\t<text><tspan font-family=\"{}\">0.0003</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M78.8,384.0 L78.8,371.5 M78.8,16.7 L78.8,29.2 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"middle\" transform=\"translate(78.8,408.0)\">\n",
"\t\t<text><tspan font-family=\"{}\">0</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M170.0,384.0 L170.0,371.5 M170.0,16.7 L170.0,29.2 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"middle\" transform=\"translate(170.0,408.0)\">\n",
"\t\t<text><tspan font-family=\"{}\">20</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M261.3,384.0 L261.3,371.5 M261.3,16.7 L261.3,29.2 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"middle\" transform=\"translate(261.3,408.0)\">\n",
"\t\t<text><tspan font-family=\"{}\">40</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M352.5,384.0 L352.5,371.5 M352.5,16.7 L352.5,29.2 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"middle\" transform=\"translate(352.5,408.0)\">\n",
"\t\t<text><tspan font-family=\"{}\">60</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M443.8,384.0 L443.8,371.5 M443.8,16.7 L443.8,29.2 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"middle\" transform=\"translate(443.8,408.0)\">\n",
"\t\t<text><tspan font-family=\"{}\">80</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M535.0,384.0 L535.0,371.5 M535.0,16.7 L535.0,29.2 \" stroke=\"black\"/>\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"middle\" transform=\"translate(535.0,408.0)\">\n",
"\t\t<text><tspan font-family=\"{}\">100</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"\t<path d=\"M78.8,16.7 L78.8,384.0 L535.0,384.0 L535.0,16.7 L78.8,16.7 Z \" stroke=\"black\"/></g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"black\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"1.00\">\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"1.00\">\n",
"\t<path d=\"M349.7,121.7 L349.7,25.7 L526.7,25.7 L526.7,121.7 L349.7,121.7 Z \" stroke=\"black\"/></g>\n",
"\t<g id=\"gnuplot_plot_1a\"><title>LU decomp</title>\n",
"<g color=\"white\" fill=\"none\" stroke=\"black\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"3.00\">\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"3.00\">\n",
"\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"end\" transform=\"translate(526.7,55.7)\">\n",
"\t\t<text><tspan font-family=\"{}\">LU decomp</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"3.00\">\n",
"\t<path d=\"M360.9,49.7 L414.7,49.7 M83.4,330.3 L87.9,244.5 L92.5,289.7 L97.0,292.1 L101.6,288.5 L106.2,289.7 L110.7,287.1 L115.3,283.6 L119.9,279.8 L124.4,276.3 L129.0,276.3 L133.5,265.5 L138.1,269.0 L142.7,266.4 L147.2,261.7 L151.8,261.7 L156.4,271.3 L160.9,269.9 L165.5,274.8 L170.0,272.5 L174.6,268.7 L179.2,212.7 L183.7,256.7 L188.3,261.4 L192.9,260.2 L197.4,234.5 L202.0,270.2 L206.5,269.0 L211.1,249.4 L215.7,265.2 L220.2,303.1 L224.8,298.2 L229.3,300.8 L233.9,299.6 L238.5,292.3 L243.0,299.3 L247.6,298.2 L252.2,294.7 L256.7,284.8 L261.3,282.4 L265.8,306.9 L270.4,297.0 L275.0,303.1 L279.5,306.9 L284.1,299.3 L288.7,283.6 L293.2,294.4 L297.8,295.8 L302.3,292.3 L306.9,292.1 L311.5,283.6 L316.0,287.4 L320.6,286.2 L325.1,272.8 L329.7,293.2 L334.3,292.1 L338.8,294.7 L343.4,293.2 L348.0,292.1 L352.5,302.0 L357.1,302.0 L361.6,302.0 L366.2,299.6 L370.8,293.2 L375.3,277.5 L379.9,294.7 L384.5,292.1 L389.0,289.7 L393.6,289.7 L398.1,288.5 L402.7,286.2 L407.3,288.5 L411.8,234.5 L416.4,285.9 L421.0,282.4 L425.5,241.8 L430.1,241.8 L434.6,279.8 L439.2,266.4 L443.8,280.1 L448.3,259.1 L452.9,229.6 L457.4,277.7 L462.0,283.6 L466.6,276.3 L471.1,278.6 L475.7,277.5 L480.3,277.5 L484.8,274.8 L489.4,265.2 L493.9,270.2 L498.5,272.5 L503.1,259.1 L507.6,264.0 L512.2,264.0 L516.8,266.4 L521.3,261.4 L525.9,264.0 L530.4,247.1 L535.0,259.1 \" stroke=\"rgb( 0, 0, 255)\"/></g>\n",
"\t</g>\n",
"\t<g id=\"gnuplot_plot_2a\"><title>Octave \\\\</title>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"3.00\">\n",
"\t<g fill=\"rgb(0,0,0)\" font-family=\"{}\" font-size=\"16.00\" stroke=\"none\" text-anchor=\"end\" transform=\"translate(526.7,103.7)\">\n",
"\t\t<text><tspan font-family=\"{}\">Octave \\</tspan></text>\n",
"\t</g>\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"3.00\">\n",
"\t<path d=\"M360.9,97.7 L414.7,97.7 M83.4,359.5 L87.9,287.4 L92.5,306.6 L97.0,311.9 L101.6,305.8 L106.2,306.9 L110.7,305.5 L115.3,303.1 L119.9,293.2 L124.4,292.3 L129.0,262.6 L133.5,278.6 L138.1,272.8 L142.7,232.2 L147.2,265.2 L151.8,260.2 L156.4,275.1 L160.9,238.3 L165.5,231.9 L170.0,240.7 L174.6,221.1 L179.2,229.9 L183.7,221.1 L188.3,218.8 L192.9,198.9 L197.4,185.8 L202.0,218.8 L206.5,221.1 L211.1,216.2 L215.7,213.8 L220.2,265.2 L224.8,265.2 L229.3,269.9 L233.9,254.1 L238.5,256.7 L243.0,256.7 L247.6,249.1 L252.2,247.1 L256.7,241.8 L261.3,204.2 L265.8,265.2 L270.4,262.9 L275.0,243.3 L279.5,264.0 L284.1,252.9 L288.7,249.4 L293.2,247.1 L297.8,245.6 L302.3,241.0 L306.9,229.9 L311.5,205.4 L316.0,229.9 L320.6,205.1 L325.1,222.3 L329.7,241.0 L334.3,244.2 L338.8,238.3 L343.4,227.2 L348.0,218.5 L352.5,250.6 L357.1,250.6 L361.6,246.8 L366.2,232.2 L370.8,240.7 L375.3,238.3 L379.9,220.0 L384.5,207.7 L389.0,222.6 L393.6,217.6 L398.1,215.0 L402.7,212.7 L407.3,213.8 L411.8,223.5 L416.4,208.9 L421.0,205.4 L425.5,196.6 L430.1,184.6 L434.6,190.5 L439.2,195.4 L443.8,185.8 L448.3,177.0 L452.9,183.2 L457.4,180.8 L462.0,188.1 L466.6,164.8 L471.1,164.8 L475.7,175.9 L480.3,175.9 L484.8,169.7 L489.4,165.9 L493.9,161.0 L498.5,75.5 L503.1,150.2 L507.6,135.3 L512.2,150.2 L516.8,145.5 L521.3,147.6 L525.9,134.1 L530.4,126.8 L535.0,135.3 \" stroke=\"rgb( 0, 128, 0)\"/></g>\n",
"\t</g>\n",
"<g color=\"white\" fill=\"none\" stroke=\"rgb( 0, 128, 0)\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"2.00\">\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"2.00\">\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"black\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"</g>\n",
"<g color=\"black\" fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-width=\"0.50\">\n",
"</g>\n",
"</g>\n",
"</svg>"
],
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"% time LU solution vs backslash\n",
"t_lu=zeros(100,1);\n",
"t_bs=zeros(100,1);\n",
"for N=1:100\n",
" A=rand(N,N);\n",
" y=rand(N,1);\n",
" [L,U,P]=lu(A);\n",
"\n",
" tic; d=L\\y; x=U\\d; t_lu(N)=toc;\n",
"\n",
" tic; x=A\\y; t_bs(N)=toc;\n",
"end\n",
"plot([1:100],t_lu,[1:100],t_bs) \n",
"legend('LU decomp','Octave \\\\')"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"Consider the problem again from the intro to Linear Algebra, 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](../lecture_09/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": 24,
"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"
]
}
],
"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]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This matrix, K, is symmetric. \n",
"\n",
"`K(i,j)==K(j,i)`\n",
"\n",
"Now we can use,\n",
"\n",
"## Cholesky Factorization\n",
"\n",
"We can decompose the matrix, K into two matrices, $U$ and $U^{T}$, where\n",
"\n",
"$K=U^{T}U$\n",
"\n",
"each of the components of U can be calculated with the following equations:\n",
"\n",
"$u_{ii}=\\sqrt{a_{ii}-\\sum_{k=1}^{i-1}u_{ki}^{2}}$\n",
"\n",
"$u_{ij}=\\frac{a_{ij}-\\sum_{k=1}^{i-1}u_{ki}u_{kj}}{u_{ii}}$\n",
"\n",
"so for K"
]
},
{
"cell_type": "code",
"execution_count": 25,
"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"
]
}
],
"source": [
"K"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"u11 = 4.4721\n",
"u12 = -2.2361\n",
"u13 = 0\n",
"u14 = 0\n",
"u22 = 3.8730\n",
"u23 = -2.5820\n",
"u24 = 0\n",
"u33 = 3.6515\n",
"u34 = -2.7386\n",
"u44 = 1.5811\n",
"U =\n",
"\n",
" 4.47214 -2.23607 0.00000 0.00000\n",
" 0.00000 3.87298 -2.58199 0.00000\n",
" 0.00000 0.00000 3.65148 -2.73861\n",
" 0.00000 0.00000 0.00000 1.58114\n",
"\n"
]
}
],
"source": [
"u11=sqrt(K(1,1))\n",
"u12=(K(1,2))/u11\n",
"u13=(K(1,3))/u11\n",
"u14=(K(1,4))/u11\n",
"u22=sqrt(K(2,2)-u12^2)\n",
"u23=(K(2,3)-u12*u13)/u22\n",
"u24=(K(2,4)-u12*u14)/u22\n",
"u33=sqrt(K(3,3)-u13^2-u23^2)\n",
"u34=(K(3,4)-u13*u14-u23*u24)/u33\n",
"u44=sqrt(K(4,4)-u14^2-u24^2-u34^2)\n",
"U=[u11,u12,u13,u14;0,u22,u23,u24;0,0,u33,u34;0,0,0,u44]"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ans =\n",
"\n",
" 20.00000 -10.00000 0.00000 0.00000\n",
" -10.00000 20.00000 -10.00000 0.00000\n",
" 0.00000 -10.00000 20.00000 -10.00000\n",
" 0.00000 0.00000 -10.00000 10.00000\n",
"\n"
]
}
],
"source": [
"U'*U"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"average time spent for Cholesky factored solution = 1.623154e-05+/-1.166726e-05\n",
"average time spent for backslash solution = 1.675844e-05+/-1.187234e-05\n"
]
}
],
"source": [
"% time solution for Cholesky vs backslash\n",
"t_chol=zeros(1000,1);\n",
"t_bs=zeros(1000,1);\n",
"for i=1:1000\n",
" tic; d=U'*y; x=U\\d; t_chol(i)=toc;\n",
" tic; x=K\\y; t_bs(i)=toc;\n",
"end\n",
"fprintf('average time spent for Cholesky factored solution = %e+/-%e',mean(t_chol),std(t_chol))\n",
"\n",
"fprintf('average time spent for backslash solution = %e+/-%e',mean(t_bs),std(t_bs))"
]
}
],
"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
}
You can’t perform that action at this time.