Homework 1

Student's Name: Pat Hooper

Directions: Add work to this notebook to solve the problems below.

Problem Sources:

  • LL: Programming for Computations - Python by Svein Linge and Hans Petter Langtangen, 2nd edition.
  • L: A Primer on Scientific Programming with Python by Hans Petter Langtangen, 2nd edition.
  • TAK: Applied Scientific Computing With Python by Peter R. Turner, Thomas Arildsen, and Kathleen Kavanagh.

These are standard imports:

In [1]:
import numpy as np
import math as m
import matplotlib.pyplot as plt

1. Volume of a sphere

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.)

In [3]:
def volume_of_sphere(r):
    return r**2
In [6]:
In [5]:
# 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.')
    print('That is correct. Good job!')
There is an error in your formula.

2. String formatting

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.'
In [ ]:
def product_string(a, b):
    return ???
In [ ]:
# This is a test
s = product_string(2, 5)
if s == '2 times 5 is 10.':
    print('That is correct. Good job.')
    print('There is an error in your code.')
    print(f"Your code returned '{s}', but it should have returned '2 times 5 is 10.'.")

3. Compute the growth of money in a bank

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.)

In [ ]:
def compute_new_amount(A, p, n):
In [ ]:
# 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}.')

4. Product of elements in a list

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.)

In [ ]:
def prod(l):
    p = 1
    for x in l:
        # update the value of p by multiplying by x
    return p
In [ ]:
answer = prod([2, -7, 5])
if answer == -70:
    print('Good job. That seemed to work.')
    print('There is an error.')
    print(f'Your code returned {answer} but the correct answer is -70.')

5. The range function

The function range is built into Python and returns the a finite ordered collection of equally spaced integers.

  • For a positive integer n, range(n) returns an object that acts like the tuple $$(0, 1, 2, \ldots, n-1).$$
  • If $a$ and $b$ are integers with $a < b$, then range(a, b) returns an object that represents the tuple $$(a, a+1, a+2, \ldots, b-1).$$
  • If $a$, $b$ and $s$ are integers so that $b-a$ and $s$ have the same sign, then 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

  • LL: Programming for Computations - Python by Svein Linge and Hans Petter Langtangen, 2nd edition.

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).

In [ ]:
ra = range(5)


Construct a range object rb which is represents the same sequence as (0, 1, 2, 3, 4, 5, 6).

In [ ]:
rb = range(???)


Construct a range object rc which is represents the same sequence as (3, 4, 5, 6).

In [ ]:
rc = range(???)


Construct a range object rd which is represents the same sequence as (10, 9, 8, 7).

In [ ]:
rd = range(???)


Construct a range object re which is represents the same sequence as (2, 9, 16, 23, 30).

In [ ]:
re = range(???)

6. Plotting a function of one variable

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 [ ]:

7. Parametric curves

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.)

In [ ]: