Student's Name: Pat Hooper
Directions: Add work to this notebook to solve the problems below.
Problem Sources:
These are standard imports:
import numpy as np
import math as m
import matplotlib.pyplot as plt
Write a function called volume_of_sphere(r)
which returns the volume of a sphere of radius r
in three dimensional space.
(Hints: Using the imports above you can get $\pi$ via m.pi
. The problem has been started for you below. Replace ???
with the formula. A test has been provided below.)
def volume_of_sphere(r):
return r**2
volume_of_sphere(3)
# This is a test that should pass without errors if
if abs(volume_of_sphere(3) - 36*m.pi) > 10**-8:
print('There is an error in your formula.')
else:
print('That is correct. Good job!')
Write a function product_string(a, b)
which takes as input two numbers a
and b
and returns a string of the form
'a times b is a*b.'
Here a
should be replaced by the value of a
, b
by the value of b
, and a*b
by the value of a*b
. For example, product_string(2, 5)
should return the string
'2 times 5 is 10.'
def product_string(a, b):
return ???
# This is a test
s = product_string(2, 5)
if s == '2 times 5 is 10.':
print('That is correct. Good job.')
else:
print('There is an error in your code.')
print(f"Your code returned '{s}', but it should have returned '2 times 5 is 10.'.")
Let $p$ be a bank’s interest rate in percent per year. An initial amount $A$ has then grown to
$$A\left(1+\frac{p}{100}\right)^n$$
after $n$ years. Write a function compute_new_amount
that takes as input three variables A
, p
, and n
as above and outputs the result of the formula above.
Check your work by compute how much money 1000 euros have grown to after three years with 5% interest rate and comparing your answer with the the value found by hand or on a calculator.
(This is a modification of Exercise 1.5 of L.)
def compute_new_amount(A, p, n):
???
# The check:
value = compute_new_amount(1000, 5, 3)
if type(value)!=float:
print('There is an error. A float should have been returned.')
print(f'You computed the amount will grow to ${value:.2f}.')
Write a function prod(l)
which returns the product of numbers in a list of numbers l
. For example prod([1, 2, 3])
should return 6.
The problem has been started for you below. You just need to fix the line that says
# update the value of p by multiplying by x
(Remark: There is a prod
function in the math library that does exactly this, but you should write an independent solution.)
def prod(l):
p = 1
for x in l:
# update the value of p by multiplying by x
return p
answer = prod([2, -7, 5])
if answer == -70:
print('Good job. That seemed to work.')
else:
print('There is an error.')
print(f'Your code returned {answer} but the correct answer is -70.')
The function range
is built into Python and returns the a finite ordered collection of equally spaced integers.
n
, range(n)
returns an object that acts like the tuple
$$(0, 1, 2, \ldots, n-1).$$range(a, b)
returns an object that represents the tuple
$$(a, a+1, a+2, \ldots, b-1).$$range(a, b, s)
returns an object representing the tuple $$(a, a+s, a+2*s, \ldots, a+t*s),$$ where $t$ is the largest integer so that $b-(a+t*s)$ has the same sign as $b-a$.For more information about ranges, see the Python library documentation or § 3.1.4 of
Here are some problems to get to know range
. The first one was done for you.
Construct a range object ra
which is represents the same sequence as (0, 1, 2, 3, 4)
.
ra = range(5)
tuple(ra)
Construct a range object rb
which is represents the same sequence as (0, 1, 2, 3, 4, 5, 6)
.
rb = range(???)
tuple(rb)
Construct a range object rc
which is represents the same sequence as (3, 4, 5, 6)
.
rc = range(???)
tuple(rc)
Construct a range object rd
which is represents the same sequence as (10, 9, 8, 7)
.
rd = range(???)
tuple(rd)
Construct a range object re
which is represents the same sequence as (2, 9, 16, 23, 30)
.
re = range(???)
tuple(re)
Use Numpy
and Matplotlib
to plot the function
$$f(x)=(1-x^2) \sin\left(\frac{1}{1-x^2}\right)$$
over the open interval $(-1,1)$.
Hint: You do not want to include $-1$ and $1$ in your $x$-values because $f(-1)$ and $f(1)$ are not defined. You should instead use np.linspace
to produce a list of $x$-values ranging from say $-0.99999$ to $0.99999$.
In class, a circle was drawn parametrically using the formulas $x(t)=\cos t$ and $y(t)=\sin t$ and $t \in [0, 2 \pi]$.
Graph the curve determined by the formulas $$x(t) = 16 \sin(t)^3,$$ $$y(t) = 12 \cos(t)-5 \cos(2t)-2 \cos(3t)- \cos(4t),$$ for $t \in [0, 2\pi].$
(Remarks: This should be a shape you recognize. Be sure to use the line plt.axes().set_aspect(1)
to set the aspect ratio correctly.)