Math 480b: Solutions to Homework Assignment 5

Due: Thursday, May 26 by 11:30pm.

{{{id=7| /// }}}

Instructions:  Do all the following 3 problems.  You may have to explore the Sage documentation to figure out how to do some of these problems (that's part of the point of the problem).  The lecture notes from class should also be very helpful.

{{{id=9| /// }}}

Problem 1: Symbolic Calculus.

{{{id=19| var('x,y') diff(sin(x)*cos(sin(x))*atan(x) + e^(x^2 * cos(y)), x) /// -sin(x)*sin(sin(x))*cos(x)*arctan(x) + 2*x*e^(x^2*cos(y))*cos(y) + cos(x)*cos(sin(x))*arctan(x) + sin(x)*cos(sin(x))/(x^2 + 1) }}} {{{id=21| integrate(sin(x)*cos(x)*tan(x)*exp(x),x) /// -1/5*e^x*sin(2*x) - 1/10*e^x*cos(2*x) + 1/2*e^x }}} {{{id=22| f(x) = sin(x^2) / x + (cos(x^2)-1)/x /// }}} {{{id=6| f.limit(x=0) /// 0 }}} {{{id=25| plot(f, -1,1) /// }}} {{{id=24| /// }}} {{{id=41| /// }}}

Problem 2: Fast Expression Evaluation.

{{{id=26| f(x,y) = x*sin(y) - cos(y)*cos(x) implicit_plot(f==0, (x,-2,2), (y,-2,2)) /// }}} {{{id=31| g = fast_float(f) /// }}} {{{id=32| a=float(0) timeit('f(a,a)') timeit('g(a,a)') /// 625 loops, best of 3: 210 µs per loop 625 loops, best of 3: 319 ns per loop }}} {{{id=34| /// }}} {{{id=33| %cython cdef extern from "math.h": double cos(double) double sin(double) def h(double x, double y): return x*sin(y) - cos(y)*cos(x) /// }}} {{{id=30| timeit('h(a,a)') /// 625 loops, best of 3: 221 ns per loop }}} {{{id=28| /// }}} {{{id=39| import scipy.optimize /// }}} {{{id=43| scipy.optimize.fmin(lambda x: f(x[0],x[1])^2, (-1,0)) /// Optimization terminated successfully. Current function value: 0.000000 Iterations: 28 Function evaluations: 51 array([-1.56596338, -0.0030895 ]) }}} {{{id=40| timeit('scipy.optimize.fmin(lambda x: f(x[0],x[1])^2, (0,0))') /// 5 loops, best of 3: 41.7 ms per loop }}} {{{id=37| timeit('scipy.optimize.fmin(lambda x: g(x[0],x[1])^2, (0,0))') /// 125 loops, best of 3: 4.2 ms per loop }}} {{{id=36| timeit('scipy.optimize.fmin(lambda x: h(x[0],x[1])^2, (0,0))') /// 125 loops, best of 3: 4.12 ms per loop }}} {{{id=27| /// }}} {{{id=5| implicit_plot(f==0, (x,-2,2), (y,-2,2)) + point((1.26288993, 0.23552294), color='red', pointsize=50) /// }}} {{{id=46| /// }}}

{{{id=48| /// }}} {{{id=44| @interact def demo(startx=(-1,1), starty=(-1,1)): a,b = scipy.optimize.fmin(lambda x: h(x[0],x[1])^2, (startx, starty)) G = implicit_plot(f==0, (x,-2,2), (y,-2,2)) G += point((a,b), color='red', pointsize=50) G += point((startx, starty), color='blue', pointsize=50) show(G) ///
startx 
starty 
}}} {{{id=49| /// }}} {{{id=45| /// }}} {{{id=4| /// }}}

Problem 3. Matrices

{{{id=50| M = matrix(8, [(i^2+j^2 + i*j + i + 1)%10 for i in range(8) for j in range(8)]); M /// [1 2 5 0 7 6 7 0] [3 5 9 5 3 3 5 9] [7 0 5 2 1 2 5 0] [3 7 3 1 1 3 7 3] [1 6 3 2 3 6 1 8] [1 7 5 5 7 1 7 5] [3 0 9 0 3 8 5 4] [7 5 5 7 1 7 5 5] }}} {{{id=53| /// }}} {{{id=3| M.det() /// 0 }}} {{{id=14| M.rank() /// 7 }}} {{{id=16| M.nullity() /// 1 }}} {{{id=18| M.kernel() /// Free module of degree 8 and rank 1 over Integer Ring Echelon basis matrix: [ 1 1 0 0 0 -1 -1 0] }}} {{{id=54| M.characteristic_polynomial() /// x^8 - 26*x^7 - 178*x^6 - 872*x^5 - 1820*x^4 - 27400*x^3 - 370000*x^2 - 760000*x }}} {{{id=57| /// }}} {{{id=56| N = matrix(RDF, 8, [(i^2+j^2 + i*j + i + 1)%10 for i in range(8) for j in range(8)]); N /// [1.0 2.0 5.0 0.0 7.0 6.0 7.0 0.0] [3.0 5.0 9.0 5.0 3.0 3.0 5.0 9.0] [7.0 0.0 5.0 2.0 1.0 2.0 5.0 0.0] [3.0 7.0 3.0 1.0 1.0 3.0 7.0 3.0] [1.0 6.0 3.0 2.0 3.0 6.0 1.0 8.0] [1.0 7.0 5.0 5.0 7.0 1.0 7.0 5.0] [3.0 0.0 9.0 0.0 3.0 8.0 5.0 4.0] [7.0 5.0 5.0 7.0 1.0 7.0 5.0 5.0] }}} {{{id=55| N.det() /// -1.68753899743e-10 }}} {{{id=59| N.rank() /// 8 }}} {{{id=60| N.nullity() /// 0 }}} {{{id=1| N.kernel() /// Vector space of degree 8 and dimension 1 over Real Double Field Basis matrix: [ 1.0 1.0 0.0 0.0 0.0 -1.0 -1.0 0.0] }}} {{{id=61| N.characteristic_polynomial() /// x^8 - 26.0*x^7 - 178.0*x^6 - 872.0*x^5 - 1820.0*x^4 - 27400.0*x^3 - 370000.0*x^2 - 760000.0*x }}} {{{id=63| /// }}} {{{id=62| /// }}} {{{id=64| /// }}}