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.
- Compute the derivative with respect to $x$ of $\sin(x)\cos(\sin(x)) \arctan(x) + e^{x^2 \cos(y)}$.
{{{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)
}}}
- Compute a symbolic integral with respect to $x$ of the function $f(x) = \sin(x) \cos(x) \tan(x) e^x$
{{{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
}}}
- Compute limit as $x\to 0$ of the function $f(x) = \sin(x^2)/x + (\cos(x)-1)/x$. Also, draw a plot of $f(x)$ on the interval $(-1,1)$.
{{{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.
- Draw a plot of where the function $f(x,y) = x\sin(y) - \cos(y)\cos(x)$ is equal to 0 using the Sage function implicit_plot.
{{{id=26|
f(x,y) = x*sin(y) - cos(y)*cos(x)
implicit_plot(f==0, (x,-2,2), (y,-2,2))
///
}}}
- Use fast_float to create a callable Python object $g$ from $f$. Then benchmark the difference in speed between evaluating $f$ at $(0.0, 0.0)$ versus evaluating $g$ at $(0.0, 0.0)$ using timeit.
{{{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|
///
}}}
- Implement a function $h$ in Cython that takes two floats x, y as input and returns $f(x,y)$. How fast is it? (Make sure to look at the lecture notes from May 14, 2010.)
{{{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|
///
}}}
- Use the scipy function scipy.optimize.fmin to find a zero of $f(x,y)$ by finding a local minimum of the function $f(x,y)^2$ on the plane, where you should start the optimization at the initial point $(0,0)$. Time using $f$, $g$, and $h$ as input to scipy.optimize.fmin. Hint: scipy.optimize.fmin expects a callable f that takes a single input z with x=z[0] and y=z[1]. So you may want to write code like this: scipy.optimize.fmin(lambda z: f(z[0],z[1])^2, (0,0))
{{{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|
///
}}}
- Draw a plot that includes both the implicit_plot above and a big red dot at the local minimum that fmin found.
{{{id=5|
implicit_plot(f==0, (x,-2,2), (y,-2,2)) + point((1.26288993, 0.23552294), color='red', pointsize=50)
///
}}}
{{{id=46|
///
}}}
- Create an interact that looks like the image below. There are two sliders which allow you to select an $(x,y)$ point. Once you select that point it is used as the starting point for the fmin iteration, and a blue dot is plotted at that point and a red dot at the zero that fmin finds. The sliders should allow you to select $x,y$ values between -1 and 1.

{{{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)
///
}}}
{{{id=49|
///
}}}
{{{id=45|
///
}}}
{{{id=4|
///
}}}
Problem 3. Matrices
- Create $8\times 8$ matrix $M$ whose $i,j$ entry (for $0\leq i, j \leq 7$) is the last digit of $i^2+j^2 + ij -i +1$. Create $M$ as a Sage matrix with integer entries.
{{{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|
///
}}}
- Compute the following invariants of this matrix $M$: determinant, rank, nullity, kernel, and characteristic polynomial.
{{{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|
///
}}}
- Create the same matrix as $M$ (call it $N$), but with entries in the "double precision" real numbers. (In Sage, this means the ring RDF.) Then compute all the same invariants as above for this new matrix $N$.
{{{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|
///
}}}