{ "metadata": { "kernelspec": { "codemirror_mode": "julia", "display_name": "Julia", "language": "julia", "name": "julia" }, "language": "Julia", "name": "", "signature": "sha256:9ed71d41de2faf793e81db76b0364373989c9ff90fe2b754c2e3c9f9aa0a48f3" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Problem 1. Vectors. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(a) (1 pt) SUM OF VECTORS: What is the sum V of the twelve unit vectors that go from the center of a clock to the hours 1:00, 2:00, ..., 12:00? Write down your answer as a pair of coordinates, correct up to .01." ] }, { "cell_type": "code", "collapsed": false, "input": [ "z=[ , ]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "(b) (1 pt) (SUBTRACTING VECTORS) In the sum of vectors above, if the 2:00 vector is removed, you can show that the remaining vectors add to 8:00. This is because subtracting a vector is the same as adding \n", "  ?
\n", "Write your answer as a number from 1 through 4 below:
\n", "1. An Opposite Vector\n", "2. An Orthogonal Vector\n", "3. A Parallel Vector\n", "4. Zero" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(c) (2 pts) (COMPONENTS) What are the components of the 2:00 vector, v=cos \u03b8,sin \u03b8?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "v=[ , ]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Problem 2. Elimination sometimes breaks down" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2 pts) We would like you to use elimination to solve the following linear system of two equations:\n", "\n", "ax + 3y = \u22123
\n", "4x + 6y = 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For which number a does elimination break down permanently?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For which number a does elimination break down temporarily?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2 pts) SOLVING THE NON-SINGULAR SYSTEM" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fix the temporary breakdown above by a row exchange and then solve.\n", "\n", "Enter the value of x:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the value of y:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "y=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Problem 3. Linear Combinations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2 pts) The column vectors\n", "u=(1,1,2)T,v=(1,2,3)T,w=(3,5,8)T\n", "are in a plane because w is a linear combination of u and v.
If w=au+bv:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the numerical value of a:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the numerical value of b:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "b=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2 pts) LINEAR COMBINATIONS 2
\n", "The combination above is unique. However, there is more than one combination of u,v,w that produces z=(0,0,0)T.
Let us find two of them. First, if 2u+cv+dw=(0,0,0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the numerical value of c:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the numerical value of d:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1 pt) LINEAR COMBINATIONS 3
\n", "Second, u+cv+dw=(0,0,0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the numerical value of c:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the numerical value of d:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At this point you should note that there are infinite ways to write any vector on that plane as a linear combination of u,v,w.
\n", "However, there is no way to do that for any vector outside the plane." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2 pts) LINEAR COMBINATIONS 4
\n", "Think about the vector b=(1,\u22121,e)T. What is the only possible number e that gives a vector on the plane?
\n", "Enter the numercal value of e:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "e=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Problem 4. ORTHOGONAL VECTORS \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2 pts) Find two vectors v and w that are perpendicular to (1,0,1) and to each other.
Enter your answer in the form [a,b,c] (note the square brackets)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "v=[ , , ]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "w=[ , , ]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1 pt)A TASTE OF PROJECTION \n", "With v=(1,1) and w=(1,5), choose a number c so that w\u2212cv is perpendicular to v.\n", "\n", "Enter the numerical value of c:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c=" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Julia Problem 1: Defining a Variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1 pt) In Julia you simply assign a variable to x by simply typing the letter x and specifying what it is equal to. Ending the assignment with a semicolon suppresses printing. In the IJulia notebook, you execute a Julia command by typing shift + enter.
\n", "\n", "Write Julia code that assigns 1234 to the variable x. See what happens if you follow with and without a semicolon." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Julia Problem 2: Define a Row Vector" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1 pt) Julia, somewhat like python, and unlike MATLAB has vectors which are different from 1 x n or n x 1 matrices.\n", "A row vector is created by placing elements between square brackets with commas separating elements, e.g. [1,1,2,4,6,3]
\n", "\n", "Why does Julia do it this way? Answer: There are many data science applications where\n", "one dimensional structures are extremely important.\n", "\n", "Write Julia code that creates the vector y=[3 5 2 1].\n", "\n", "Julia indicates that the result is a 4-element array of 64 bit integers that is 1-dimensional, in other words, a vector! " ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Julia Problem 3: Define a One Column Matrix (there are no column vectors!) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1 pt) In Julia one creates a one column matrix by placing elements between square brackets, with spaces separating elements, and then using apostrophe as a transpose: e.g. [1 1 2]'
\n", "\n", "What really happens here is that [1 1 2] is a 1x3 two dimensional matrix, and the apostrophe\n", "turns it into a 3x1 two dimensional matrix. \n", "\n", "\n", "Write Julia code that creates the 5x1 matrix \n", "\n", "\n", "\n", "\n", "\n", "
1
2
-1
0
0
" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Julia Problem 4. Define a Matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1 pt) In Julia a matrix can be written
\n", "[1 2 3
\n", " 4 5 6
\n", " 7 8 9] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write Julia code that creates the 2x2 matrix A= \n", " \n", " \n", " \n", "
8 4
2 3
" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Julia Problem 5: Solve a Linear System" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1 pt) Solve Ax=b, where
\n", "A=
\n", "[ 2 1 -1
\n", " -1 2 -1
\n", " 0 -1 1]
\n", " and b=[1,2,1].\n", " \n", " To solve for x, use the backlash command x=A\\b " ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Julia Problem 6: Plot a Line and Interact with it" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1 pt) Plot the linear function u(x) = 5 + 3x on 0 \u2264 x \u2264 8\n", "\n", "Julia has a number of packages for plotting because different people enjoy plotting in different ways. We will use the matplotlib package from python for plotting, and the julia interact package for interacting. Some of these commands may not make complete sense yet, but you\n", "can use these \"magic incantations\" for many purposes without needing to understand how they work.\n", "\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Execute this cell\n", "using PyPlot # takes a little time but only needs to be executed once\n", "using Interact # adds the fun interactive feature" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Execute this cell as well\n", "u(x) = 5 + 3x\n", "range=0:.01:8\n", "f=figure()\n", "\n", "@manipulate for x=0:.5:8; withfig(f) do\n", " plot(range,u(range))\n", " plot(x,u(x),\"o\")\n", " title(\"x=$x u(x)=$(round(u(x),2))\")\n", " end\n", "end" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slide x to 6.5 and observe u(6.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There should be a plot that highlights x=6.5. What is u(6.5)?" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Julia Problem 7. Plot Solutions of Linear Systems" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1 pt) Consider the linear system for various values of p and q that you can manipulate
\n", "x + y = p
\n", " -x + 3y = q\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Where is the solution when p=7 and q=1?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Execute the code below\n", "f=figure()\n", "x=-3:7:20\n", "@manipulate for p=0:.5:14,q=0:.5:14; withfig(f) do\n", " plot(x,p-x)\n", " plot(x,(q+x)/3)\n", " plot((3*p-q)/4, (p+q)/4, \"o\")\n", " title(\"p=$p q=$q\")\n", " end\n", "end" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Obtain the solution when p=7 and q=1 and show the plot\n", "( , )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Julia Problem 8. Singular Matrices with no zeros" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(5 pts) Write Julia code to create a 100 x 100 singular matrix named soln. It should not have any zeros. Try to search the Julia online help.
\n", "Hint: Look in http://julia.readthedocs.org/en/latest/stdlib/arrays/?highlight=ones#Base.ones" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }