{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# More NumPy\n",
"\n",
"Let's talk about some more we can do with NumPy. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Exercise 1:*** Write code that will take a function `f` and use the trapezoidal rule to approximate the integral of `f` with `N` points from 0 to 1.\n",
"\n",
"The trapezoial rule is something you may have learned in a calculus course, if you've taken one. The way it works is that we approximate our function by finding the value at `N` points and drawing line segments between these points. The area underneath a line segment is a trapezoid, which we can compute using the formula A = 1/2(b1 + b2)h. This gives us an estimate for the area underneath the graph of f, which is the integral of f.\n",
"\n",
"This time, we will be writing a function to compute this for us. We'll have each subpart of the problem be its own function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Part 1:*** Write a function called `sample_points` which takes a function `f` and returns a NumPy vector of `f` evaluated at `N` points evenly spaced between 0 and 1.\n",
"\n",
"Hint: Normally we want to avoid looping over a NumPy vector, but here we will have to.\n",
"\n",
"(see my website [here](https://www.math.ucla.edu/~egeo/classes/f20_pic16a/notebook_week6b.html) or discussion video for solution)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Part 2:*** Write a function called `compute_area` which computes the area under the trapezoids defined by the sampled points.\n",
"\n",
"For evenly spaced points (which we have), this has a simple formula. It is \n",
"1/2(value of first point) + (value of second point) + (value of third point) + ... + (value of second to last point) + 1/2(value of last point)\n",
"(this is the sum of all the point values, except only half for the first and last point)\n",
"and all of that multipled by the distance between the sampled points.\n",
"\n",
"(see my website [here](https://www.math.ucla.edu/~egeo/classes/f20_pic16a/notebook_week6b.html) or discussion video for solution)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After this, we have our solution:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def trapezoidal_rule(f, N):\n",
" pts = sample_points(f, N)\n",
" return compute_area(pts)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Part 3:*** Use the above functions and Pyplot to plot y = sin(pi x) and the trapezoial rule approximation. Also, approximate the integral.\n",
"\n",
"(see my website [here](https://www.math.ucla.edu/~egeo/classes/f20_pic16a/notebook_week6b.html) or discussion video for solution)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Exercise 2:*** Download the files `cal_cities_lat_long.csv` and `cal_cities_names.txt` from CCLE or my website. Imagine we're trying to pick a city to place something (such as a utility, or a concert hall, or a business, etc.). We want it to be as close as possible to all of California. By this I mean that we want the furthest California city from our city to be as close as possible.\n",
"\n",
"We'll plot the cities as latitude and longitude on the X-Y plane and use the distance there as the distance between cities. This isn't completely accurate, but it's good enough.\n",
"\n",
"First, we'll get the data from the files. Later on in class, we'll learn a better package to load data from files, but NumPy works just fine."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cal_cities = np.genfromtxt('cal_cities_lat_long.csv', delimiter=',')\n",
"print(cal_cities.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is the latitude and longitude of 459 California cities."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Part 1:*** Write a function `max_dist` that takes in a NumPy array `pts` with two columns and a NumPy vector `v` and returns the maximum distance between any point in `pts` and `v`.\n",
"\n",
"(see my website [here](https://www.math.ucla.edu/~egeo/classes/f20_pic16a/notebook_week6b.html) or discussion video for solution)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Part 2:*** Write a function called `choose_city` that uses the above to choose the city we want. Return the index of the city.\n",
"\n",
"(Hint: again, we are going to need to loop over our NumPy array, after this I will explain how we can vectorize our code.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To vectorize the code properly, the best way I know how to do is to create a 2D array of distances in a vectorized way, and then use NumPy methods to find the maximum of each column and then then the minimum of the maximums. This requires a little bit more knowledge of broadcasting, however, which you don't quite know yet.\n",
"\n",
"***Part 3:*** Use these two functions to find our choosen city. Then load `cal_cities_names.txt` to get the name of the city. The list of cities are in the same order as the latitude and longitude and separated by lines. So the 0-th index row in `cal_cities` is the first line of `cal_cities_names.txt`.\n",
"\n",
"(see my website [here](https://www.math.ucla.edu/~egeo/classes/f20_pic16a/notebook_week6b.html) or discussion video for solution)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Part 4:*** Plot all the cities of California, with our chosen city in a different color."
]
},
{
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}