{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Homework 1\n",
"\n",
"Student's Name: Pat Hooper"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Directions:__ Add work to this notebook to solve the problems below.\n",
"\n",
"Problem Sources:\n",
"* __LL__: *Programming for Computations - Python* by Svein Linge and Hans Petter Langtangen, 2nd edition.\n",
"* __L__: *A Primer on Scientific Programming with Python* by Hans Petter Langtangen, 2nd edition.\n",
"* __TAK__: *Applied Scientific Computing With Python* by Peter R. Turner, Thomas Arildsen, and Kathleen Kavanagh."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These are standard imports:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import math as m\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Volume of a sphere\n",
"\n",
"Write a function called `volume_of_sphere(r)` which returns the volume of a sphere of radius `r` in three dimensional space.\n",
"\n",
"(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.)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def volume_of_sphere(r):\n",
" return r**2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"volume_of_sphere(3)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"There is an error in your formula.\n"
]
}
],
"source": [
"# This is a test that should pass without errors if \n",
"if abs(volume_of_sphere(3) - 36*m.pi) > 10**-8:\n",
" print('There is an error in your formula.')\n",
"else:\n",
" print('That is correct. Good job!')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. String formatting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a function `product_string(a, b)` which takes as input two numbers `a` and `b` and returns a string of the form \n",
"```\n",
"'a times b is a*b.'\n",
"```\n",
"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\n",
"```\n",
"'2 times 5 is 10.'\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def product_string(a, b):\n",
" return ???"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This is a test\n",
"s = product_string(2, 5)\n",
"if s == '2 times 5 is 10.':\n",
" print('That is correct. Good job.')\n",
"else:\n",
" print('There is an error in your code.')\n",
" print(f\"Your code returned '{s}', but it should have returned '2 times 5 is 10.'.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Compute the growth of money in a bank\n",
"\n",
"Let $p$ be a bank’s interest rate in percent per year. An initial amount $A$ has then grown to\n",
"$$A\\left(1+\\frac{p}{100}\\right)^n$$\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. \n",
"\n",
"Check your work by compute how much money 1000\n",
"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.\n",
"\n",
"(This is a modification of Exercise 1.5 of __L__.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def compute_new_amount(A, p, n):\n",
" ???"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The check:\n",
"value = compute_new_amount(1000, 5, 3)\n",
"if type(value)!=float:\n",
" print('There is an error. A float should have been returned.')\n",
"print(f'You computed the amount will grow to ${value:.2f}.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Product of elements in a list\n",
"\n",
"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.\n",
"\n",
"The problem has been started for you below. You just need to fix the line that says \n",
"```\n",
"# update the value of p by multiplying by x```\n",
"\n",
"(*Remark:* There is a `prod` function in the math library that does exactly this, but you should write an independent solution.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def prod(l):\n",
" p = 1\n",
" for x in l:\n",
" # update the value of p by multiplying by x\n",
" return p"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"answer = prod([2, -7, 5])\n",
"if answer == -70:\n",
" print('Good job. That seemed to work.')\n",
"else:\n",
" print('There is an error.')\n",
" print(f'Your code returned {answer} but the correct answer is -70.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. The range function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The function `range` is built into Python and returns the a finite ordered collection of equally spaced integers.\n",
"* For a positive integer `n`, `range(n)` returns an object that acts like the tuple \n",
"$$(0, 1, 2, \\ldots, n-1).$$\n",
"* If $a$ and $b$ are integers with $a < b$, then `range(a, b)` returns an object that represents the tuple\n",
"$$(a, a+1, a+2, \\ldots, b-1).$$\n",
"* 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$.\n",
"\n",
"For more information about ranges, see the [Python library documentation](https://docs.python.org/3/library/stdtypes.html#range) or § 3.1.4 of \n",
"* __LL__: *Programming for Computations - Python* by Svein Linge and Hans Petter Langtangen, 2nd edition.\n",
"\n",
"Here are some problems to get to know `range`. The first one was done for you."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5a\n",
"\n",
"Construct a range object `ra` which is represents the same sequence as `(0, 1, 2, 3, 4)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ra = range(5)\n",
"tuple(ra)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5b\n",
"\n",
"Construct a range object `rb` which is represents the same sequence as `(0, 1, 2, 3, 4, 5, 6)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rb = range(???)\n",
"tuple(rb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5c\n",
"\n",
"Construct a range object `rc` which is represents the same sequence as `(3, 4, 5, 6)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rc = range(???)\n",
"tuple(rc)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5d\n",
"\n",
"Construct a range object `rd` which is represents the same sequence as `(10, 9, 8, 7)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rd = range(???)\n",
"tuple(rd)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5e\n",
"\n",
"Construct a range object `re` which is represents the same sequence as `(2, 9, 16, 23, 30)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"re = range(???)\n",
"tuple(re)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 6. Plotting a function of one variable\n",
"\n",
"Use `Numpy` and `Matplotlib` to plot the function \n",
"$$f(x)=(1-x^2) \\sin\\left(\\frac{1}{1-x^2}\\right)$$\n",
"over the open interval $(-1,1)$. \n",
"\n",
"*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$. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 7. Parametric curves\n",
"\n",
"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]$.\n",
"\n",
"Graph the curve determined by the formulas\n",
"$$x(t) = 16 \\sin(t)^3,$$\n",
"$$y(t) = 12 \\cos(t)-5 \\cos(2t)-2 \\cos(3t)- \\cos(4t),$$\n",
"for $t \\in [0, 2\\pi].$\n",
"\n",
"(*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.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}