Author: John Jeng

# HOMEWORK TOTAL: 100 POINTS

For this homework assignment:
• Take off 5 points if they did not use show() as in the directions.
• Grade points for each question based on output. Check if output is similar to this solution guide.
• If they typed the wrong function for a problem but their procedure looks correct, only take off points for having the wrong function.
• Award half credit to any part where they did not use Sage (ie. they solved by hand and typed the result in)

### Problem 1 -- defining and evaluating a function:

1. Define in Sage $f(x) = \sinh(x^2+\sqrt{x-1}) + e^{\pi x} + \arcsin(x) + \frac{1}{x^3-x-e}$.
2. Compute $f(1/2)$ symbolically (exactly).
3. Compute $f(1/2)$ numerically (so a decimal expansion).
4. Plot $f(x)$ from $-1$ to $1$.

## 10 Points Total

#### 2 points per part; 2 points for completion

f(x) = sinh(x^2 + sqrt(x-1)) + e^(pi*x) + arcsin(x) + 1/(x^3 - x - e)
show(f)
show(f(1/2))
show(N(f(0.5)))
complex_plot(f, (-1, 1), (-1, 1))

$\displaystyle x \ {\mapsto}\ \frac{1}{x^{3} - x - e} + \arcsin\left(x\right) + e^{\left(\pi x\right)} + \sinh\left(x^{2} + \sqrt{x - 1}\right)$
$\displaystyle \frac{1}{6} \, \pi - \frac{8}{8 \, e + 3} + e^{\left(\frac{1}{2} \, \pi\right)} + \sinh\left(\sqrt{-\frac{1}{2}} + \frac{1}{4}\right)$
$\displaystyle 5.20284206077875 + 0.670044049132845i$


%html

<font color="red"><h2>10 Points Total</h2>
<h4>2 points per part; 2 points for completion</h4>
</font>


### Problem 2 -- finding zeros numerically:

Let $f(x) = \displaystyle x^2 + \sin(x)$

1. Draw a plot of $f$ on the interval $[-2,2]$.
2. Differentiate $f$
3. Integrate $f$
4. Find all the zeros of $f(x)$ numerically.

## 10 Points Total

#### 2 points for parts 1, 2, and 3. 4 points total for part 4 (2 points per root)

f(x) = x^2 + sin(x)
plot(f, -2,2)
show(diff(f, x))
show(integrate(f, x))
f.find_root(-2,-1/2)
f.find_root(-1/2,1/2)
# (use brain) no other zeros because $x^2$ is big.

$\displaystyle x \ {\mapsto}\ 2 \, x + \cos\left(x\right)$
$\displaystyle x \ {\mapsto}\ \frac{1}{3} \, x^{3} - \cos\left(x\right)$
-0.8767262153950622 5.78218635220173e-23



### Problem 3 -- The Cauchy Distribution

Let $\displaystyle\space f(x;\space x_0,\space\gamma) = \frac{1}{\pi\gamma}\left[\frac{\gamma^2}{(x - x_0)^2 + \gamma^2}\right]$

1. Plot and find the area under the curve of $f([-2, 2])$ for the following values of $x_0$ and $\gamma$ on the interval $x = [-4,4]$
• $x_0 = 0,\hspace{3mm} \gamma = 1$
• $x_0 = 2,\hspace{3mm} \gamma = 2$
• $x_0 = 0,\hspace{3mm} \gamma = 0.5$
1. Integrate $f$ from $-\infty$ to $x$ using the dummy variable $t$ as in $f(t;\space x_0,\space\gamma)$.
2. Plot the resulting function from 3 for $x_0 = 0,\hspace{3mm} \gamma = 0.5$.

## 10 Points Total

#### 1 points for part 3

# 1a.
gamma = 1
x_0 = 0
f(x) = 1/(pi*gamma) * (gamma^2/((x - x_0)^2 + gamma^2))
p = plot(f, -2, 2, fill=True) + plot(f, -4, 4)
show(p)
show(N(integral(f(x), x, -2, 2)))
show(integral(f, x, -2, 2))

$\displaystyle 0.704832764699133$
$\displaystyle \frac{2 \, \arctan\left(2\right)}{\pi}$
# 1b.
gamma = 2
x_0 = 2
f(x) = 1/(pi*gamma) * (gamma^2/((x - x_0)^2 + gamma^2))
p = plot(f, -2, 2, fill=True) + plot(f, -4, 4)
show(p)
show(N(integral(f, x, -2, 2)))
show(integral(f, x, -2, 2))

$\displaystyle 0.352416382349567$
$\displaystyle \frac{\arctan\left(2\right)}{\pi}$
# 1c.
gamma =0.5
x_0 = 0
f(x) = 1/(pi*gamma) * (gamma^2/((x - x_0)^2 + gamma^2))
p = plot(f, -2, 2, fill=True) + plot(f, -4, 4)
show(p)
show(N(integral(f, x, -2, 2)))
show(integral(f, x, -2, 2))

$\displaystyle 0.844041739245261$
$\displaystyle \frac{2.0 \, \arctan\left(4\right)}{\pi}$
f(t, x_0, gamma) = 1/(pi*gamma) * (gamma^2/((t - x_0)^2 + gamma^2))
F(t, x_0, gamma) = integral(f(t, x_0, gamma), t, -oo, x)
show(F(t, x_0, gamma))
print ""
plot(integral(f(t, 0, 1/5), t, -oo, x),-4, 4)
print "Either plot is good."
plot(integral(f(t, 0, 1/5), t, -oo, x))

$\displaystyle \frac{\gamma {\left(\frac{\pi}{\gamma} - \frac{2 \, \arctan\left(-\frac{x - x_{0}}{\gamma}\right)}{\gamma}\right)}}{2 \, \pi}$
Either plot is good.

### Problem 4 -- a function with no elementary antiderivative:

Let $f(x) = \sin(x^2) + \exp(1/x)$

1. Draw a plot of $f$ on the interval $[1/2, 4]$.
2. Differentiate $f$
3. Integrate $f$

## 10 Points Total

#### 3 points per part, 1 point for completion

# 1
f(x) = sin(x^2) + exp(1/x)
show(f)
print ""
plot(f, (1/2,4))

$\displaystyle x \ {\mapsto}\ e^{\frac{1}{x}} + \sin\left(x^{2}\right)$
# 2
f(x) = sin(x^2) + exp(1/x)
show(diff(f, x))

$\displaystyle x \ {\mapsto}\ 2 \, x \cos\left(x^{2}\right) - \frac{e^{\frac{1}{x}}}{x^{2}}$
# 3
f(x) = sin(x^2) + exp(1/x)
show(integrate(f, x))

$\displaystyle x \ {\mapsto}\ x e^{\frac{1}{x}} + \frac{1}{16} \, \sqrt{\pi} {\left(\left(i + 1\right) \, \sqrt{2} \text{erf}\left(\left(\frac{1}{2} i + \frac{1}{2}\right) \, \sqrt{2} x\right) + \left(i - 1\right) \, \sqrt{2} \text{erf}\left(\left(\frac{1}{2} i - \frac{1}{2}\right) \, \sqrt{2} x\right) - \left(i - 1\right) \, \sqrt{2} \text{erf}\left(\sqrt{-i} x\right) + \left(i + 1\right) \, \sqrt{2} \text{erf}\left(\left(-1\right)^{\frac{1}{4}} x\right)\right)} - {\rm Ei}\left(\frac{1}{x}\right)$

### Problem 5: Limits

1. Compute $\lim_{x\to 0} \sin(x)/x$
2. Use Sage to verify that strange and amazing fact $\lim_{x\to 0} (\cos x)^{1/x^2} = \frac{1}{\sqrt{e}}$.

## 10 Points Total

#### 5 points per part

# Solutions
show(limit(sin(x)/x, x=0))
show(limit(cos(x)^(1/x^2), x=0))

$\displaystyle 1$
$\displaystyle e^{\left(-\frac{1}{2}\right)}$

### Problem 6: Taylor Series

Let $f(x) = \sin(x^2)$

1. Find the 3rd degree taylor series, $p_3(x)$, of $f$ where $x_0 = 2\pi$
2. Plot the 10th degree taylor series $p_{10}(x)$ where $x_0 = 2\pi$ alongside $f$
• Plot on the interval $x=[\pi, 3\pi]$

## 10 Points Total

#### 7 points for part 2

# 1
f(x) = sin(x^2)
p_3(x) = taylor(f(x), x, 2*pi, 3)
show(p_3)

$\displaystyle x \ {\mapsto}\ \frac{4}{3} \, {\left(2 \, \pi - x\right)}^{3} {\left(8 \, \pi^{3} \cos\left(4 \, \pi^{2}\right) + 3 \, \pi \sin\left(4 \, \pi^{2}\right)\right)} - {\left(2 \, \pi - x\right)}^{2} {\left(8 \, \pi^{2} \sin\left(4 \, \pi^{2}\right) - \cos\left(4 \, \pi^{2}\right)\right)} - 4 \, \pi {\left(2 \, \pi - x\right)} \cos\left(4 \, \pi^{2}\right) + \sin\left(4 \, \pi^{2}\right)$


# 2
f(x) = sin(x^2)
p_10(x) = taylor(f(x), x, 2*pi, 10)

P1 = plot(p_10(x), pi, 3*pi, ymax=2, ymin=-2)
P2 = plot(f(x), pi, 3*pi, linestyle='--')

show(P1 + P2)


### Problem 7 - Gradient Vector Field:

1. Compute the gradient of $f(x,y) = 3\sin(x) - 2\cos(2y) - x - y$.
2. Plot the 2-dimensiona vector field defined by the gradient of $f$ in the rectangle $-2 \leq x,y \leq 2$.

## 10 Points Total

#### 5 points for each part


f(x,y) = 3*sin(x) - 2*cos(2*y) - x- y


$\displaystyle \left( x, y \right) \ {\mapsto} \ \left(3 \, \cos\left(x\right) - 1,\,4 \, \sin\left(2 \, y\right) - 1\right)$

### Problem 8 - Symbolic Sums:

1. Compute $\sum_{n=1}^{\infty} \frac{(-1)^n}{n}$.
2. Compute $\sum_{n=1}^{\infty} \frac{1}{n^2}$.
3. Compute $\sum_{n=1}^{\infty} \frac{1}{n^3}$ both symbolically (in terms of the Riemann Zeta function) and numerically.
4. Compute $\sum_{n=1}^{\infty} \frac{1}{n^4}$.
5. Compute $\sum_{n=1}^k \sin(n)$.

## 10 Points Total

#### 2 points per part

%var n
show(sum((-1)^n/n, n, 1, oo))

$\displaystyle -\log\left(2\right)$
%var n
show(sum(1/n^2, n, 1, oo))

$\displaystyle \frac{1}{6} \, \pi^{2}$
%var n
show(sum(1/n^3, n, 1, oo))
show(N(sum(1/n^3, n, 1, oo)))

$\displaystyle \zeta(3)$
$\displaystyle 1.20205690315959$
%var n
show(sum(1/n^4, n, 1, oo))

$\displaystyle \frac{1}{90} \, \pi^{4}$
%var k
show(sum(sin(n), n, 1, k))

$\displaystyle \frac{\cos\left(k \arctan\left(\frac{\sin\left(1\right)}{\cos\left(1\right)}\right) + \arctan\left(\frac{\sin\left(1\right)}{\cos\left(1\right)}\right)\right) \sin\left(1\right) - {\left(\cos\left(1\right) - 1\right)} \sin\left(k \arctan\left(\frac{\sin\left(1\right)}{\cos\left(1\right)}\right) + \arctan\left(\frac{\sin\left(1\right)}{\cos\left(1\right)}\right)\right) - \sin\left(1\right)}{2 \, {\left(\cos\left(1\right) - 1\right)}}$

### Problem 9 -- Unit Conversion:

Use Sage's units functionality (written by a UW undergrad -- David Ackerman!)

1. Convert 68 degrees Fahrenheit to Celcius. Hint: use 68*units.temperature.fahrenheit to define fahrenheit.
2. Convert 15 milliseconds to hours.
3. Convert 2016 degrees kelvins to degrees Fahrenheit.
4. Convert 9.8 meters per second squared to feet per second squared.

## 10 Points Total

#### 2 points per part; 2 points for completion

# 1
a = 68*units.temperature.fahrenheit
show(a.convert(units.temperature.celsius))
a.convert(units.temperature.celsius)

$\displaystyle 20 \, \mathit{celsius}$
20*celsius
# 4
a = 0.015*units.time.second
show(a.convert(units.time.hour))
a.convert(units.time.hour)

$\displaystyle \left(4.16666666666667 \times 10^{-6}\right) \, \mathit{hour}$
(4.16666666666667e-6)*hour
# 3
a = 2016*units.temperature.kelvin
show(a.convert(units.temperature.fahrenheit))
a.convert(units.temperature.fahrenheit)

$\displaystyle 3169.13000000000 \, \mathit{fahrenheit}$
3169.13000000000*fahrenheit
# 4
a = 9.8*units.length.meter/units.time.second^2
show(a.convert(units.length.feet/units.time.second^2))
a.convert(units.length.feet/units.time.second^2)

$\displaystyle 32.1522309711286 \, \left(\frac{\mathit{feet}}{\mathit{second}^{2}}\right)$
32.1522309711286*(feet/second^2)

### Problem 10 - 3d Plotting:

1. Draw a 3d plot of a torus.
2. Draw a single 3d plot the has the five regular polytopes in it: tetrahedron, cube, octahedron, dodecahedron, icosahedron. All five must be visible.
3. Draw a 3d plot of the "Mexican hat function" (see, e.g., https://en.wikipedia.org/wiki/Mexican_hat_wavelet). [Hint: you have to make a choice of parameter $\sigma$ so that it looks like Mexican hat.]

## 10 Points Total

#### Maximum 3 points per part. 1 point for completion

Based on:

• Is it a torus?
• Can you count the polytopes? -0.5 for each one you don't see.
• Does it look like a hat? (it can be upside down)

u, v = var('u,v')
f1 = (4+(3+cos(v))*sin(u), 4+(3+cos(v))*cos(u), 4+sin(v))
parametric_plot3d(f1, (u,0,2*pi), (v,0,2*pi), texture="red", mesh=2)

3D rendering not yet implemented
tetrahedron() + cube().translate((1,0,0)) + octahedron().translate((2,0,0)) + dodecahedron().translate((3,0,0)) + icosahedron().translate((4,0,0))

3D rendering not yet implemented
sigma = -1/2
f(x,y) = 1/(pi*sigma^4) * (1-((x^2+y^2)/(2*sigma^2)))*exp(-(x^2+y^2)/(2*sigma^2))
plot3d(f, (x,-2,2), (y,-2,2))

3D rendering not yet implemented