{ "metadata": { "kernelspec": { "codemirror_mode": { "name": "ipython", "version": 2 }, "display_name": "IPython (Python 2)", "language": "python", "name": "python2" }, "language": "Julia", "name": "", "signature": "sha256:9b58d858acf29fd3f7c821ba368cb897ff368d3b58432a13a0b4b1752bfc032e" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "### This code needs to be executed with shift enter to activate autograding\n", "\n", "Pkg.rm(\"Homework\")\n", "Pkg.clone(\"git://github.com/shashi/Homework.jl.git\")\n", "\n", "using Homework\n", "Homework.configure({\"host\" => \"https://juliabox.org\", \"course\" => \"MIT-18.06\", \"problem_set\" => \"pset2\"})\n", "\n", "using SymPy\n", "a,b,c,r,s,t=@syms a b c r s t\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Problem 1. LU Decomposition" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2 pts.) We would like to find the LU decomposition of A=\n", "$\\left( \\begin{matrix}\n", "a & r & r & r \\\\\n", "a & b & s & s \\\\\n", "a & b & c & t \\\\\n", "a & b & c & d\n", "\\end{matrix} \\right) $.\n", "\n", "Find the four conditions on a,b,c,d,r,s,t so that A=LU above has four pivots.\n", "\n", "Your expressions can involve any of the variables: a,b,c,d,r,s,t. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "a \u2260 (a is not equal to)" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "1" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b \u2260 (b is not equal to)" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "2" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "c \u2260 (c is not equal to)" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "3" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "d \u2260 (d is not equal to)" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "4" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "LU Decompositon 2
\n", "\n", "(2 pts.) For the same problem above, write down L below as a numerical matrix.\n", "example
L=[1 2 3 4
\n", " 5 6 7 8
\n", " 9 10 11 12
\n", " 13 14 15 16]" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L=" ], "language": "python", "metadata": { "question": "5" }, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Problem 2 Transposes and Inverses" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2pts)\n", "Find $A^T$ and $A^{-1}$ and $(A^{-1})^T$ and $(A^T)^{-1}$ for\n", "\n", "$A=\\left( \\begin{matrix} 1 & 0 \\\\ 9 & 3 \\end{matrix}\\right)$\n", "\n", "Enter your answer $ \\begin{pmatrix} a&b \\\\c&d \\end{pmatrix}$ in the form [a b ; c d]. Enter fractions with two slashes (example 3//4) for now. Julia has an exact rational format. In the next pset we will allow decimals with tolerances." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$A^T=$" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "6" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$A^{-1}=$" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "7" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$(A^{-1})^T=$" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "8" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$(A^T)^{-1}=$" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "9" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2 pts) TRANSPOSES AND INVERSES" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find $A^T$ and $A^{\u22121}$ and $(A^{\u22121})^T$ and $(A^T)^{\u22121}$ for\n", "$A=\n", "\\begin{pmatrix}\n", "1 & c \\\\ c & 0\n", "\\end{pmatrix}.$\n", "\n", "Enter your answer $ \\begin{pmatrix} a&b \\\\c&d \\end{pmatrix}$ in the form [a b ; c d]. Enter fractions with two slashes (example 3//4) for now. Julia has an exact rational format. In the next pset we will allow decimals with tolerances. Use the carrot (shift 6) for squares. (Note: it is not recommended to use Julia to get the answer. Rather use scrap paper.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$A^T=$" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "10" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$A^{-1}=$" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "11" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$(A^{-1})^T=$" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "12" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$(A^T)^{-1}=$" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "13" }, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Problem 3 Symmetric Matrices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2 pts.) Which of these are true?
\n", "\n", "1. The block matrix\n", "$\\begin{pmatrix} 0 & A\\\\ A & 0 \\end{pmatrix}$\n", "is automatically symmetric.
\n", "\n", "2. If A and B are symmetric then their product AB is symmetric.
\n", "\n", "3. If A is not symmetric then A^{\u22121} is not symmetric.
\n", "\n", "4. When A,B,C are symmetric, the transpose of ABC is CBA
\n", "\n", "Write your answer as a vector of numbers corresponding to true, example [1, 3] means that 1 and 3 are true,and 2 and 4 are false.\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "14" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SYMMETRIC MATRICES 2
\n", "\n", "(2 pts.) If $A=A^T$ and $B=B^T$, which of these matrices are certainly symmetric?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.$A^2\u2212B^2$
2. $(A+B)(A\u2212B)$
3. $ABA$
4. $ABAB$\n", "\n", "Write your answer as a vector of numbers corresponding to certainly symmetric, example [1, 3] means that 1 and 3 are certainly symmetric." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "15" }, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Problem 4. Subspaces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "( 4 pts.) Which of the following subsets of $R^3$ are actually subspaces?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. The plane of vectors (b1,b2,b3) with b1=b2
\n", "2. The plane of vectors with b1=1
\n", "3. The vectors with b1 b2 b3 = 0
\n", "4. All linear combinations of v=(1,4,0) and w=(2,2,2)
\n", "5. All vectors that satisfy b1+b2+b3=0 \n", "6. All vectors with b1\u2264b2\u2264b3.\n", "\n", "Write your answer as a vector of numbers corresponding to which are subspaces, example [1, 3] means that 1 and 3 are subspaces." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "16" }, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Problem 5. Matrix Subspaces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2 pts.) Remember (from Chapter 3.1) that all two-by-two matrices form a vector space, which we denote M.\n", "\n", "True or false (select which of the following are true). Recommendation: check addition in each case by an example.\n", "\n", "1. The symmetric matrices in M (with $A^T=A$) form a subspace.
\n", "2. The skew-symmetric matrices in M (with $A^T=\u2212A$) form a subspace.
\n", "3. The un-symmetric matrices in M (with $A^T\u2260A$) form a subspace\n", "\n", "Write your answer as a vector of numbers corresponding to which are a vector space, example [1, 3] means that 1 and 3 are vector spaces." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "17" }, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Problem 6. Special Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "( 4 pts.) For the matrix \n", "$A=\\begin{pmatrix}\n", "1 & 2 & 2 & 4 & 6 \\\\\n", "1 & 2 & 3 & 6 & 9 \\\\\n", "0 & 0 & 1 & 2 & 3\n", "\\end{pmatrix}$\n", " find a special solution for every free variable. \n", "(Set one free variable to 1. Set the other free variables to zero.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the special solutions by columns [[a,b,c,d,e] [f,g,h,i,j] [k,l,m,n,o]] in order of the free variables.\n", "(If you see bounds error, make sure you put a space between right brackets and left brackets)" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "18" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the matrix\n", "$B=\\begin{pmatrix}\n", "2 & 4 & 4 \\\\\n", "0 & 4 & 4\\\\\n", "0 & 8 & 8\n", "\\end{pmatrix}\n", "$\n", "find a special solution for every free variable. (Set one free variable to 1. Set the other free variables to zero.)\n", "\n", "Enter the special solutions more or less as above." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "19" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(3 pts.) RREF and Nullspace" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the reduced row echelon form $R_B$ of\n", "$B=\\begin{pmatrix}\n", "2 & 4 & 4 \\\\\n", "0 & 4 & 4\\\\\n", "0 & 8 & 8\n", "\\end{pmatrix}\n", "$\n", "\n", "In Julia a matrix\n", "$\\begin{pmatrix} a & b & c \\\\ d & e & f \\\\ g & h & i \\end{pmatrix}$ can be entered\n", "as
\n", "[ a b c
\n", " d e f
\n", " g h i]
\n", "or [ a b c;d e f;g h i]
\n", "or by columns [ [a,d,g] [b,e,h] [c,f,i]]" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "20" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the reduced row echelon form $R_A$ of\n", "$A=\\begin{pmatrix}\n", "1 & 2 & 2 & 4 & 6 \\\\\n", "1 & 2 & 3 & 6 & 9 \\\\\n", "0 & 0 & 1 & 2 & 3\n", "\\end{pmatrix}\n", "$" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "21" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is the nullspace of $R_A$ equal to the nullspace of A?
\n", "Type true or false" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "22" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(3 pts.) PIVOTS AND FREE VARIABLES (type true or false for each of these problems)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A square matrix has no free variables" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "23" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An invertible matrix has no free variables" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "24" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An m by n matrix has no more than n pivot variables" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "25" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An m by n matrix has no more than m pivot variable" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "26" }, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "JULIA PROBLEM 1: LU Decomposition" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Julia one can type L,U,p = lu(A) for the LU decomposition. Here L is lower triangular, U is upper triangular,\n", "and p is a permutation vector. (Storing a permutation vector on a computer is much more sensible than storing\n", "a large permutation matrix!)\n", "\n", "One can learn more by typing
\n", "? lu\n", "\n", "(2 pts.) Write three lines of Julia.
\n", "First line: take A=[1 2 3; 4 5 6; 7 7 7].
\n", "Second line: obtain L, U, and p.
\n", "Third Line: write an expression that combines L,U, p that gives a 0 matrix.\n" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "question": "27" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is another command that we will not explore right now: lufact, which stores L and U in half the memory\n", "(as half the numbers are 0!). If you are curious if z=lufact(A), then z[:L] displays L for humans, z[:U] displays U,\n", "and z[:P] displays a permutation matrix, but never stores it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# JULIA PROBLEM 2: Symmetric Matrices and Diffusion (10 pts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "

\n", "

Symmetric matrices are important because they appear in the real world \n", " all the time. One such place is in numerically solving a model for diffusion.\n", "

\n", "

\n", " The following partial differential equation models diffusion, and \n", " is known as the heat equation.

\n", "$$\\frac{\\partial}{\\partial t} c(x,t)=\\frac{\\partial^2}\n", " {\\partial x^2}c(x,t) $$

\n", "

This equation models any sort of diffusion: the diffusion of dye into a \n", " liquid, or the diffusion of heat through a solid. Note that we have omitted constants\n", " required to guarantee the balance of units for simplicity here.

\n", "

\n", "

To use linear algebra to solve this equation, we need to change this \n", " differential equation into a difference equation.\n", " The partial derivative with respect to $t$\n", " can be approximated as a difference equation as follows:

\n", "

$$\\frac{\\partial}{\\partial t} c(x,t) \n", " \\approx \\frac{c(x,t+\\Delta t) - c(x,t)}{\\Delta t}$$

\n", "

The second derivative can be approximated as the following difference equation:

\n", "

$$\\frac{\\partial^2}{\\partial x^2}c(x,t) \n", " \\approx \\cfrac{\\cfrac{c(x+\\Delta x,t) - c(x,t)}{\\Delta x}- \n", " \\cfrac{c(x,t) - c(x-\\Delta x,t)}{\\Delta x}}{\\Delta x} = \\cfrac{c(x+\\Delta x,t) \n", " - 2c(x,t)+ c(x-\\Delta x,t)}{(\\Delta x)^2}$$

\n", "

We can rearrange this difference equation to find an expression \n", " at the grid point x at the time step $t + \\Delta t$ as \n", " follows:

\n", "

$$c(x,t+\\Delta t) = c(x,t) + \\frac{\\Delta t}{(\\Delta x)^2}\n", " (c(x+\\Delta x,t)-2c(x,t)+c(x-\\Delta x,t))$$

\n", "

Notice that every term on the right hand side only depends \n", " on the values at time step t. So we can rewrite this as a matrix \n", " equation, in terms of a vector c(t) whose entries are points \n", " $x$ separated\n", " by $\\Delta x$

\n", "

$$ c(x,t + \\Delta t) \\approx c(x,t) + \\Delta t \\frac{\\partial}{\\partial t}c(x,t) = c(x,t) + \\Delta t \\frac{\\partial^2}{\\partial x^2}c(x,t).$$

\n", "

$$ c(t + \\Delta t) = (I - \\frac{\\Delta t}{(\\Delta x)^2}A)*c(t)$$

\n", "

In this problem, we provide you with the code to observe the evolution of this \n", " differential equation, which describes diffusion. We start by discretizing the \n", " variable x, which we represent as a vector of 101 points lying on the \n", " interval from 0 to 1. Thus our meshwidth in space,$\\Delta x$,\n", " is 0.01. We start with an c(x,0) = c is the initial concentration, \n", " which we take to be a Gaussian distribution, or a bell curve.

\n", "

\n", "

\n", " Your job is to create a negative second difference matrix A. And, to plot the \n", " concentration vector C(:,t) for (at least) 3 later times. (See further \n", " instructions within the code body.)\n", " \n", " \n", "

A note about A:\n", " The dimension of A is 101x101. \n", " The matrix A must enforce boundary conditions!!!! Here, this means we will \n", " make the first row and column zero, and the last row and column zero. You can see\n", " what happens in the plot if you don't enforce this condition!\n", " Note that the diagonal of A will be 2, the super and sub diagonals will be -1, \n", " except at the boundary, where all entries will be zero.\n", "

\n", "

$A = \\begin{bmatrix} \n", " 0 & 0 & & \\dots & & & 0 & 0 \\\\\n", " 0 & 2 & -1 & 0 & \\dots & & & \\dots & 0 \\\\\n", " 0 &-1 & 2 & -1 & 0 &\\dots & &\\dots & 0\\\\\n", " 0 & & -1 & 2 & -1 & 0 &\\dots&\\dots & 0\\\\\n", " \\vdots & & &\\ddots & \\ddots & \\ddots & & &\\vdots \\\\\n", " 0 & \\dots & \\dots & 0 & -1 & 2 & -1 & & 0\\\\\n", " 0 &\\dots & &\\dots & 0 & -1 & 2 & -1 & 0\\\\\n", " 0 & \\dots & & & \\dots &0 & -1 & 2 & 0\\\\\n", " 0 & 0 & & & \\dots & & & 0 & 0 \n", " \\end{bmatrix}$\n", "

\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This matrix is symmetric tridiagonal so it would be foolish to store more\n", "than $n+(n-1)=2n-1$ number to represent or compute with $A$.\n", "\n", "First define the diagonal of $A$ as a vector of length 101 by typing\n", "\n", "`d = [0, 2*ones(?), 0]`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d=" ], "language": "python", "metadata": { "question": "28" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next obtain the sub and super diagonal of A as a vector of length 100 by typing
\n", "e = [? , ? , ?]" ] }, { "cell_type": "code", "collapsed": false, "input": [ "e=" ], "language": "python", "metadata": { "question": "29" }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then this wonderful matrix is stored compactly in Julia as
\n", "(Note the display is for humans, only the 201 numbers are stored)\n", "\n", "From here on in there is no autograding, just enjoy the show." ] }, { "cell_type": "code", "collapsed": false, "input": [ "A=SymTridiagonal(d,e)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "## We will do the rest\n", "numx = 101 #number of grid points in x\n", "numt = 10000 #number of time steps to be iterated over \n", "\u0394x = 1/(numx - 1)\n", "\u0394t = 0.00005\n", "x = 0: \u0394x :1 #vector of x values used for plotting and initial conditions\n", " \n", "# initialize to Guassian\n", "\u03bc = 0.5;\n", "\u03c3 = 0.05;\n", "c = exp(-(x-\u03bc).^2/(2*\u03c3^2)) / sqrt(2*\u03c0*\u03c3^2);\n", "c[1]=0;\n", "c[numx]=0;\n", "\n", "# Iterate difference equation - note that c[1] and c[numx] always \n", "#remain 0 for all time steps\n", "#We create a matrix C whose rows are iterative applications of \n", "#the discrete diffusion equation to the vector c\n", "#We must specify initial conditions\n", "C=zeros(numx,numt);\n", "C[:,1]=c; # Initially Gaussian\n", "C[1,1] = 0; #C=0 at x=0 left boundary\n", "C[numx,1] = 0; #C=0 at x=1 right boundary\n", "\n", "for j=1:(numt-1)\n", " C[:,j+1] = C[:,j] -(\u0394t/\u0394x^2)*A*C[:,j]; \n", "end\n", "\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "using Interact\n", "using PyPlot" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "f=figure(figsize=(4,3.5))\n", "@manipulate for iteration = 1:numt; withfig(f) do\n", " plot(x,C[:,iteration])\n", " axis([0,1,0,8])\n", " xlabel(\"x\")\n", " ylabel(\"C\")\n", " title(\"diffusion at time=$(round(iteration*\u0394t,3))\")\n", " \n", " end\n", "end" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enjoy manipulating the diffusion from a large peak at t=0 to near dissipation at t=.5 ! (Nothing further to submit, your main job is to define A, and learn something about how useful linear algebra can be)" ] } ], "metadata": {} } ] }