diff --git a/notebook1-4_test/notebook1-4_test.ipynb b/notebook1-4_test/notebook1-4_test.ipynb index 98e4bc0..0346811 100644 --- a/notebook1-4_test/notebook1-4_test.ipynb +++ b/notebook1-4_test/notebook1-4_test.ipynb @@ -23,7 +23,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1\n", + "### Exercise 1\n", "\n", "Write a function that plots a square centered at the origin. The length of the side of the square is $L$. The input of the function is the length $L$. \n", "\n", @@ -60,7 +60,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2\n", + "### Exercise 2\n", "Write a function called `countsquares` that takes as input the filename of a file that consists of a bunch of numbers separated by spaces. Inside the function, you must first read the numbers from the filename and then you must determine how many of these numbers are a perfect square. A perfect square means that the root of the number is an integer (i.e., 1, 4, 16, 25, etc.). The function returns the number of perfect squares in the file. Note: to convert a number to an integer, use the `int` function. \n", "\n", "Demonstrate that your function works by using the file `numbers2016.txt` and execute the following line of code:\n", @@ -86,7 +86,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3\n", + "### Exercise 3\n", "The file `quiz_answers.dat` contains the result of a small multiple-choice quiz with 8 questions. Every row contains the 8 answers to the quiz of one of the students. Your job is to count how many students had the correct answer for each question. Write a double loop to compute the number of correct answers for each question and present the results in a bar graph. The correct answers for the 8 questions of the quiz are: `a b c d a b c d`" ] }, @@ -134,7 +134,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5\n", + "### Exercise 5\n", "Write a function that computes the percentage of grades that is above a given value. The function takes as input arguments an array with grades between 1 and 10 and a minimum value and returns the precentage of grades (so between 0% and 100%) that are above or equal to that value. Demonstrate that your function works by loading the grades in the file `schoolgrades2016.txt` and print the result of the function to the screen with two decimal places, given a minimum value of 7." ] }, @@ -163,31 +163,31 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1\n", + "Answer to Exercise 1\n", "\n", "You probably know what this should look like\n", "\n", "Back to Exercise 1\n", "\n", - "Answer to Exercise 2\n", + "Answer to Exercise 2\n", "\n", "93\n", "\n", "Back to Exercise 2\n", "\n", - "Answer to Exercise 3\n", + "Answer to Exercise 3\n", "\n", "Q1: 4, Q2: 4, Q3: 5, Q4:3, Q5:5, Q6:5, Q7:4, Q8:4\n", "\n", "Back to Exercise 3\n", "\n", - "Answer to Exercise 4\n", + "Answer to Exercise 4\n", "\n", "Your graph should look like the provided figure\n", "\n", "Back to Exercise 4\n", "\n", - "Answer to Exercise 5\n", + "Answer to Exercise 5\n", "\n", "31.3\n", "\n", diff --git a/notebook10_continuous_random_variables/py_exploratory_comp_10.ipynb b/notebook10_continuous_random_variables/py_exploratory_comp_10.ipynb index abe8f79..6569688 100755 --- a/notebook10_continuous_random_variables/py_exploratory_comp_10.ipynb +++ b/notebook10_continuous_random_variables/py_exploratory_comp_10.ipynb @@ -121,7 +121,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1: First histogram\n", + "### Exercise 1: First histogram\n", "Generate 1000 random numbers from a Normal distribution with mean 100 and standard deviation 10. Compute and print to the screen the mean and standard deviation of your data. Create two graphs above each other using the `plt.subplot` command. In the top graph, plot a histogram using 20 bins going from 50 to 150. Note that with this size of a data set (1000 data points), the histogram starts to look a lot more like the typical bell-shaped curve of a Normal distribution. Add a red line representing the probability density function of the underlying normal distribution to the graph. In the bottom graph, draw a histogram of the cumulative distribution function, by setting the keyword `cumulative=True` (see `plt.hist?` for details). For the latter graph, use the keyword `align='right'` so that the bars are centered on the right bin edges (so that the line you are drawing next will approximately go through the centers of the bars). Add a red line representing the cumulative distribution function of the underlying normal distribution to the graph using the `norm.cdf` function, which works the same as the `norm.pdf` function but computes the cumulative distribution function (cdf). Finally, make sure the limits along the horizontal axis are the same for both graphs. " ] }, @@ -181,7 +181,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Expercise 2. Lower and upper quartile\n", + "### Expercise 2. Lower and upper quartile\n", "Generate 100 data points from a normal distribution with a mean of 20 and a standard deviation of 4. Compute the interquartile range (25%-75% range). Compute the theoretical value of the interquartile range and compare it to the interquartile range of the data. Draw a histogram of the cumulative distribution. Add red vertical lines to your graph for the 0.25 and 0.75 empirical quantiles of the data, and black vertical lines for the 0.25 and 0.75 quantiles of the underlying distribution. Vertical lines that span the graph may be added with the `plt.axvline` function, which takes the $x$ value of the line as an argument. To specify the color of the vertical line, use the `color` keyword argument." ] }, @@ -391,7 +391,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Loading experimental data and basic operations\n", + "### Exercise 3. Loading experimental data and basic operations\n", "Load the data in the file `douglas_data.csv` using the `read_csv` command of the `pandas` package (see the `pandas` Notebook if you forgot how). Use the `skiprows` and `skipinitialspace` keywords. Carry out the following three tasks:\n", "\n", "* Determine and report the minimum and maximum measured values of the bending strength. \n", @@ -417,7 +417,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. Boxplot of moisture content\n", + "### Exercise 4. Boxplot of moisture content\n", "The moisture content is defined as the mass of moisture in a beam divided by the total mass of the beam (including the moisture) and is recorded as a percentage. Compute and report the mean and standard deviation of the moisture content, and make a box plot." ] }, @@ -453,7 +453,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. Histogram of bending strength\n", + "### Exercise 5. Histogram of bending strength\n", "Create a histogram of the bending strength. Add labels to the axes. Does the histogram look like a Normal distribution? On the same graph draw a red vertical line for the experimentally determined 5% bending strength. Print the 0.05 experimental quantile bending strength to the screen." ] }, @@ -475,7 +475,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. Normal distribution for bending strength\n", + "### Exercise 6. Normal distribution for bending strength\n", "Let's try to fit a normal distribution to the bending strength data. This is obviously not quite correct, as the tail of the Normal distribution will extend below zero to the left, which is unrealistic. If the part of the tail below zero is small, it may be a reasonable first step. Create a normalized histogram of the bending strength. Compute the mean and standard deviation of the bending strength data and plot on the same graph the Normal probability density function using these estimates of the mean and standard deviation. Add a red vertical line for the 5% bending strength according to the data, and a black vertical line for the 5% bending strength according to the fitted Normal distribution" ] }, @@ -504,7 +504,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -542,7 +542,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -573,7 +573,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -596,7 +596,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -626,7 +626,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -648,7 +648,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/notebook10_continuous_random_variables/py_exploratory_comp_10_sol.ipynb b/notebook10_continuous_random_variables/py_exploratory_comp_10_sol.ipynb index 3a16801..70d6d3c 100755 --- a/notebook10_continuous_random_variables/py_exploratory_comp_10_sol.ipynb +++ b/notebook10_continuous_random_variables/py_exploratory_comp_10_sol.ipynb @@ -180,7 +180,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1: First histogram\n", + "### Exercise 1: First histogram\n", "Generate 1000 random numbers from a Normal distribution with mean 100 and standard deviation 10. Compute and print to the screen the mean and standard deviation of your data. Create two graphs above each other using the `plt.subplot` command. In the top graph, plot a histogram using 20 bins going from 50 to 150. Note that with this size of a data set (1000 data points), the histogram starts to look a lot more like the typical bell-shaped curve of a Normal distribution. Add a red line representing the probability density function of the underlying normal distribution to the graph. In the bottom graph, draw a histogram of the cumulative distribution function, by setting the keyword `cumulative=True` (see `plt.hist?` for details). For the latter graph, use the keyword `align='right'` so that the bars are centered on the right bin edges (so that the line you are drawing next will approximately go through the centers of the bars). Add a red line representing the cumulative distribution function of the underlying normal distribution to the graph using the `norm.cdf` function, which works the same as the `norm.pdf` function but computes the cumulative distribution function (cdf). Finally, make sure the limits along the horizontal axis are the same for both graphs. " ] }, @@ -262,7 +262,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Expercise 2. Lower and upper quartile\n", + "### Expercise 2. Lower and upper quartile\n", "Generate 100 data points from a normal distribution with a mean of 20 and a standard deviation of 4. Compute the interquartile range (25%-75% range). Compute the theoretical value of the interquartile range and compare it to the interquartile range of the data. Draw a histogram of the cumulative distribution. Add red vertical lines to your graph for the 0.25 and 0.75 empirical quantiles of the data, and black vertical lines for the 0.25 and 0.75 quantiles of the underlying distribution. Vertical lines that span the graph may be added with the `plt.axvline` function, which takes the $x$ value of the line as an argument. To specify the color of the vertical line, use the `color` keyword argument." ] }, @@ -639,7 +639,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Loading experimental data and basic operations\n", + "### Exercise 3. Loading experimental data and basic operations\n", "Load the data in the file `douglas_data.csv` using the `read_csv` command of the `pandas` package (see the `pandas` Notebook if you forgot how). Use the `skiprows` and `skipinitialspace` keywords. Carry out the following three tasks:\n", "\n", "* Determine and report the minimum and maximum measured values of the bending strength. \n", @@ -665,7 +665,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. Boxplot of moisture content\n", + "### Exercise 4. Boxplot of moisture content\n", "The moisture content is defined as the mass of moisture in a beam divided by the total mass of the beam (including the moisture) and is recorded as a percentage. Compute and report the mean and standard deviation of the moisture content, and make a box plot." ] }, @@ -701,7 +701,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. Histogram of bending strength\n", + "### Exercise 5. Histogram of bending strength\n", "Create a histogram of the bending strength. Add labels to the axes. Does the histogram look like a Normal distribution? On the same graph draw a red vertical line for the experimentally determined 5% bending strength. Print the 0.05 experimental quantile bending strength to the screen." ] }, @@ -723,7 +723,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. Normal distribution for bending strength\n", + "### Exercise 6. Normal distribution for bending strength\n", "Let's try to fit a normal distribution to the bending strength data. This is obviously not quite correct, as the tail of the Normal distribution will extend below zero to the left, which is unrealistic. If the part of the tail below zero is small, it may be a reasonable first step. Create a normalized histogram of the bending strength. Compute the mean and standard deviation of the bending strength data and plot on the same graph the Normal probability density function using these estimates of the mean and standard deviation. Add a red vertical line for the 5% bending strength according to the data, and a black vertical line for the 5% bending strength according to the fitted Normal distribution" ] }, @@ -752,7 +752,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -809,7 +809,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -859,7 +859,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -896,7 +896,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -956,7 +956,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -996,7 +996,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/notebook11_hypothesis_test/py_exploratory_comp_11.ipynb b/notebook11_hypothesis_test/py_exploratory_comp_11.ipynb index 29ea8b3..9024bb1 100644 --- a/notebook11_hypothesis_test/py_exploratory_comp_11.ipynb +++ b/notebook11_hypothesis_test/py_exploratory_comp_11.ipynb @@ -72,7 +72,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Histogram of the means of datasets with 100 values\n", + "### Exercise 1. Histogram of the means of datasets with 100 values\n", "Generate 1000 datasets each with 100 values drawn from a normal distribution with mean 4 and standard deviation 2; use a seed of 22. Compute the mean of each dataset and store them in an array of length 1000. Compute the mean of the means and the standard deviation of the means, and print them to the screen. Draw a boxplot of the means. In a separate figure, draw a histogram of the means. Make sure the vertical axis of the boxplot and the horizontal axis of the histogram extend from 3 to 5." ] }, @@ -94,7 +94,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Histogram of the means of datasets with 1000 values\n", + "### Exercise 2. Histogram of the means of datasets with 1000 values\n", "Repeat exercise 1 but now generate 1000 datasets each with 1000 values (rather than 100 values) drawn from the same normal distribution with mean 4 and standard deviation 2, and again with a seed of 22. Make sure the vertical axis of the boxplot and the horizontal axis of the histogram extend from 3 to 5, so that the graphs can be compared to the graphs you created in the previous exercise. Is the spread of the mean much smaller now as compared to the datasets consisting of only 100 values?" ] }, @@ -170,7 +170,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Count the number of means outside 95 percentile\n", + "### Exercise 3. Count the number of means outside 95 percentile\n", "Go back to Exercise 1. Generate 1000 datasets each with 100 values drawn from a normal distribution with mean 4 and standard deviation 2. For each dataset, evaluate whether the sample mean is within the 95 percentile of the $t$-distribution around the true mean of 4 (the standard deviation of the sample mean is different every time, of course). Count how many times the sample mean is outside the 95 percentile around the true mean of the $t$ distribution. If the theory is correct, it should, of course, be the case for about 5% of the datasets. Try five different seeds and report the percentage of means in the dataset that is outside the 95 percentile around the true mean. " ] }, @@ -192,7 +192,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. $t$ test on dataset of 20 values\n", + "### Exercise 4. $t$ test on dataset of 20 values\n", "Generate 20 datapoints from a Normal distribution with mean 39 and standard deviation 4. Use a seed of 2. Compute and report the sample mean and sample standard deviation of the dataset and the sample standard deviation of the sample mean." ] }, @@ -228,7 +228,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. Hypothesis tests on Wooden beam data\n", + "### Exercise 5. Hypothesis tests on Wooden beam data\n", "Load the data set of experiments on wooden beams stored in the file `douglas_data.csv`. First, consider the first 20 measurements of the bending strength. Compute the sample mean and the standard deviation of the sample mean. The manufacturer claims that the mean bending strength is only 50 N/mm$^2$. Perform a $t$-test (significance level 5%) with null hypothesis that the mean is indeed 50 N/mm$^2$ and alternative hypothesis that the mean is not 50 N/mm$^2$ using the approach applied in Exercise 4." ] }, @@ -329,7 +329,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. Explore Central Limit Theorem for Gamma Distribution\n", + "### Exercise 6. Explore Central Limit Theorem for Gamma Distribution\n", "Generate $N$ datasets of 20 numbers randomly drawn from a Gamma distribution with shape parameter equal to 2 and scale equal to 1. Draw a histogram of the means of the $N$ datasets using 20 bins. On the same graph, draw the pdf of the Normal distribution using the mean of means and sample standard deviation of the means; choose the limits of the $x$-axis between 0 and 4. Make 3 graphs, for $N=100,1000,10000$ and notice that the distribution starts to approach a Normal distribution. Add a title to each graph stating the number of datasets." ] }, @@ -358,7 +358,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -385,7 +385,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -412,7 +412,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -441,7 +441,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -483,7 +483,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -537,7 +537,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/notebook11_hypothesis_test/py_exploratory_comp_11_sol.ipynb b/notebook11_hypothesis_test/py_exploratory_comp_11_sol.ipynb index 959d8df..6515d05 100644 --- a/notebook11_hypothesis_test/py_exploratory_comp_11_sol.ipynb +++ b/notebook11_hypothesis_test/py_exploratory_comp_11_sol.ipynb @@ -84,7 +84,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Histogram of the means of datasets with 100 values\n", + "### Exercise 1. Histogram of the means of datasets with 100 values\n", "Generate 1000 datasets each with 100 values drawn from a normal distribution with mean 4 and standard deviation 2; use a seed of 22. Compute the mean of each dataset and store them in an array of length 1000. Compute the mean of the means and the standard deviation of the means, and print them to the screen. Draw a boxplot of the means. In a separate figure, draw a histogram of the means. Make sure the vertical axis of the boxplot and the horizontal axis of the histogram extend from 3 to 5." ] }, @@ -106,7 +106,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Histogram of the means of datasets with 1000 values\n", + "### Exercise 2. Histogram of the means of datasets with 1000 values\n", "Repeat exercise 1 but now generate 1000 datasets each with 1000 values (rather than 100 values) drawn from the same normal distribution with mean 4 and standard deviation 2, and again with a seed of 22. Make sure the vertical axis of the boxplot and the horizontal axis of the histogram extend from 3 to 5, so that the graphs can be compared to the graphs you created in the previous exercise. Is the spread of the mean much smaller now as compared to the datasets consisting of only 100 values?" ] }, @@ -206,7 +206,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Count the number of means outside 95 percentile\n", + "### Exercise 3. Count the number of means outside 95 percentile\n", "Go back to Exercise 1. Generate 1000 datasets each with 100 values drawn from a normal distribution with mean 4 and standard deviation 2. For each dataset, evaluate whether the sample mean is within the 95 percentile of the $t$-distribution around the true mean of 4 (the standard deviation of the sample mean is different every time, of course). Count how many times the sample mean is outside the 95 percentile around the true mean of the $t$ distribution. If the theory is correct, it should, of course, be the case for about 5% of the datasets. Try five different seeds and report the percentage of means in the dataset that is outside the 95 percentile around the true mean. " ] }, @@ -228,7 +228,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. $t$ test on dataset of 20 values\n", + "### Exercise 4. $t$ test on dataset of 20 values\n", "Generate 20 datapoints from a Normal distribution with mean 39 and standard deviation 4. Use a seed of 2. Compute and report the sample mean and sample standard deviation of the dataset and the sample standard deviation of the sample mean." ] }, @@ -264,7 +264,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. Hypothesis tests on Wooden beam data\n", + "### Exercise 5. Hypothesis tests on Wooden beam data\n", "Load the data set of experiments on wooden beams stored in the file `douglas_data.csv`. First, consider the first 20 measurements of the bending strength. Compute the sample mean and the standard deviation of the sample mean. The manufacturer claims that the mean bending strength is only 50 N/mm$^2$. Perform a $t$-test (significance level 5%) with null hypothesis that the mean is indeed 50 N/mm$^2$ and alternative hypothesis that the mean is not 50 N/mm$^2$ using the approach applied in Exercise 4." ] }, @@ -406,7 +406,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. Explore Central Limit Theorem for Gamma Distribution\n", + "### Exercise 6. Explore Central Limit Theorem for Gamma Distribution\n", "Generate $N$ datasets of 20 numbers randomly drawn from a Gamma distribution with shape parameter equal to 2 and scale equal to 1. Draw a histogram of the means of the $N$ datasets using 20 bins. On the same graph, draw the pdf of the Normal distribution using the mean of means and sample standard deviation of the means; choose the limits of the $x$-axis between 0 and 4. Make 3 graphs, for $N=100,1000,10000$ and notice that the distribution starts to approach a Normal distribution. Add a title to each graph stating the number of datasets." ] }, @@ -435,7 +435,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -501,7 +501,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -567,7 +567,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -608,7 +608,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -671,7 +671,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -761,7 +761,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/notebook12_oop/py_exploratory_comp_12.ipynb b/notebook12_oop/py_exploratory_comp_12.ipynb index b2f2eba..f93f818 100644 --- a/notebook12_oop/py_exploratory_comp_12.ipynb +++ b/notebook12_oop/py_exploratory_comp_12.ipynb @@ -291,7 +291,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. A Circle class\n", + "### Exercise 1. A Circle class\n", "Create a `Circle` class that stores the $(x,y)$ coordinates of the center of a circle and the radius $R$ of the circle. Class functions are: `area`, which returns the area of the circle and `plot`, which plots a filled circle using a transparancy of 0.5 (i.e., use keyword argument `alpha=0.5` in the `fill` function). The input argument of the `plot` function is a radius $R^*$. When the radius of the circle is less than $R^*$, fill the circle with the color blue. When the radius of the circle is larger than or equal to $R^*$, fill the circle with the color red. Demonstrate that your `Circle` class works by loading the text file `circle_data.txt`. This file contains the $x$, $y$, and $R$ data for 20 circles. Compute and report the total area of all the circles in the file. Make a plot of the 20 circles to demonstrate that the colors are assigned correctly, using $R^*=1$." ] }, @@ -313,7 +313,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Flow to a set of extraction and injection wells\n", + "### Exercise 2. Flow to a set of extraction and injection wells\n", "Consider an extraction well with a discharge $Q$. The well is located at $(x,y)=(x_w,y_w)$ in an aquifer with thickness $H$ and hydraulic conductivity $k$. The head at a radial distance $R$ from the well is fixed to $h_0$. The groundwater head $h$ decreases towards the well as (e.g., Eq. 11.4 in Verruijt (2012) Soil Mechanics)\n", "\n", "$$h = \\frac{Q}{2\\pi kH} \\ln\\left(\\frac{r}{R}\\right) + h_0$$\n", @@ -530,7 +530,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Matplotlib patches\n", + "### Exercise 3. Matplotlib patches\n", "Write Python code to create the figure shown below using `matplotlib` patches. Use colors of your own choice. Write 'circle', 'ellipse', etc. inside the corresponding patches using the `ax.text` function.\n", "\n", "\"Patches\"" @@ -554,7 +554,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. 3D - Spiral\n", + "### Exercise 4. 3D - Spiral\n", "\n", "Consider the $x$, $y$, $z$ coordinates of a three-dimensional spiral\n", "\n", @@ -594,7 +594,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -640,7 +640,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -688,7 +688,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -731,7 +731,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { diff --git a/notebook12_oop/py_exploratory_comp_12_sol.ipynb b/notebook12_oop/py_exploratory_comp_12_sol.ipynb index 8fee3e8..c711d95 100644 --- a/notebook12_oop/py_exploratory_comp_12_sol.ipynb +++ b/notebook12_oop/py_exploratory_comp_12_sol.ipynb @@ -390,7 +390,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. A Circle class\n", + "### Exercise 1. A Circle class\n", "Create a `Circle` class that stores the $(x,y)$ coordinates of the center of a circle and the radius $R$ of the circle. Class functions are: `area`, which returns the area of the circle and `plot`, which plots a filled circle using a transparancy of 0.5 (i.e., use keyword argument `alpha=0.5` in the `fill` function). The input argument of the `plot` function is a radius $R^*$. When the radius of the circle is less than $R^*$, fill the circle with the color blue. When the radius of the circle is larger than or equal to $R^*$, fill the circle with the color red. Demonstrate that your `Circle` class works by loading the text file `circle_data.txt`. This file contains the $x$, $y$, and $R$ data for 20 circles. Compute and report the total area of all the circles in the file. Make a plot of the 20 circles to demonstrate that the colors are assigned correctly, using $R^*=1$." ] }, @@ -412,7 +412,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Flow to a set of extraction and injection wells\n", + "### Exercise 2. Flow to a set of extraction and injection wells\n", "Consider an extraction well with a discharge $Q$. The well is located at $(x,y)=(x_w,y_w)$ in an aquifer with thickness $H$ and hydraulic conductivity $k$. The head at a radial distance $R$ from the well is fixed to $h_0$. The groundwater head $h$ decreases towards the well as (e.g., Eq. 11.4 in Verruijt (2012) Soil Mechanics)\n", "\n", "$$h = \\frac{Q}{2\\pi kH} \\ln\\left(\\frac{r}{R}\\right) + h_0$$\n", @@ -725,7 +725,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Matplotlib patches\n", + "### Exercise 3. Matplotlib patches\n", "Write Python code to create the figure shown below using `matplotlib` patches. Use colors of your own choice. Write 'circle', 'ellipse', etc. inside the corresponding patches using the `ax.text` function.\n", "\n", "\"Patches\"" @@ -749,7 +749,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. 3D - Spiral\n", + "### Exercise 4. 3D - Spiral\n", "\n", "Consider the $x$, $y$, $z$ coordinates of a three-dimensional spiral\n", "\n", @@ -789,7 +789,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -853,7 +853,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -919,7 +919,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -973,7 +973,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { diff --git a/notebook13_regression1/py_exploratory_comp_13.ipynb b/notebook13_regression1/py_exploratory_comp_13.ipynb index 37d5a01..12e970c 100644 --- a/notebook13_regression1/py_exploratory_comp_13.ipynb +++ b/notebook13_regression1/py_exploratory_comp_13.ipynb @@ -56,7 +56,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Fit a straight line\n", + "### Exercise 1. Fit a straight line\n", "Load the $x,y$ values of 20 data points from the file `xydatafit.dat`. Fit a straight line through the data using the `linregress` function of `scipy.stats`. Note that the `linregress` function returns 3 other values beyond the slope and intercept (use `linregress?` to find out); more on these 3 additional values later on in this Notebook. Plot the data and the fitted straight line. Add a legend. Add the root mean square error as a title to the graph. Print the optimal values for the slope and intercept of the straight line to the screen." ] }, @@ -78,7 +78,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Fit a polynomial\n", + "### Exercise 2. Fit a polynomial\n", "Use the $x,y$ values of 20 data points from the file `xydatafit.dat`. Fit a second degree polynomial (a parabola) through the data using the `np.polyfit` function. Plot the data and the fitted parabola. Add a legend. Report the root mean squared error in the title. Did the root mean squared error improve?" ] }, @@ -144,7 +144,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Fit an exponential function with `curve_fit`\n", + "### Exercise 3. Fit an exponential function with `curve_fit`\n", "Use the $x,y$ values of 20 data points from the file `xydatafit.dat`. Fit the function $f(x) = A\\exp(ax) + b$ through the data using the `curve_fit` function of `scipy.optimize`. Plot the data and the fitted function. Report the root mean squared error in the title. Did the root means squared error improve?" ] }, @@ -297,7 +297,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. Fitting a straight line revisited\n", + "### Exercise 4. Fitting a straight line revisited\n", "Compute the optimal values (in the least squares sense) of $a$ and $b$ using the two equations derived above and the corresponding sum of squared errors (using the `xdata` and `ydata` arrays for the three points given above). Next, use the `linregress` function of the `scipy.stats` package to compute the optimal values and verify that the `linregress` function gives the same answers. Create a graph that shows the three data points and the fitted straight line." ] }, @@ -365,7 +365,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. Verification of goodness of fit parameters\n", + "### Exercise 5. Verification of goodness of fit parameters\n", "Implement the equations for $r^2$ and $s$ given above to verify that the values returned by the `linregress` function are correct. Perform a two-sided hypothesis test with significance level 5% where the null hypothesis is that the slope of the fitted line is zero and the alternative hypothesis is that the slope is not zero. Draw the probability density function of a $t$-distribution with mean 0 and standard deviation equal to the computed value of $s$. Use $N-2$ as the number of degrees of freedom (You subtract the number of parameters from $N$ as you used up these two degrees of freedom). Draw red vertical lines indicating the 2.5% and 97.5% percentiles according to the $t$-distribution. Draw a heavy black vertical line at the position of the computed value of the slope. Decide whether you can reject the null hypothesis that the slope is zero in favor of the alternative hypothesis that the slope is not 0 and add that as a title to the figure." ] }, @@ -452,7 +452,7 @@ } }, "source": [ - "### Exercise 6. Getting a value of $p<0.05$ by chance\n", + "### Exercise 6. Getting a value of $p<0.05$ by chance\n", "Perform the following experiment: Generate 100 $x$ values randomly from a uniform distribution between 0 and 10 using the `np.random.rand` function. Generate 100 $y$ values randomly from a uniform distribution between 0 and 10. Fit a straight line using `linregress`. Perform the experiment 1000 time and count the number of times that the $p$-value is smaller than 0.05. As you will see, you will get approximately 50 out of the 1000 experiments where a line is fitted with a $p$-value smaller than 0.05 just by chance (as there really is no correlation between the random $x$ and $y$ values). " ] }, @@ -481,7 +481,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -509,7 +509,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -535,7 +535,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -566,7 +566,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -628,7 +628,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -678,7 +678,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/notebook13_regression1/py_exploratory_comp_13_sol.ipynb b/notebook13_regression1/py_exploratory_comp_13_sol.ipynb index 7a083a7..b296763 100644 --- a/notebook13_regression1/py_exploratory_comp_13_sol.ipynb +++ b/notebook13_regression1/py_exploratory_comp_13_sol.ipynb @@ -56,7 +56,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Fit a straight line\n", + "### Exercise 1. Fit a straight line\n", "Load the $x,y$ values of 20 data points from the file `xydatafit.dat`. Fit a straight line through the data using the `linregress` function of `scipy.stats`. Note that the `linregress` function returns 3 other values beyond the slope and intercept (use `linregress?` to find out); more on these 3 additional values later on in this Notebook. Plot the data and the fitted straight line. Add a legend. Add the root mean square error as a title to the graph. Print the optimal values for the slope and intercept of the straight line to the screen." ] }, @@ -78,7 +78,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Fit a polynomial\n", + "### Exercise 2. Fit a polynomial\n", "Use the $x,y$ values of 20 data points from the file `xydatafit.dat`. Fit a second degree polynomial (a parabola) through the data using the `np.polyfit` function. Plot the data and the fitted parabola. Add a legend. Report the root mean squared error in the title. Did the root mean squared error improve?" ] }, @@ -152,7 +152,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Fit an exponential function with `curve_fit`\n", + "### Exercise 3. Fit an exponential function with `curve_fit`\n", "Use the $x,y$ values of 20 data points from the file `xydatafit.dat`. Fit the function $f(x) = A\\exp(ax) + b$ through the data using the `curve_fit` function of `scipy.optimize`. Plot the data and the fitted function. Report the root mean squared error in the title. Did the root means squared error improve?" ] }, @@ -336,7 +336,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. Fitting a straight line revisited\n", + "### Exercise 4. Fitting a straight line revisited\n", "Compute the optimal values (in the least squares sense) of $a$ and $b$ using the two equations derived above and the corresponding sum of squared errors (using the `xdata` and `ydata` arrays for the three points given above). Next, use the `linregress` function of the `scipy.stats` package to compute the optimal values and verify that the `linregress` function gives the same answers. Create a graph that shows the three data points and the fitted straight line." ] }, @@ -404,7 +404,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. Verification of goodness of fit parameters\n", + "### Exercise 5. Verification of goodness of fit parameters\n", "Implement the equations for $r^2$ and $s$ given above to verify that the values returned by the `linregress` function are correct. Perform a two-sided hypothesis test with significance level 5% where the null hypothesis is that the slope of the fitted line is zero and the alternative hypothesis is that the slope is not zero. Draw the probability density function of a $t$-distribution with mean 0 and standard deviation equal to the computed value of $s$. Use $N-2$ as the number of degrees of freedom (You subtract the number of parameters from $N$ as you used up these two degrees of freedom). Draw red vertical lines indicating the 2.5% and 97.5% percentiles according to the $t$-distribution. Draw a heavy black vertical line at the position of the computed value of the slope. Decide whether you can reject the null hypothesis that the slope is zero in favor of the alternative hypothesis that the slope is not 0 and add that as a title to the figure." ] }, @@ -521,7 +521,7 @@ } }, "source": [ - "### Exercise 6. Getting a value of $p<0.05$ by chance\n", + "### Exercise 6. Getting a value of $p<0.05$ by chance\n", "Perform the following experiment: Generate 100 $x$ values randomly from a uniform distribution between 0 and 10 using the `np.random.rand` function. Generate 100 $y$ values randomly from a uniform distribution between 0 and 10. Fit a straight line using `linregress`. Perform the experiment 1000 time and count the number of times that the $p$-value is smaller than 0.05. As you will see, you will get approximately 50 out of the 1000 experiments where a line is fitted with a $p$-value smaller than 0.05 just by chance (as there really is no correlation between the random $x$ and $y$ values). " ] }, @@ -550,7 +550,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -596,7 +596,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -633,7 +633,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -675,7 +675,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -765,7 +765,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -844,7 +844,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/notebook1_basics_plotting/py_exploratory_comp_1.ipynb b/notebook1_basics_plotting/py_exploratory_comp_1.ipynb index 9a68dea..9165e15 100644 --- a/notebook1_basics_plotting/py_exploratory_comp_1.ipynb +++ b/notebook1_basics_plotting/py_exploratory_comp_1.ipynb @@ -107,7 +107,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1a, First Python code\n", + "### Exercise 1a, First Python code\n", "Compute the value of the polynomial $y=ax^2+bx+c$ at $x=-2$, $x=0$, and $x=2.1$ using $a=1$, $b=1$, $c=-6$ and print the results to the screen." ] }, @@ -183,7 +183,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1b, First Python code using f-strings\n", + "### Exercise 1b, First Python code using f-strings\n", "Compute the value of the polynomial $y=ax^2+bx+c$ at $x=-2$, $x=0$, and $x=2.1$ using $a=1$, $b=1$, $c=-6$ and print the results to the screen using f-strings and 2 decimal places." ] }, @@ -403,7 +403,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2, First graph\n", + "### Exercise 2, First graph\n", "Plot $y=(x+2)(x-1)(x-2)$ for $x$ going from $-3$ to $+3$ using a dashed red line. On the same figure, plot a blue circle for every point where $y$ equals zero. Set the size of the markers to 10 (you may need to read the help of `plt.plot` to find out how to do that). Label the axes as 'x-axis' and 'y-axis'. Add the title 'First nice Python figure of Your Name', where you enter your own name." ] }, @@ -502,7 +502,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3, Loading data and adding a legend\n", + "### Exercise 3, Loading data and adding a legend\n", "You are provided with the data files containing the mean montly temperature of Holland, New York City, and Beijing. The Dutch data is stored in `holland_temperature.dat`, and the other filenames are similar. Plot the temperature for each location against the number of the month (starting with 1 for January) all in a single graph. Add a legend by using the function `plt.legend(['line1','line2'])`, etc., but then with more descriptive names. Find out about the `legend` command using `plt.legend?`. Place the legend in an appropriate spot (the upper left-hand corner may be nice, or let Python figure out the best place). " ] }, @@ -524,7 +524,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4, Subplots and fancy tick markers\n", + "### Exercise 4, Subplots and fancy tick markers\n", "Load the average monthly air temperature and seawater temperature for Holland. Create one plot with two graphs above each other using the `subplot` command (use `plt.subplot?` to find out how). On the top graph, plot the air and sea temperature. Label the ticks on the horizontal axis as 'jan', 'feb', 'mar', etc., rather than numbers. Use `plt.xticks?` to find out how. In the bottom graph, plot the difference between the air and seawater temperature. Add legends, axes labels, the whole shebang." ] }, @@ -613,7 +613,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5, Pie Chart\n", + "### Exercise 5, Pie Chart\n", "At the 2012 London Olympics, the top ten countries (plus the rest) receiving gold medals were `['USA', 'CHN', 'GBR', 'RUS', 'KOR', 'GER', 'FRA', 'ITA', 'HUN', 'AUS', 'OTHER']`. They received `[46, 38, 29, 24, 13, 11, 11, 8, 8, 7, 107]` gold medals, respectively. Make a pie chart (use `plt.pie?` or go to the pie charts in the matplotlib gallery) of the top 10 gold medal winners plus the others at the London Olympics. Try some of the keyword arguments to make the plot look nice. You may want to give the command `plt.axis('equal')` to make the scales along the horizontal and vertical axes equal so that the pie actually looks like a circle rather than an ellipse. Use the `colors` keyword in your pie chart to specify a sequence of colors. The sequence must be between square brackets, each color must be between quotes preserving upper and lower cases, and they must be separated by comma's like `['MediumBlue','SpringGreen','BlueViolet']`; the sequence is repeated if it is not long enough." ] }, @@ -635,7 +635,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6, Fill between\n", + "### Exercise 6, Fill between\n", "Load the air and sea temperature, as used in Exercise 4, but this time make one plot of temperature vs the number of the month and use the `plt.fill_between` command to fill the space between the curve and the horizontal axis. Specify the `alpha` keyword, which defines the transparancy. Some experimentation will give you a good value for alpha (stay between 0 and 1). Note that you need to specify the color using the `color` keyword argument." ] }, @@ -664,7 +664,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1" + "Answer to Exercise 1" ] }, { @@ -693,7 +693,7 @@ "source": [ "Back to Exercise 1a\n", "\n", - "Answer to Exercise 1b" + "Answer to Exercise 1b" ] }, { @@ -722,7 +722,7 @@ "source": [ "Back to Exercise 1b\n", "\n", - "Answer to Exercise 2" + "Answer to Exercise 2" ] }, { @@ -746,7 +746,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answer to Exercise 3" + "Answer to Exercise 3" ] }, { @@ -773,7 +773,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answer to Exercise 4" + "Answer to Exercise 4" ] }, { @@ -805,7 +805,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answer to Exercise 5" + "Answer to Exercise 5" ] }, { @@ -826,7 +826,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answer to Exercise 6" + "Answer to Exercise 6" ] }, { @@ -851,6 +851,11 @@ "source": [ "Back to Exercise 6" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] } ], "metadata": { diff --git a/notebook1_basics_plotting/py_exploratory_comp_1_sol.ipynb b/notebook1_basics_plotting/py_exploratory_comp_1_sol.ipynb index 674a646..54e1fcb 100644 --- a/notebook1_basics_plotting/py_exploratory_comp_1_sol.ipynb +++ b/notebook1_basics_plotting/py_exploratory_comp_1_sol.ipynb @@ -159,7 +159,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1a, First Python code\n", + "### Exercise 1a, First Python code\n", "Compute the value of the polynomial $y=ax^2+bx+c$ at $x=-2$, $x=0$, and $x=2.1$ using $a=1$, $b=1$, $c=-6$ and print the results to the screen." ] }, @@ -261,7 +261,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1b, First Python code using f-strings\n", + "### Exercise 1b, First Python code using f-strings\n", "Compute the value of the polynomial $y=ax^2+bx+c$ at $x=-2$, $x=0$, and $x=2.1$ using $a=1$, $b=1$, $c=-6$ and print the results to the screen using f-strings and 2 decimal places." ] }, @@ -572,7 +572,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2, First graph\n", + "### Exercise 2, First graph\n", "Plot $y=(x+2)(x-1)(x-2)$ for $x$ going from $-3$ to $+3$ using a dashed red line. On the same figure, plot a blue circle for every point where $y$ equals zero. Set the size of the markers to 10 (you may need to read the help of `plt.plot` to find out how to do that). Label the axes as 'x-axis' and 'y-axis'. Add the title 'First nice Python figure of Your Name', where you enter your own name." ] }, @@ -703,7 +703,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3, Loading data and adding a legend\n", + "### Exercise 3, Loading data and adding a legend\n", "You are provided with the data files containing the mean montly temperature of Holland, New York City, and Beijing. The Dutch data is stored in `holland_temperature.dat`, and the other filenames are similar. Plot the temperature for each location against the number of the month (starting with 1 for January) all in a single graph. Add a legend by using the function `plt.legend(['line1','line2'])`, etc., but then with more descriptive names. Find out about the `legend` command using `plt.legend?`. Place the legend in an appropriate spot (the upper left-hand corner may be nice, or let Python figure out the best place). " ] }, @@ -725,7 +725,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4, Subplots and fancy tick markers\n", + "### Exercise 4, Subplots and fancy tick markers\n", "Load the average monthly air temperature and seawater temperature for Holland. Create one plot with two graphs above each other using the `subplot` command (use `plt.subplot?` to find out how). On the top graph, plot the air and sea temperature. Label the ticks on the horizontal axis as 'jan', 'feb', 'mar', etc., rather than numbers. Use `plt.xticks?` to find out how. In the bottom graph, plot the difference between the air and seawater temperature. Add legends, axes labels, the whole shebang." ] }, @@ -847,7 +847,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5, Pie Chart\n", + "### Exercise 5, Pie Chart\n", "At the 2012 London Olympics, the top ten countries (plus the rest) receiving gold medals were `['USA', 'CHN', 'GBR', 'RUS', 'KOR', 'GER', 'FRA', 'ITA', 'HUN', 'AUS', 'OTHER']`. They received `[46, 38, 29, 24, 13, 11, 11, 8, 8, 7, 107]` gold medals, respectively. Make a pie chart (use `plt.pie?` or go to the pie charts in the matplotlib gallery) of the top 10 gold medal winners plus the others at the London Olympics. Try some of the keyword arguments to make the plot look nice. You may want to give the command `plt.axis('equal')` to make the scales along the horizontal and vertical axes equal so that the pie actually looks like a circle rather than an ellipse. Use the `colors` keyword in your pie chart to specify a sequence of colors. The sequence must be between square brackets, each color must be between quotes preserving upper and lower cases, and they must be separated by comma's like `['MediumBlue','SpringGreen','BlueViolet']`; the sequence is repeated if it is not long enough." ] }, @@ -869,7 +869,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6, Fill between\n", + "### Exercise 6, Fill between\n", "Load the air and sea temperature, as used in Exercise 4, but this time make one plot of temperature vs the number of the month and use the `plt.fill_between` command to fill the space between the curve and the horizontal axis. Specify the `alpha` keyword, which defines the transparancy. Some experimentation will give you a good value for alpha (stay between 0 and 1). Note that you need to specify the color using the `color` keyword argument." ] }, @@ -898,7 +898,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1" + "Answer to Exercise 1" ] }, { @@ -937,7 +937,7 @@ "source": [ "Back to Exercise 1a\n", "\n", - "Answer to Exercise 1b" + "Answer to Exercise 1b" ] }, { @@ -976,7 +976,7 @@ "source": [ "Back to Exercise 1b\n", "\n", - "Answer to Exercise 2" + "Answer to Exercise 2" ] }, { @@ -1011,7 +1011,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answer to Exercise 3" + "Answer to Exercise 3" ] }, { @@ -1049,7 +1049,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answer to Exercise 4" + "Answer to Exercise 4" ] }, { @@ -1092,7 +1092,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answer to Exercise 5" + "Answer to Exercise 5" ] }, { @@ -1124,7 +1124,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answer to Exercise 6" + "Answer to Exercise 6" ] }, { diff --git a/notebook2_arrays/py_exploratory_comp_2.ipynb b/notebook2_arrays/py_exploratory_comp_2.ipynb index 565ce87..6c86a26 100644 --- a/notebook2_arrays/py_exploratory_comp_2.ipynb +++ b/notebook2_arrays/py_exploratory_comp_2.ipynb @@ -178,7 +178,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1, Arrays and indices\n", + "### Exercise 1, Arrays and indices\n", "Create an array of zeros with length 20. Change the first 5 values to 10. Change the next 10 values to a sequence starting at 12 and increasig with steps of 2 to 30 (do this with one command). Set the final 5 values to 30. Plot the value of the array on the $y$-axis vs. the index of the array on the $x$-axis. Draw vertical dashed lines at $x=4$ and $x=14$ (i.e., the section between the dashed lines is where the line increases from 10 to 30). Set the minimum and maximum values of the $y$-axis to 8 and 32 using the `ylim` command." ] }, @@ -325,7 +325,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2, Two-dimensional array indices\n", + "### Exercise 2, Two-dimensional array indices\n", "For the array `x` shown below, write code to print: \n", "\n", "* the first row of `x`\n", @@ -400,7 +400,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3, Create and visualize an array\n", + "### Exercise 3, Create and visualize an array\n", "Create an array of size 10 by 10. Set the upper left-hand quadrant of the array should to 4, the upper right-hand quadrant to 3, the lower right-hand quadrant t0 2 and the lower left-hand quadrant to 1. First create an array of 10 by 10 using the `zeros` command, then fill each quadrant by specifying the correct index ranges. Visualize the array using `matshow`. It should give a red, yellow, light blue and dark blue box (clock-wise starting from upper left) when you use the `jet` colormap." ] }, @@ -422,8 +422,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4, Create and visualize a slightly fancier array\n", - "Consider the image shown below, which roughly shows the letters TU. You are asked to create an array that represents the same TU. First create a zeros array of 11 rows and 17 columns. Give the background value 0, the letter T value -1, and the letter U value +1. Use the `jet` colormap. \n", + "### Exercise 4, Create and visualize a slightly fancier array\n", + "Consider the image shown below, which roughly shows the letters TU. You are asked to create an array that represents the same TU. First create a zeros array of 11 rows and 17 columns. Give the background value 0, the letter T value -1, and the letter U value +1. Use the `jet` colormap. \n", "\n", "![](tufig.png)" ] @@ -581,7 +581,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5, Replace high and low values in an array\n", + "### Exercise 5, Replace high and low values in an array\n", "Create an array for variable $x$ consisting of 100 values from 0 to 20. Compute $y=\\sin(x)$ and plot $y$ vs. $x$ with a blue line. Next, replace all values of $y$ that are larger than 0.5 by 0.5, and all values that are smaller than $-$0.75 by $-$0.75, and plot the modified $y$ values vs. $x$ using a red line on the same graph. " ] }, @@ -598,7 +598,7 @@ "source": [ "Answer to Exercise 5\n", "\n", - "### Exercise 6, Change marker color based on data value\n", + "### Exercise 6, Change marker color based on data value\n", "Create an array for variable $x$ consisting of 100 points from 0 to 20 and compute $y=\\sin(x)$. Plot a blue dot for every $y$ that is larger than zero, and a red dot otherwise" ] }, @@ -640,7 +640,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 7, Multiple conditions \n", + "### Exercise 7, Multiple conditions \n", "The file `xypoints.dat` contains 1000 randomly chosen $x,y$ locations of points; both $x$ and $y$ vary between -10 and 10. Load the data using `loadtxt`, and store the first row of the array in an array called `x` and the second row in an array called `y`. First, plot a red dot for all points. On the same graph, plot a blue dot for all $x,y$ points where $x<-2$ and $-5\\le y \\le 0$. Finally, plot a green dot for any point that lies in the circle with center $(x_c,y_c)=(5,0)$ and with radius $R=5$. Hint: it may be useful to compute a new array for the radial distance $r$ between any point and the center of the circle using the formula $r=\\sqrt{(x-x_c)^2+(y-y_c)^2}$. Use the `plt.axis('equal')` command to make sure the scales along the two axes are equal and the circular area looks like a circle." ] }, @@ -662,7 +662,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 8, Fix the error \n", + "### Exercise 8, Fix the error \n", "In the code below, it is meant to give the last 5 values of the array `x` the values [50, 52, 54, 56, 58] and print the result to the screen, but there are some errors in the code. Remove the comment markers and run the code to see the error message. Then fix the code and run it again." ] }, @@ -695,7 +695,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1" + "Answer to Exercise 1" ] }, { @@ -720,7 +720,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answer to Exercise 2" + "Answer to Exercise 2" ] }, { @@ -753,7 +753,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answer to Exercise 3" + "Answer to Exercise 3" ] }, { @@ -778,7 +778,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answer to Exercise 4" + "Answer to Exercise 4" ] }, { @@ -807,7 +807,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answer to Exercise 5" + "Answer to Exercise 5" ] }, { @@ -830,7 +830,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answer to Exercise 6" + "Answer to Exercise 6" ] }, { @@ -851,7 +851,7 @@ "source": [ "Back to Exercise 6\n", "\n", - "Answer to Exercise 7" + "Answer to Exercise 7" ] }, { @@ -874,7 +874,7 @@ "source": [ "Back to Exercise 7\n", "\n", - "Answer to Exercise 8" + "Answer to Exercise 8" ] }, { diff --git a/notebook2_arrays/py_exploratory_comp_2_sol.ipynb b/notebook2_arrays/py_exploratory_comp_2_sol.ipynb index 1540f07..2ea48a1 100644 --- a/notebook2_arrays/py_exploratory_comp_2_sol.ipynb +++ b/notebook2_arrays/py_exploratory_comp_2_sol.ipynb @@ -250,7 +250,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1, Arrays and indices\n", + "### Exercise 1, Arrays and indices\n", "Create an array of zeros with length 20. Change the first 5 values to 10. Change the next 10 values to a sequence starting at 12 and increasig with steps of 2 to 30 (do this with one command). Set the final 5 values to 30. Plot the value of the array on the $y$-axis vs. the index of the array on the $x$-axis. Draw vertical dashed lines at $x=4$ and $x=14$ (i.e., the section between the dashed lines is where the line increases from 10 to 30). Set the minimum and maximum values of the $y$-axis to 8 and 32 using the `ylim` command." ] }, @@ -462,7 +462,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2, Two-dimensional array indices\n", + "### Exercise 2, Two-dimensional array indices\n", "For the array `x` shown below, write code to print: \n", "\n", "* the first row of `x`\n", @@ -569,7 +569,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3, Create and visualize an array\n", + "### Exercise 3, Create and visualize an array\n", "Create an array of size 10 by 10. Set the upper left-hand quadrant of the array should to 4, the upper right-hand quadrant to 3, the lower right-hand quadrant t0 2 and the lower left-hand quadrant to 1. First create an array of 10 by 10 using the `zeros` command, then fill each quadrant by specifying the correct index ranges. Visualize the array using `matshow`. It should give a red, yellow, light blue and dark blue box (clock-wise starting from upper left) when you use the `jet` colormap." ] }, @@ -591,8 +591,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4, Create and visualize a slightly fancier array\n", - "Consider the image shown below, which roughly shows the letters TU. You are asked to create an array that represents the same TU. First create a zeros array of 11 rows and 17 columns. Give the background value 0, the letter T value -1, and the letter U value +1. Use the `jet` colormap. \n", + "### Exercise 4, Create and visualize a slightly fancier array\n", + "Consider the image shown below, which roughly shows the letters TU. You are asked to create an array that represents the same TU. First create a zeros array of 11 rows and 17 columns. Give the background value 0, the letter T value -1, and the letter U value +1. Use the `jet` colormap. \n", "\n", "![](tufig.png)" ] @@ -819,7 +819,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5, Replace high and low values in an array\n", + "### Exercise 5, Replace high and low values in an array\n", "Create an array for variable $x$ consisting of 100 values from 0 to 20. Compute $y=\\sin(x)$ and plot $y$ vs. $x$ with a blue line. Next, replace all values of $y$ that are larger than 0.5 by 0.5, and all values that are smaller than $-$0.75 by $-$0.75, and plot the modified $y$ values vs. $x$ using a red line on the same graph. " ] }, @@ -836,7 +836,7 @@ "source": [ "Answer to Exercise 5\n", "\n", - "### Exercise 6, Change marker color based on data value\n", + "### Exercise 6, Change marker color based on data value\n", "Create an array for variable $x$ consisting of 100 points from 0 to 20 and compute $y=\\sin(x)$. Plot a blue dot for every $y$ that is larger than zero, and a red dot otherwise" ] }, @@ -889,7 +889,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 7, Multiple conditions \n", + "### Exercise 7, Multiple conditions \n", "The file `xypoints.dat` contains 1000 randomly chosen $x,y$ locations of points; both $x$ and $y$ vary between -10 and 10. Load the data using `loadtxt`, and store the first row of the array in an array called `x` and the second row in an array called `y`. First, plot a red dot for all points. On the same graph, plot a blue dot for all $x,y$ points where $x<-2$ and $-5\\le y \\le 0$. Finally, plot a green dot for any point that lies in the circle with center $(x_c,y_c)=(5,0)$ and with radius $R=5$. Hint: it may be useful to compute a new array for the radial distance $r$ between any point and the center of the circle using the formula $r=\\sqrt{(x-x_c)^2+(y-y_c)^2}$. Use the `plt.axis('equal')` command to make sure the scales along the two axes are equal and the circular area looks like a circle." ] }, @@ -911,7 +911,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 8, Fix the error \n", + "### Exercise 8, Fix the error \n", "In the code below, it is meant to give the last 5 values of the array `x` the values [50, 52, 54, 56, 58] and print the result to the screen, but there are some errors in the code. Remove the comment markers and run the code to see the error message. Then fix the code and run it again." ] }, @@ -944,7 +944,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1" + "Answer to Exercise 1" ] }, { @@ -980,7 +980,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answer to Exercise 2" + "Answer to Exercise 2" ] }, { @@ -1037,7 +1037,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answer to Exercise 3" + "Answer to Exercise 3" ] }, { @@ -1089,7 +1089,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answer to Exercise 4" + "Answer to Exercise 4" ] }, { @@ -1146,7 +1146,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answer to Exercise 5" + "Answer to Exercise 5" ] }, { @@ -1180,7 +1180,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answer to Exercise 6" + "Answer to Exercise 6" ] }, { @@ -1212,7 +1212,7 @@ "source": [ "Back to Exercise 6\n", "\n", - "Answer to Exercise 7" + "Answer to Exercise 7" ] }, { @@ -1246,7 +1246,7 @@ "source": [ "Back to Exercise 7\n", "\n", - "Answer to Exercise 8" + "Answer to Exercise 8" ] }, { diff --git a/notebook3_for_and_if/py_exploratory_comp_3.ipynb b/notebook3_for_and_if/py_exploratory_comp_3.ipynb index 822dae8..682a1f2 100644 --- a/notebook3_for_and_if/py_exploratory_comp_3.ipynb +++ b/notebook3_for_and_if/py_exploratory_comp_3.ipynb @@ -150,7 +150,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. First `for` loop\n", + "### Exercise 1. First `for` loop\n", "Create a list with the names of the months. Create a second list with the number of days in each month (for a regular year). Create a `for` loop that prints:\n", "\n", "`The number of days in MONTH is XX days`\n", @@ -275,7 +275,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Combination of `for` loop with `if` statement\n", + "### Exercise 2. Combination of `for` loop with `if` statement\n", "Consider the function\n", "\n", "$\\begin{split}\n", @@ -304,7 +304,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Load and loop through temperature data\n", + "### Exercise 3. Load and loop through temperature data\n", "Load the temperature data for Holland from the data file `holland_temperature.dat`. Loop through all monthly temperatures and print a message that includes the month number and states whether the monthly average temperature is above or below 10 degrees" ] }, @@ -363,7 +363,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. Running total\n", + "### Exercise 4. Running total\n", "For the data of the previous example, compute the running total and store it in an array using a loop. Hence, the result should be an array with the same length as `data` where item `i` is the sum of all values in the array `data` up to and including `data[i]`. Print both the array `data` and the array with the running total to the screen. Finally, check your answer by using the `cumsum` function of `numpy`, which should give the same answer as your loop." ] }, @@ -426,7 +426,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. Month closest to 15 degrees\n", + "### Exercise 5. Month closest to 15 degrees\n", "Find the month in which the average monthly temperature in Holland is closest to 15 degrees (use the data from Exercise 3). Apply the approach described above by looping through all values and use an `if` statement inside the loop. You may also want to use the `abs` function to compute the absolute value of a number. Check your answer by using a few `numpy` methods that don't require a loop and an `if` statement." ] }, @@ -547,7 +547,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6, Oil data\n", + "### Exercise 6, Oil data\n", "The file `oil_price.dat` contains the monthly oil price since 1985. The file contains three columns: year, month, price in Euros (from the european bank website). Make a plot of the oil price (put numbers along the horizontal axis; we will learn how to do dates in another notebook) and determine the month and year the oil price first rose above 40 euros, above 60 euros, and above 80 euros. You need to write to the screen something like `The oil price exceeds 40 euros for the first time in month xx of year yyyy` where `xx` and `yyyy` are the correct month and year. Use a double loop. Can you modify the code such that it doesn't print the number of the month but the name of the month?" ] }, @@ -672,7 +672,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 7. Find the position of your name in a list\n", + "### Exercise 7. Find the position of your name in a list\n", "Use a loop to figure out between which two names your name falls in the list\n", "`['Aaldrich', 'Babette', 'Chris', 'Franka', 'Joe', 'Louisa', 'Pierre', 'Simone', 'Tarek', 'Yvonne', 'Zu']` and print the result to the screen." ] @@ -702,7 +702,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1" + "Answer to Exercise 1" ] }, { @@ -725,7 +725,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answer to Exercise 2" + "Answer to Exercise 2" ] }, { @@ -751,7 +751,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answer to Exercise 3" + "Answer to Exercise 3" ] }, { @@ -774,7 +774,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answer to Exercise 4" + "Answer to Exercise 4" ] }, { @@ -799,7 +799,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answer to Exercise 5" + "Answer to Exercise 5" ] }, { @@ -828,7 +828,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answer to Exercise 6" + "Answer to Exercise 6" ] }, { @@ -857,7 +857,7 @@ "source": [ "Back to Exercise 6\n", "\n", - "Answer to Exercise 7" + "Answer to Exercise 7" ] }, { diff --git a/notebook3_for_and_if/py_exploratory_comp_3_sol.ipynb b/notebook3_for_and_if/py_exploratory_comp_3_sol.ipynb index ec22209..8fe1375 100644 --- a/notebook3_for_and_if/py_exploratory_comp_3_sol.ipynb +++ b/notebook3_for_and_if/py_exploratory_comp_3_sol.ipynb @@ -222,7 +222,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. First `for` loop\n", + "### Exercise 1. First `for` loop\n", "Create a list with the names of the months. Create a second list with the number of days in each month (for a regular year). Create a `for` loop that prints:\n", "\n", "`The number of days in MONTH is XX days`\n", @@ -385,7 +385,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Combination of `for` loop with `if` statement\n", + "### Exercise 2. Combination of `for` loop with `if` statement\n", "Consider the function\n", "\n", "$\\begin{split}\n", @@ -414,7 +414,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Load and loop through temperature data\n", + "### Exercise 3. Load and loop through temperature data\n", "Load the temperature data for Holland from the data file `holland_temperature.dat`. Loop through all monthly temperatures and print a message that includes the month number and states whether the monthly average temperature is above or below 10 degrees" ] }, @@ -489,7 +489,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. Running total\n", + "### Exercise 4. Running total\n", "For the data of the previous example, compute the running total and store it in an array using a loop. Hence, the result should be an array with the same length as `data` where item `i` is the sum of all values in the array `data` up to and including `data[i]`. Print both the array `data` and the array with the running total to the screen. Finally, check your answer by using the `cumsum` function of `numpy`, which should give the same answer as your loop." ] }, @@ -570,7 +570,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. Month closest to 15 degrees\n", + "### Exercise 5. Month closest to 15 degrees\n", "Find the month in which the average monthly temperature in Holland is closest to 15 degrees (use the data from Exercise 3). Apply the approach described above by looping through all values and use an `if` statement inside the loop. You may also want to use the `abs` function to compute the absolute value of a number. Check your answer by using a few `numpy` methods that don't require a loop and an `if` statement." ] }, @@ -725,7 +725,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6, Oil data\n", + "### Exercise 6, Oil data\n", "The file `oil_price.dat` contains the monthly oil price since 1985. The file contains three columns: year, month, price in Euros (from the european bank website). Make a plot of the oil price (put numbers along the horizontal axis; we will learn how to do dates in another notebook) and determine the month and year the oil price first rose above 40 euros, above 60 euros, and above 80 euros. You need to write to the screen something like `The oil price exceeds 40 euros for the first time in month xx of year yyyy` where `xx` and `yyyy` are the correct month and year. Use a double loop. Can you modify the code such that it doesn't print the number of the month but the name of the month?" ] }, @@ -910,7 +910,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 7. Find the position of your name in a list\n", + "### Exercise 7. Find the position of your name in a list\n", "Use a loop to figure out between which two names your name falls in the list\n", "`['Aaldrich', 'Babette', 'Chris', 'Franka', 'Joe', 'Louisa', 'Pierre', 'Simone', 'Tarek', 'Yvonne', 'Zu']` and print the result to the screen." ] @@ -940,7 +940,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1" + "Answer to Exercise 1" ] }, { @@ -982,7 +982,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answer to Exercise 2" + "Answer to Exercise 2" ] }, { @@ -1019,7 +1019,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answer to Exercise 3" + "Answer to Exercise 3" ] }, { @@ -1061,7 +1061,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answer to Exercise 4" + "Answer to Exercise 4" ] }, { @@ -1096,7 +1096,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answer to Exercise 5" + "Answer to Exercise 5" ] }, { @@ -1136,7 +1136,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answer to Exercise 6" + "Answer to Exercise 6" ] }, { @@ -1185,7 +1185,7 @@ "source": [ "Back to Exercise 6\n", "\n", - "Answer to Exercise 7" + "Answer to Exercise 7" ] }, { diff --git a/notebook4_functions/py_exploratory_comp_4.ipynb b/notebook4_functions/py_exploratory_comp_4.ipynb index cf41c33..e40d9dd 100644 --- a/notebook4_functions/py_exploratory_comp_4.ipynb +++ b/notebook4_functions/py_exploratory_comp_4.ipynb @@ -162,7 +162,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. First function\n", + "### Exercise 1. First function\n", "Write a Python function for the following function:\n", "\n", "$f(x)=e^{-\\alpha x}\\cos(x)$\n", @@ -307,7 +307,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2, Stream function for flow around a cylinder\n", + "### Exercise 2, Stream function for flow around a cylinder\n", "Consider two-dimensional inviscid fluid flow (potential flow) around a cylinder.\n", "The origin of the coordinate system is at the center of the cylinder.\n", "The stream function is a function that is constant along stream lines. \n", @@ -414,7 +414,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3, Contour plot for flow around a cylinder\n", + "### Exercise 3, Contour plot for flow around a cylinder\n", "Evaluate the function for the stream function around a cylinder with radius 1.5 on a grid of 100 by 100 points, where `x` varies from -4 to +4, and `y` varies from -3 to 3; use $U=1$. Evaluate the stream function on the entire grid (you need to create a vectorized version of the function you wrote to compute the stream function). Then use the `np.contour` function to create a contour plot (find out how by reading the help of the `contour` function or go to [this demo](http://matplotlib.org/examples/pylab_examples/contour_demo.html)) of the `matplotlib` gallery). You need to use the command `plt.axis('equal')`, so that the scales along the axes are equal and the circle looks like a circle rather than an ellipse. Finally, you may want to add a nice circular patch using the `fill` command and specifying a bunch of $x$ and $y$ values around the circumference of the cylinder." ] }, @@ -493,7 +493,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4, Streamplot of flow around a cylinder\n", + "### Exercise 4, Streamplot of flow around a cylinder\n", "The radial and tangential components of the velocity vector $\\vec{v}=(v_r,v_\\theta)$ for inviscid fluid flow around a cylinder are given by\n", "\n", "$\\begin{split}\n", @@ -531,7 +531,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5, Derivative of a function\n", + "### Exercise 5, Derivative of a function\n", "The function `func`, which we wrote earlier in this notebook, implements the following function\n", "\n", "$f(x)= \\cos(x) \\qquad x <0$\n", @@ -598,7 +598,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. Numerical integration" + "### Exercise 6. Numerical integration" ] }, { @@ -720,7 +720,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 7. First wiget" + "### Exercise 7. First wiget" ] }, { @@ -755,7 +755,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1" + "Answer to Exercise 1" ] }, { @@ -783,7 +783,7 @@ "source": [ " Back to Exercise 1\n", "\n", - "Answer to Exercise 2" + "Answer to Exercise 2" ] }, { @@ -811,7 +811,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answer to Exercise 3" + "Answer to Exercise 3" ] }, { @@ -836,7 +836,7 @@ "source": [ " Back to Exercise 3\n", "\n", - "Answer to Exercise 4" + "Answer to Exercise 4" ] }, { @@ -879,7 +879,7 @@ "source": [ " Back to Exercise 4\n", "\n", - "Answer to Exercise 5" + "Answer to Exercise 5" ] }, { @@ -913,7 +913,7 @@ "source": [ " Back to Exercise 5\n", "\n", - "Answer to Exercise 6" + "Answer to Exercise 6" ] }, { @@ -944,7 +944,7 @@ "source": [ " Back to Exercise 6\n", "\n", - "Answer to Exercise 7" + "Answer to Exercise 7" ] }, { diff --git a/notebook4_functions/py_exploratory_comp_4_sol.ipynb b/notebook4_functions/py_exploratory_comp_4_sol.ipynb index 6f828d6..ce464e5 100644 --- a/notebook4_functions/py_exploratory_comp_4_sol.ipynb +++ b/notebook4_functions/py_exploratory_comp_4_sol.ipynb @@ -186,7 +186,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. First function\n", + "### Exercise 1. First function\n", "Write a Python function for the following function:\n", "\n", "$f(x)=e^{-\\alpha x}\\cos(x)$\n", @@ -369,7 +369,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2, Stream function for flow around a cylinder\n", + "### Exercise 2, Stream function for flow around a cylinder\n", "Consider two-dimensional inviscid fluid flow (potential flow) around a cylinder.\n", "The origin of the coordinate system is at the center of the cylinder.\n", "The stream function is a function that is constant along stream lines. \n", @@ -502,7 +502,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3, Contour plot for flow around a cylinder\n", + "### Exercise 3, Contour plot for flow around a cylinder\n", "Evaluate the function for the stream function around a cylinder with radius 1.5 on a grid of 100 by 100 points, where `x` varies from -4 to +4, and `y` varies from -3 to 3; use $U=1$. Evaluate the stream function on the entire grid (you need to create a vectorized version of the function you wrote to compute the stream function). Then use the `np.contour` function to create a contour plot (find out how by reading the help of the `contour` function or go to [this demo](http://matplotlib.org/examples/pylab_examples/contour_demo.html)) of the `matplotlib` gallery). You need to use the command `plt.axis('equal')`, so that the scales along the axes are equal and the circle looks like a circle rather than an ellipse. Finally, you may want to add a nice circular patch using the `fill` command and specifying a bunch of $x$ and $y$ values around the circumference of the cylinder." ] }, @@ -608,7 +608,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4, Streamplot of flow around a cylinder\n", + "### Exercise 4, Streamplot of flow around a cylinder\n", "The radial and tangential components of the velocity vector $\\vec{v}=(v_r,v_\\theta)$ for inviscid fluid flow around a cylinder are given by\n", "\n", "$\\begin{split}\n", @@ -646,7 +646,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5, Derivative of a function\n", + "### Exercise 5, Derivative of a function\n", "The function `func`, which we wrote earlier in this notebook, implements the following function\n", "\n", "$f(x)= \\cos(x) \\qquad x <0$\n", @@ -722,7 +722,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. Numerical integration" + "### Exercise 6. Numerical integration" ] }, { @@ -896,7 +896,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 7. First wiget" + "### Exercise 7. First wiget" ] }, { @@ -931,7 +931,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1" + "Answer to Exercise 1" ] }, { @@ -970,7 +970,7 @@ "source": [ " Back to Exercise 1\n", "\n", - "Answer to Exercise 2" + "Answer to Exercise 2" ] }, { @@ -1007,7 +1007,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answer to Exercise 3" + "Answer to Exercise 3" ] }, { @@ -1053,7 +1053,7 @@ "source": [ " Back to Exercise 3\n", "\n", - "Answer to Exercise 4" + "Answer to Exercise 4" ] }, { @@ -1114,7 +1114,7 @@ "source": [ " Back to Exercise 4\n", "\n", - "Answer to Exercise 5" + "Answer to Exercise 5" ] }, { @@ -1159,7 +1159,7 @@ "source": [ " Back to Exercise 5\n", "\n", - "Answer to Exercise 6" + "Answer to Exercise 6" ] }, { @@ -1203,7 +1203,7 @@ "source": [ " Back to Exercise 6\n", "\n", - "Answer to Exercise 7" + "Answer to Exercise 7" ] }, { diff --git a/notebook5_rootfinding/py_exploratory_comp_5.ipynb b/notebook5_rootfinding/py_exploratory_comp_5.ipynb index 449666f..d9115d4 100644 --- a/notebook5_rootfinding/py_exploratory_comp_5.ipynb +++ b/notebook5_rootfinding/py_exploratory_comp_5.ipynb @@ -83,7 +83,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. \n", + "### Exercise 1. \n", "Write a Python function for $f(x)=\\frac{1}{2}-\\text{e}^{-x}$. Create a plot of $f(x)$ for $x$ varying from 0 to 4. Notice that $f(x)$ has a zero somewhere on the plotted interval (for this example it isn't really that hard to determine the zero exactly, of course, and we will do that later on to test whether our code works correctly)." ] }, @@ -110,7 +110,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Bisection method\n", + "### Exercise 2. Bisection method\n", "Implement the bisection method in a function called `bisection`. Your `bisection` method should take the following arguments:\n", "\n", "1. The function for which you want to find the zero.\n", @@ -213,7 +213,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Newton's method\n", + "### Exercise 3. Newton's method\n", "Implement Newton's method in a Python function called `newtonsmethod` and test your function by finding the zero of $f(x)=\\frac{1}{2}-\\text{e}^{-x}$, as we used in Exercise 1. Use $x_0=1$ as the starting point of your search. The `newtonsmethod` function should take the following arguments:\n", "\n", "1. The function for which you want to find the zero.\n", @@ -250,7 +250,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. \n", + "### Exercise 4. \n", "Demonstrate that your `newton` function works by finding the zero of $\\sin(x)$. As you know, the $\\sin(x)$ function has many zeros: $-2\\pi$, $-\\pi$, $0$, $pi$, $2\\pi$, etc. Which zero do you find when starting at $x=1$ and which zero do you find when starting at $x=1.5$?" ] }, @@ -285,7 +285,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. `fsolve` \n", + "### Exercise 5. `fsolve` \n", "Use the `fsolve` method of the `scipy.optimize` package to find the $x$ value for which $\\ln(x^2)=2$ (i.e., find the zero of the function $\\ln(x^2)-2$), and demonstrate that your value of $x$ indeed gives $\\ln(x^2)=2$." ] }, @@ -312,7 +312,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. Plot the zero\n", + "### Exercise 6. Plot the zero\n", "Plot the function $f(x)=x+2\\cos(x)$ for $x$ going from -2 to 4. On the same graph, plot a red dot at the location where $f(x)=0$. Obviously, you need to find this location with one of the methods you learned in this Notebook. " ] }, @@ -346,7 +346,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -375,7 +375,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -447,7 +447,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -517,7 +517,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -547,7 +547,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -575,7 +575,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/notebook5_rootfinding/py_exploratory_comp_5_sol.ipynb b/notebook5_rootfinding/py_exploratory_comp_5_sol.ipynb index e69e011..bba5fcd 100644 --- a/notebook5_rootfinding/py_exploratory_comp_5_sol.ipynb +++ b/notebook5_rootfinding/py_exploratory_comp_5_sol.ipynb @@ -54,7 +54,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. \n", + "### Exercise 1. \n", "Write a Python function for $f(x)=\\frac{1}{2}-\\text{e}^{-x}$. Create a plot of $f(x)$ for $x$ varying from 0 to 4. Notice that $f(x)$ has a zero somewhere on the plotted interval (for this example it isn't really that hard to determine the zero exactly, of course, and we will do that later on to test whether our code works correctly)." ] }, @@ -76,7 +76,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Bisection method\n", + "### Exercise 2. Bisection method\n", "Implement the bisection method in a function called `bisection`. Your `bisection` method should take the following arguments:\n", "\n", "1. The function for which you want to find the zero.\n", @@ -169,7 +169,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Newton's method\n", + "### Exercise 3. Newton's method\n", "Implement Newton's method in a Python function called `newtonsmethod` and test your function by finding the zero of $f(x)=\\frac{1}{2}-\\text{e}^{-x}$, as we used in Exercise 1. Use $x_0=1$ as the starting point of your search. The `newtonsmethod` function should take the following arguments:\n", "\n", "1. The function for which you want to find the zero.\n", @@ -201,7 +201,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. \n", + "### Exercise 4. \n", "Demonstrate that your `newton` function works by finding the zero of $\\sin(x)$. As you know, the $\\sin(x)$ function has many zeros: $-2\\pi$, $-\\pi$, $0$, $pi$, $2\\pi$, etc. Which zero do you find when starting at $x=1$ and which zero do you find when starting at $x=1.5$?" ] }, @@ -231,7 +231,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. `fsolve` \n", + "### Exercise 5. `fsolve` \n", "Use the `fsolve` method of the `scipy.optimize` package to find the $x$ value for which $\\ln(x^2)=2$ (i.e., find the zero of the function $\\ln(x^2)-2$), and demonstrate that your value of $x$ indeed gives $\\ln(x^2)=2$." ] }, @@ -253,7 +253,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. Plot the zero\n", + "### Exercise 6. Plot the zero\n", "Plot the function $f(x)=x+2\\cos(x)$ for $x$ going from -2 to 4. On the same graph, plot a red dot at the location where $f(x)=0$. Obviously, you need to find this location with one of the methods you learned in this Notebook. " ] }, @@ -282,7 +282,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -327,7 +327,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -401,7 +401,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -469,7 +469,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -508,7 +508,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -539,7 +539,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/notebook6_linear_systems/py_exploratory_comp_6.ipynb b/notebook6_linear_systems/py_exploratory_comp_6.ipynb index 14923c1..f53d8a7 100644 --- a/notebook6_linear_systems/py_exploratory_comp_6.ipynb +++ b/notebook6_linear_systems/py_exploratory_comp_6.ipynb @@ -160,7 +160,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Fitting a wave\n", + "### Exercise 1. Fitting a wave\n", "Consider the following four measurements of the quantity $y$ at time $t$: $(t_0,y_0)=(0,3)$, $(t_1,y_1)=(0.25,1)$, $(t_2,y_2)=(0.5,-3)$, $(t_3,y_3)=(0.75,1)$. The measurements are part of a wave that may be written as\n", "\n", "$y = a\\cos(\\pi t) + b\\cos(2\\pi t) + c\\cos(3\\pi t) + d\\cos(4\\pi t)$\n", @@ -203,7 +203,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. The `fpoly` function\n", + "### Exercise 2. The `fpoly` function\n", "First, write a function called `fpoly` that returns an array where item $n$ is equal to $x^n$. The input arguments of the function are the value of $x$ and the degree of the polynomial $N$. The output of the function is an array of length $N+1$. Test your function for $x=2$ and $N=4$ by executing\n", "\n", "`print fpoly(2, 4)`\n", @@ -234,7 +234,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. The `solvepoly` function\n", + "### Exercise 3. The `solvepoly` function\n", "Next, write a function that computes the parameters $a_n$ to fit a polynomial of degree $N$ through $N+1$ data points. Call the function `solvepoly`. The input arguments of the function are an array $x$ of length $N+1$ and an array $y$ of length $N+1$. The output is an array of parameters $a_n$ such that a polynomial of degree $N$ goes exactly through the $N+1$ data points $x$ and $y$. Inside the function, you need to compute a matrix of $N+1$ equations for the $N+1$ unknown parameters. For each of the rows of the matrix, call the function `fpoly`. Test your function by executing the following four commands. If your code is correct, the parameter array `a` is [ 3. 2.33333333 -6. 1.66666667].\n", "\n", " xp = np.array([0, 1, 2, 3])\n", @@ -266,7 +266,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. The `fpolyeval` function\n", + "### Exercise 4. The `fpolyeval` function\n", "Finally, write a function called `fpolyeval` to evaluate the function $f(x) = \\sum\\limits_{n=0}^{N}a_nx^n$ for given parameters $a$ and an array of $x$ values. The `fpolyeval` function takes as input argument an array of arbitrary length $x$ and an array of parameters $a$ of length $N+1$. The function returns an array of $f(x)$ values with a length equal to the length of $x$. First test your function by executing\n", "\n", "`fpolyeval(xp, a)`\n", @@ -404,7 +404,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. The head between two canals\n", + "### Exercise 5. The head between two canals\n", "Compute the head distribution in a sand layer bounded on each side by a canal. Given: $k=10$ m/day, $D=10$ m, $h_1^*=20$ m, $h_2^*=22$ m, $L=1000$ m, $P=0.001$ m/d. Use $N=40$. Write Python code to:\n", "\n", "* Solve for the heads in all $N+1$ points\n", @@ -550,7 +550,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. One-dimensional consolidation\n", + "### Exercise 6. One-dimensional consolidation\n", "Consider the consolidation process of a clay layer that is 2 m thick. A uniform load is applied at time $t=0$ causing a unit increase in the pressure, i.e., $p(t=0)=1$. The consolidation coefficient of the clay is $c_v=1\\cdot 10^{-6}$ m$^2$/s. Simulate the consolidation process using the numerical method described above. Use $N=40$ and a time step $\\Delta t=4\\cdot 10^4$ sec. Take 50 time steps and plot the pressure distribution in the clay every 10 time steps. Add labels along the axes and a legend. Compare your solution to the graph in Fig. 16.2 of Veruit (2012) (Note that Verruijt plots $p$ along the horizontal axis). The last line of your plot should approximate the line $c_v t / h^2=0.5$ in the graph of Verrujt. \n", "\n", "Make sure that your Python code:\n", @@ -610,7 +610,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 7. One-dimensional consolidation revisited\n", + "### Exercise 7. One-dimensional consolidation revisited\n", "Modify your solution to Exercise 6 by computing and storing the inverse of the matrix, and compute a solution through multiplication of the inverse of the matrix with the right-hand-side vector" ] }, @@ -709,7 +709,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 8 Sparse matrix\n", + "### Exercise 8 Sparse matrix\n", "Redo Exercise 5 but now use a *sparse* matrix. Obtain a solution for the head using $h_1^*=42$, $h_2^*=40$, and $N=10000$. All other parameters are the same as for Exercise 2. Create a plot of the head between the two canals. **Warning**: When you try to solve the problem with a regular (full) matrix and $N=10000$, you may run out of computer memory or the solve may take a *very* long time, and/or your program may hang. Don't try this, but if you accidentally do this and your Notebook doesn't recover, try to click on 'Kernel' in the menu bar at the top and then on 'Restart'." ] }, @@ -743,7 +743,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -782,7 +782,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -811,7 +811,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -845,7 +845,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -892,7 +892,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -985,7 +985,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { @@ -1038,7 +1038,7 @@ "source": [ "Back to Exercise 7\n", "\n", - "Answers to Exercise 8" + "Answers to Exercise 8" ] }, { diff --git a/notebook6_linear_systems/py_exploratory_comp_6_sol.ipynb b/notebook6_linear_systems/py_exploratory_comp_6_sol.ipynb index 39d7d67..0a7713f 100644 --- a/notebook6_linear_systems/py_exploratory_comp_6_sol.ipynb +++ b/notebook6_linear_systems/py_exploratory_comp_6_sol.ipynb @@ -173,7 +173,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Fitting a wave\n", + "### Exercise 1. Fitting a wave\n", "Consider the following four measurements of the quantity $y$ at time $t$: $(t_0,y_0)=(0,3)$, $(t_1,y_1)=(0.25,1)$, $(t_2,y_2)=(0.5,-3)$, $(t_3,y_3)=(0.75,1)$. The measurements are part of a wave that may be written as\n", "\n", "$y = a\\cos(\\pi t) + b\\cos(2\\pi t) + c\\cos(3\\pi t) + d\\cos(4\\pi t)$\n", @@ -211,7 +211,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. The `fpoly` function\n", + "### Exercise 2. The `fpoly` function\n", "First, write a function called `fpoly` that returns an array where item $n$ is equal to $x^n$. The input arguments of the function are the value of $x$ and the degree of the polynomial $N$. The output of the function is an array of length $N+1$. Test your function for $x=2$ and $N=4$ by executing\n", "\n", "`print fpoly(2, 4)`\n", @@ -237,7 +237,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. The `solvepoly` function\n", + "### Exercise 3. The `solvepoly` function\n", "Next, write a function that computes the parameters $a_n$ to fit a polynomial of degree $N$ through $N+1$ data points. Call the function `solvepoly`. The input arguments of the function are an array $x$ of length $N+1$ and an array $y$ of length $N+1$. The output is an array of parameters $a_n$ such that a polynomial of degree $N$ goes exactly through the $N+1$ data points $x$ and $y$. Inside the function, you need to compute a matrix of $N+1$ equations for the $N+1$ unknown parameters. For each of the rows of the matrix, call the function `fpoly`. Test your function by executing the following four commands. If your code is correct, the parameter array `a` is [ 3. 2.33333333 -6. 1.66666667].\n", "\n", " xp = np.array([0, 1, 2, 3])\n", @@ -264,7 +264,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. The `fpolyeval` function\n", + "### Exercise 4. The `fpolyeval` function\n", "Finally, write a function called `fpolyeval` to evaluate the function $f(x) = \\sum\\limits_{n=0}^{N}a_nx^n$ for given parameters $a$ and an array of $x$ values. The `fpolyeval` function takes as input argument an array of arbitrary length $x$ and an array of parameters $a$ of length $N+1$. The function returns an array of $f(x)$ values with a length equal to the length of $x$. First test your function by executing\n", "\n", "`fpolyeval(xp, a)`\n", @@ -404,7 +404,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. The head between two canals\n", + "### Exercise 5. The head between two canals\n", "Compute the head distribution in a sand layer bounded on each side by a canal. Given: $k=10$ m/day, $D=10$ m, $h_1^*=20$ m, $h_2^*=22$ m, $L=1000$ m, $P=0.001$ m/d. Use $N=40$. Write Python code to:\n", "\n", "* Solve for the heads in all $N+1$ points\n", @@ -545,7 +545,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. One-dimensional consolidation\n", + "### Exercise 6. One-dimensional consolidation\n", "Consider the consolidation process of a clay layer that is 2 m thick. A uniform load is applied at time $t=0$ causing a unit increase in the pressure, i.e., $p(t=0)=1$. The consolidation coefficient of the clay is $c_v=1\\cdot 10^{-6}$ m$^2$/s. Simulate the consolidation process using the numerical method described above. Use $N=40$ and a time step $\\Delta t=4\\cdot 10^4$ sec. Take 50 time steps and plot the pressure distribution in the clay every 10 time steps. Add labels along the axes and a legend. Compare your solution to the graph in Fig. 16.2 of Veruit (2012) (Note that Verruijt plots $p$ along the horizontal axis). The last line of your plot should approximate the line $c_v t / h^2=0.5$ in the graph of Verrujt. \n", "\n", "Make sure that your Python code:\n", @@ -603,7 +603,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 7. One-dimensional consolidation revisited\n", + "### Exercise 7. One-dimensional consolidation revisited\n", "Modify your solution to Exercise 6 by computing and storing the inverse of the matrix, and compute a solution through multiplication of the inverse of the matrix with the right-hand-side vector" ] }, @@ -723,7 +723,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 8 Sparse matrix\n", + "### Exercise 8 Sparse matrix\n", "Redo Exercise 5 but now use a *sparse* matrix. Obtain a solution for the head using $h_1^*=42$, $h_2^*=40$, and $N=10000$. All other parameters are the same as for Exercise 2. Create a plot of the head between the two canals. **Warning**: When you try to solve the problem with a regular (full) matrix and $N=10000$, you may run out of computer memory or the solve may take a *very* long time, and/or your program may hang. Don't try this, but if you accidentally do this and your Notebook doesn't recover, try to click on 'Kernel' in the menu bar at the top and then on 'Restart'." ] }, @@ -752,7 +752,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -804,7 +804,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -836,7 +836,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -873,7 +873,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -929,7 +929,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -992,7 +992,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { @@ -1061,7 +1061,7 @@ "source": [ "Back to Exercise 6\n", "\n", - "Answers to Exercise 7" + "Answers to Exercise 7" ] }, { @@ -1131,7 +1131,7 @@ "source": [ "Back to Exercise 7\n", "\n", - "Answers to Exercise 8" + "Answers to Exercise 8" ] }, { diff --git a/notebook7_bugs/py_exploratory_comp_7.ipynb b/notebook7_bugs/py_exploratory_comp_7.ipynb index 0c4d1d4..a13afd8 100644 --- a/notebook7_bugs/py_exploratory_comp_7.ipynb +++ b/notebook7_bugs/py_exploratory_comp_7.ipynb @@ -224,7 +224,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1 " + "### Exercise 1 " ] }, { @@ -258,7 +258,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2 " + "### Exercise 2 " ] }, { @@ -293,7 +293,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3 " + "### Exercise 3 " ] }, { @@ -330,7 +330,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4 " + "### Exercise 4 " ] }, { @@ -365,7 +365,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5 " + "### Exercise 5 " ] }, { @@ -396,7 +396,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1" + "Answer to Exercise 1" ] }, { @@ -425,7 +425,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answer to Exercise 2" + "Answer to Exercise 2" ] }, { @@ -455,7 +455,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answer to Exercise 3" + "Answer to Exercise 3" ] }, { @@ -487,7 +487,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answer to Exercise 4" + "Answer to Exercise 4" ] }, { @@ -517,7 +517,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answer to Exercise 5" + "Answer to Exercise 5" ] }, { diff --git a/notebook7_bugs/py_exploratory_comp_7_sol.ipynb b/notebook7_bugs/py_exploratory_comp_7_sol.ipynb index 96c5e16..e1386c1 100644 --- a/notebook7_bugs/py_exploratory_comp_7_sol.ipynb +++ b/notebook7_bugs/py_exploratory_comp_7_sol.ipynb @@ -300,7 +300,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1 " + "### Exercise 1 " ] }, { @@ -341,7 +341,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2 " + "### Exercise 2 " ] }, { @@ -380,7 +380,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3 " + "### Exercise 3 " ] }, { @@ -421,7 +421,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4 " + "### Exercise 4 " ] }, { @@ -460,7 +460,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5 " + "### Exercise 5 " ] }, { @@ -502,7 +502,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answer to Exercise 1" + "Answer to Exercise 1" ] }, { @@ -537,7 +537,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answer to Exercise 2" + "Answer to Exercise 2" ] }, { @@ -573,7 +573,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answer to Exercise 3" + "Answer to Exercise 3" ] }, { @@ -611,7 +611,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answer to Exercise 4" + "Answer to Exercise 4" ] }, { @@ -644,7 +644,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answer to Exercise 5" + "Answer to Exercise 5" ] }, { diff --git a/notebook8_pandas/py_exploratory_comp_8.ipynb b/notebook8_pandas/py_exploratory_comp_8.ipynb index c1adafe..a97405c 100644 --- a/notebook8_pandas/py_exploratory_comp_8.ipynb +++ b/notebook8_pandas/py_exploratory_comp_8.ipynb @@ -264,7 +264,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Average annual rainfall by country\n", + "### Exercise 1. Average annual rainfall by country\n", "The file `annual_precip.csv` contains the average yearly rainfall and total land area for all the countries in the world (well, there are some missing values); the data is available on the website of the world bank. Open the data file to see what it looks like (just click on it in the Files tab on the Jupyter Dashboard). Load the data with the `read_csv` function of `pandas`, making sure that the names of the countries can be used to select a row, and perform the following tasks:\n", "\n", "* Print the first 5 lines of the `DataFrame` to the screen with the `.head()` function.\n", @@ -418,7 +418,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Average annual rainfall by country continued\n", + "### Exercise 2. Average annual rainfall by country continued\n", "Continue with the average yearly rainfall and total land area for all the countries in the world and perform the following tasks:\n", "\n", "* Add a new column that stores the total average annual freshwater influx in km$^3$/year for each country. Make sure you convert your units correctly. \n", @@ -535,7 +535,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3, Load and plot daily rainfall\n", + "### Exercise 3, Load and plot daily rainfall\n", "Rainfall data for the Netherlands may be obtained from the website of the Royal Dutch Meteorological Society KNMI . Daily rainfall for the weather station Rotterdam in 2012 is stored in the file `rotterdam_rainfall_2012.txt`. First open the file in a text editor to see what the file looks like. At the top of the file, an explanation is given of the data in the file. Read this. Load the data file with the `read_csv` function of `pandas`. Use the keyword `skiprows` to skip all rows except for the row with the names of the columns. Use the keyword `parse_dates` to give either the name or number of the column that needs to be converted to a datetime. Don't forget the `skipinitialspace` keyword, else the names of the columns may start with a bunch of spaces. Perform the following tasks:\n", "* Convert the rainfall data to mm/d.\n", "* Some rainfall values in the dataset may be -1 (read the header of the file to learn why); set all rainfall values that are less than zero to zero. \n", @@ -562,7 +562,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4, Compute monthly rainfall from daily rainfall\n", + "### Exercise 4, Compute monthly rainfall from daily rainfall\n", "In this exercise we are going to compute the total monthly rainfall for 2012 in the City of Rotterdam using the daily rainfall measurements we loaded in the previous Exercise. Later on in this Notebook we learn convenient functions from `pandas` to do this, but here we are going to do this with a loop. Create an array of 12 zeros to store the monthly totals and loop through all the days in 2012 to compute the total rainfall for each month. The month associated with each row of the DataFrame may be obtained with the `.month` syntax, as shown above. Print the monthly totals (in mm/month) to the screen and create a bar graph of the total monthly rainfall (in mm/month) vs. the month using the `plt.bar` function of matplotlib. " ] }, @@ -634,7 +634,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5, Resample weather data\n", + "### Exercise 5, Resample weather data\n", "The file `rotterdam_weather_2000_2010.txt` contains daily weather data at the weather station Rotterdam for the period 2000-2010 (again from the KNMI). Open the data file in an editor to see what is in it. Perform the following tasks:\n", "* Load the data making sure the dates are used as index. \n", "* Convert the rain and evaporation to mm/day, and the temperature to degrees Celcius. \n", @@ -668,7 +668,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -707,7 +707,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -731,7 +731,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -760,7 +760,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -787,7 +787,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { diff --git a/notebook8_pandas/py_exploratory_comp_8_sol.ipynb b/notebook8_pandas/py_exploratory_comp_8_sol.ipynb index f6e43ff..c32c890 100644 --- a/notebook8_pandas/py_exploratory_comp_8_sol.ipynb +++ b/notebook8_pandas/py_exploratory_comp_8_sol.ipynb @@ -472,7 +472,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Average annual rainfall by country\n", + "### Exercise 1. Average annual rainfall by country\n", "The file `annual_precip.csv` contains the average yearly rainfall and total land area for all the countries in the world (well, there are some missing values); the data is available on the website of the world bank. Open the data file to see what it looks like (just click on it in the Files tab on the Jupyter Dashboard). Load the data with the `read_csv` function of `pandas`, making sure that the names of the countries can be used to select a row, and perform the following tasks:\n", "\n", "* Print the first 5 lines of the `DataFrame` to the screen with the `.head()` function.\n", @@ -998,7 +998,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Average annual rainfall by country continued\n", + "### Exercise 2. Average annual rainfall by country continued\n", "Continue with the average yearly rainfall and total land area for all the countries in the world and perform the following tasks:\n", "\n", "* Add a new column that stores the total average annual freshwater influx in km$^3$/year for each country. Make sure you convert your units correctly. \n", @@ -1359,7 +1359,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3, Load and plot daily rainfall\n", + "### Exercise 3, Load and plot daily rainfall\n", "Rainfall data for the Netherlands may be obtained from the website of the Royal Dutch Meteorological Society KNMI . Daily rainfall for the weather station Rotterdam in 2012 is stored in the file `rotterdam_rainfall_2012.txt`. First open the file in a text editor to see what the file looks like. At the top of the file, an explanation is given of the data in the file. Read this. Load the data file with the `read_csv` function of `pandas`. Use the keyword `skiprows` to skip all rows except for the row with the names of the columns. Use the keyword `parse_dates` to give either the name or number of the column that needs to be converted to a datetime. Don't forget the `skipinitialspace` keyword, else the names of the columns may start with a bunch of spaces. Perform the following tasks:\n", "* Convert the rainfall data to mm/d.\n", "* Some rainfall values in the dataset may be -1 (read the header of the file to learn why); set all rainfall values that are less than zero to zero. \n", @@ -1386,7 +1386,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4, Compute monthly rainfall from daily rainfall\n", + "### Exercise 4, Compute monthly rainfall from daily rainfall\n", "In this exercise we are going to compute the total monthly rainfall for 2012 in the City of Rotterdam using the daily rainfall measurements we loaded in the previous Exercise. Later on in this Notebook we learn convenient functions from `pandas` to do this, but here we are going to do this with a loop. Create an array of 12 zeros to store the monthly totals and loop through all the days in 2012 to compute the total rainfall for each month. The month associated with each row of the DataFrame may be obtained with the `.month` syntax, as shown above. Print the monthly totals (in mm/month) to the screen and create a bar graph of the total monthly rainfall (in mm/month) vs. the month using the `plt.bar` function of matplotlib. " ] }, @@ -1568,7 +1568,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5, Resample weather data\n", + "### Exercise 5, Resample weather data\n", "The file `rotterdam_weather_2000_2010.txt` contains daily weather data at the weather station Rotterdam for the period 2000-2010 (again from the KNMI). Open the data file in an editor to see what is in it. Perform the following tasks:\n", "* Load the data making sure the dates are used as index. \n", "* Convert the rain and evaporation to mm/day, and the temperature to degrees Celcius. \n", @@ -1638,7 +1638,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -2107,7 +2107,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -2229,7 +2229,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -2277,7 +2277,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -2322,7 +2322,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { diff --git a/notebook9_discrete_random_variables/py_exploratory_comp_9.ipynb b/notebook9_discrete_random_variables/py_exploratory_comp_9.ipynb index c7c8700..a1f3b4f 100644 --- a/notebook9_discrete_random_variables/py_exploratory_comp_9.ipynb +++ b/notebook9_discrete_random_variables/py_exploratory_comp_9.ipynb @@ -175,7 +175,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Throwing a dice\n", + "### Exercise 1. Throwing a dice\n", "Throw a dice 100 times and report how many times you throw 1, 2, 3, 4, 5, and 6. Use a seed of 33. Make sure that the reported values add up to 100. Make sure you use a loop in your code as we did in the previous code cell." ] }, @@ -298,7 +298,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Flip a coin five times\n", + "### Exercise 2. Flip a coin five times\n", "Flip a coin five times in a row and record how many times you obtain tails (varying from 0-5). Perform the experiment 1000 times. Make a bar graph with the total number of tails on the horizontal axis and the experimentally computed probability to get that many tails, on the vertical axis. Execute your code several times (hit [shift]-[enter]) and see that the graph changes a bit every time, as the sequence of random numbers changes every time. " ] }, @@ -347,7 +347,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Flip a coin 5 times revisited\n", + "### Exercise 3. Flip a coin 5 times revisited\n", "Go back to the experiment where we flip a coin five times in a row and record how many times we obtain tails.\n", "Compute the theoretical probability for 0, 1, 2, 3, 4, and 5 tails and compare your answer to the probability computed from 1000 trials, 10000 trials, and 100000 trials (use a loop for these three sets of trials). Do you approach the theoretical value with more trials?" ] @@ -370,7 +370,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. Maximum value of two dice throws\n", + "### Exercise 4. Maximum value of two dice throws\n", "Throw a dice two times and record the maximum value of the two throws. Use the `np.max` function to compute the maximum value. Like the `np.sum` function, the `np.max` function takes an array as input argument and an optional keyword argument named `axis`. Perform the experiment 1000 times and compute the probability that the highest value is 1, 2, 3, 4, 5, or 6. Make a graph of the cumulative probability distribution function using a bar graph." ] }, @@ -392,7 +392,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. Maximum value of two dice throws revisited\n", + "### Exercise 5. Maximum value of two dice throws revisited\n", "Refer back to Exercise 4.\n", "Compute the theoretical value of the probability of the highest dice when throwing the dice twice (the throws are labeled T1 and T2, respectively). There are 36 possible outcomes for this experiment. Let $M$ denote the random variable corresponding to this experiment (this means for instance that $M=3$ when your first throw is a 2, and the second throw is a 3). All outcomes of $M$ can easily be written down, as shown in the following Table: \n", "\n", @@ -494,7 +494,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. Election poll\n", + "### Exercise 6. Election poll\n", "Consider an election where one million people will vote. 490,000 people will vote for candidate $A$ and 510,000 people will vote for candidate $B$. One day before the election, the company of 'Maurice the Dog' conducts a poll among 1000 randomly chosen voters. Compute whether the Dog will predict the winner correctly using the approach explained above and a seed of 2." ] }, @@ -551,7 +551,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -575,7 +575,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -613,7 +613,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -642,7 +642,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -667,7 +667,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -696,7 +696,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/notebook9_discrete_random_variables/py_exploratory_comp_9_sol.ipynb b/notebook9_discrete_random_variables/py_exploratory_comp_9_sol.ipynb index f98b983..49ac20b 100644 --- a/notebook9_discrete_random_variables/py_exploratory_comp_9_sol.ipynb +++ b/notebook9_discrete_random_variables/py_exploratory_comp_9_sol.ipynb @@ -246,7 +246,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Throwing a dice\n", + "### Exercise 1. Throwing a dice\n", "Throw a dice 100 times and report how many times you throw 1, 2, 3, 4, 5, and 6. Use a seed of 33. Make sure that the reported values add up to 100. Make sure you use a loop in your code as we did in the previous code cell." ] }, @@ -396,7 +396,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 2. Flip a coin five times\n", + "### Exercise 2. Flip a coin five times\n", "Flip a coin five times in a row and record how many times you obtain tails (varying from 0-5). Perform the experiment 1000 times. Make a bar graph with the total number of tails on the horizontal axis and the experimentally computed probability to get that many tails, on the vertical axis. Execute your code several times (hit [shift]-[enter]) and see that the graph changes a bit every time, as the sequence of random numbers changes every time. " ] }, @@ -445,7 +445,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 3. Flip a coin 5 times revisited\n", + "### Exercise 3. Flip a coin 5 times revisited\n", "Go back to the experiment where we flip a coin five times in a row and record how many times we obtain tails.\n", "Compute the theoretical probability for 0, 1, 2, 3, 4, and 5 tails and compare your answer to the probability computed from 1000 trials, 10000 trials, and 100000 trials (use a loop for these three sets of trials). Do you approach the theoretical value with more trials?" ] @@ -468,7 +468,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 4. Maximum value of two dice throws\n", + "### Exercise 4. Maximum value of two dice throws\n", "Throw a dice two times and record the maximum value of the two throws. Use the `np.max` function to compute the maximum value. Like the `np.sum` function, the `np.max` function takes an array as input argument and an optional keyword argument named `axis`. Perform the experiment 1000 times and compute the probability that the highest value is 1, 2, 3, 4, 5, or 6. Make a graph of the cumulative probability distribution function using a bar graph." ] }, @@ -490,7 +490,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 5. Maximum value of two dice throws revisited\n", + "### Exercise 5. Maximum value of two dice throws revisited\n", "Refer back to Exercise 4.\n", "Compute the theoretical value of the probability of the highest dice when throwing the dice twice (the throws are labeled T1 and T2, respectively). There are 36 possible outcomes for this experiment. Let $M$ denote the random variable corresponding to this experiment (this means for instance that $M=3$ when your first throw is a 2, and the second throw is a 3). All outcomes of $M$ can easily be written down, as shown in the following Table: \n", "\n", @@ -629,7 +629,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 6. Election poll\n", + "### Exercise 6. Election poll\n", "Consider an election where one million people will vote. 490,000 people will vote for candidate $A$ and 510,000 people will vote for candidate $B$. One day before the election, the company of 'Maurice the Dog' conducts a poll among 1000 randomly chosen voters. Compute whether the Dog will predict the winner correctly using the approach explained above and a seed of 2." ] }, @@ -686,7 +686,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -724,7 +724,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -770,7 +770,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -811,7 +811,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -836,7 +836,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -876,7 +876,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/old_notebooks/notebook_adv4/py_exp_comp_adv4.ipynb b/old_notebooks/notebook_adv4/py_exp_comp_adv4.ipynb index 0684f5e..22923eb 100644 --- a/old_notebooks/notebook_adv4/py_exp_comp_adv4.ipynb +++ b/old_notebooks/notebook_adv4/py_exp_comp_adv4.ipynb @@ -122,7 +122,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Execise 1. Radio buttons to set color\n", + "###Execise 1. Radio buttons to set color\n", "Add a second set of radio buttons to change the color of the circle shown in the previous example. You may want to relocate the radio buttons that set the radius of the circle to a more convenient place on the graph. Provide at least three options for setting the color of the circle. Note that a `Circle` object has a `set_color` function." ] }, @@ -254,7 +254,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 2. Deflection of a beam\n", + "###Exercise 2. Deflection of a beam\n", "Consider the elastic deflection of a beam with a uniform load $q$. The beam is supported by two simple supports (see figure below). The formula for the shape $y(x)$ of the deformed beam is\n", "\n", "$y = -\\frac{qx}{24EI} (L^3 - 2Lx^2 + x^3)$\n", @@ -332,7 +332,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 3. Sliders\n", + "###Exercise 3. Sliders\n", "*Part 1* Add a small blue circle with radius 0.2 at the intersection of the line and circle (see figure below but without the second slider). Initially, when $\\alpha=0$, the center of the small circle is at $(x,y)=(R,0)$. Make sure that the small circle rotates with the line when the angle is changed with the slider. " ] }, @@ -373,7 +373,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -421,7 +421,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -482,7 +482,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { diff --git a/old_notebooks/notebook_adv4/py_exp_comp_adv4_sol.ipynb b/old_notebooks/notebook_adv4/py_exp_comp_adv4_sol.ipynb index 92c4b41..470a87c 100644 --- a/old_notebooks/notebook_adv4/py_exp_comp_adv4_sol.ipynb +++ b/old_notebooks/notebook_adv4/py_exp_comp_adv4_sol.ipynb @@ -133,7 +133,7 @@ "id": "d2c8a4ef", "metadata": {}, "source": [ - "###Execise 1. Radio buttons to set color\n", + "###Execise 1. Radio buttons to set color\n", "Add a second set of radio buttons to change the color of the circle shown in the previous example. You may want to relocate the radio buttons that set the radius of the circle to a more convenient place on the graph. Provide at least three options for setting the color of the circle. Note that a `Circle` object has a `set_color` function." ] }, @@ -280,7 +280,7 @@ "id": "92988662", "metadata": {}, "source": [ - "###Exercise 2. Deflection of a beam\n", + "###Exercise 2. Deflection of a beam\n", "Consider the elastic deflection of a beam with a uniform load $q$. The beam is supported by two simple supports (see figure below). The formula for the shape $y(x)$ of the deformed beam is\n", "\n", "$y = -\\frac{qx}{24EI} (L^3 - 2Lx^2 + x^3)$\n", @@ -368,7 +368,7 @@ "id": "6caa767c", "metadata": {}, "source": [ - "###Exercise 3. Sliders\n", + "###Exercise 3. Sliders\n", "*Part 1* Add a small blue circle with radius 0.2 at the intersection of the line and circle (see figure below but without the second slider). Initially, when $\\alpha=0$, the center of the small circle is at $(x,y)=(R,0)$. Make sure that the small circle rotates with the line when the angle is changed with the slider. " ] }, @@ -417,7 +417,7 @@ "id": "ad0020c0", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -469,7 +469,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -534,7 +534,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { diff --git a/old_notebooks/notebook_pandas/py_exploratory_comp_5.ipynb b/old_notebooks/notebook_pandas/py_exploratory_comp_5.ipynb index e5a8fcf..8b44db5 100644 --- a/old_notebooks/notebook_pandas/py_exploratory_comp_5.ipynb +++ b/old_notebooks/notebook_pandas/py_exploratory_comp_5.ipynb @@ -209,7 +209,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 1. Average annual rainfall by country\n", + "###Exercise 1. Average annual rainfall by country\n", "The file `annual_precip.csv` contains the average yearly rainfall and total land area for all the countries in the world (well, there are some missing values); the data is available on the website of the world bank. Open the data file to see what it looks like (Notepad, Textedit, even Word if you have to). Load the data with the `read_csv` function of `pandas`, making sure that the names of the countries can be used to select a row, and perform the following tasks:\n", "\n", "* Print the first 5 lines of the DataFrame to the screen with the `.head()` function.\n", @@ -391,7 +391,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 2. Average annual rainfall by country continued\n", + "###Exercise 2. Average annual rainfall by country continued\n", "Continue with the average yearly rainfall and total land area for all the countries in the world and perform the following tasks:\n", "\n", "* Add a new column that stores the total average annual freshwater influx in km$^3$/year for each country. Make sure you convert your units correctly. \n", @@ -500,7 +500,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 3, Load and plot daily rainfall\n", + "###Exercise 3, Load and plot daily rainfall\n", "Rainfall data for the Netherlands may be obtained from the website of the Royal Dutch Meteorological Society KNMI . Daily rainfall for the weather station Rotterdam in 2012 is stored in the file `rotterdam_rainfall_2012.txt`. First open the file in a text editor to see what the file looks like. At the top of the file, an explanation is given of the data in the file. Read this. Load the data file with the `read_csv` function of `pandas`. Use the keyword `skiprows` to skip all rows except for the row with the names of the columns. Use the keyword `parse_dates` to give either the name or number of the column that needs to be converted to a datetime. Don't forget the `skipinitialspace` keyword, else the names of the columns may start with a bunch of spaces. Perform the following tasks:\n", "* Some rainfall values in the dataset may be -1 (read the header of the file to learn why); set all rainfall values that are less than zero to zero. \n", "* Use the `plot` function of `pandas` to create a line plot of the daily rainfall with the number of the day (so not the date) along the horizontal axis. \n", @@ -532,7 +532,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 4, Compute monthly rainfall from daily rainfall\n", + "###Exercise 4, Compute monthly rainfall from daily rainfall\n", "In this exercise we are going to compute the total monthly rainfall for 2012 in the City of Rotterdam using the daily rainfall measurements we loaded in the previous Exercise; later on in this Notebook we learn convenient functions from `pandas` to do this, but here we are going to do this with a loop. Create an array of 12 zeros to store the monthly totals and loop through all the days in 2012 to compute the total rainfall for each month. The month associated with each row of the DataFrame may be obtained with the `.month` syntax, as shown above. Print the monthly totals (in mm/month) to the screen and create a bar graph of the total monthly rainfall (in mm/month) vs. the month using the `plt.bar` function of matplotlib. " ] }, @@ -611,7 +611,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 5, Resample weather data\n", + "###Exercise 5, Resample weather data\n", "The file `rotterdam_weather_2000_2010.txt` contains daily weather data at the weather station Rotterdam for the period 2000-2010 (again from the KNMI). Open the data file in an editor to see what is in it. Perform the following tasks:\n", "* Load the data making sure the dates are used as index. \n", "* Convert the rain and evaporation to mm/day, and the temperature to degrees Celcius. \n", diff --git a/old_notebooks/notebook_pandas/py_exploratory_comp_5_sol.ipynb b/old_notebooks/notebook_pandas/py_exploratory_comp_5_sol.ipynb index 5cb0186..ed6609a 100644 --- a/old_notebooks/notebook_pandas/py_exploratory_comp_5_sol.ipynb +++ b/old_notebooks/notebook_pandas/py_exploratory_comp_5_sol.ipynb @@ -376,7 +376,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercise 1. Average annual rainfall by country\n", + "### Exercise 1. Average annual rainfall by country\n", "The file `annual_precip.csv` contains the average yearly rainfall and total land area for all the countries in the world (well, there are some missing values); the data is available on the website of the world bank. Open the data file to see what it looks like (Notepad, Textedit, even Word if you have to). Load the data with the `read_csv` function of `pandas`, making sure that the names of the countries can be used to select a row, and perform the following tasks:\n", "\n", "* Print the first 5 lines of the DataFrame to the screen with the `.head()` function.\n", @@ -846,7 +846,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 2. Average annual rainfall by country continued\n", + "###Exercise 2. Average annual rainfall by country continued\n", "Continue with the average yearly rainfall and total land area for all the countries in the world and perform the following tasks:\n", "\n", "* Add a new column that stores the total average annual freshwater influx in km$^3$/year for each country. Make sure you convert your units correctly. \n", @@ -1032,7 +1032,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 3, Load and plot daily rainfall\n", + "###Exercise 3, Load and plot daily rainfall\n", "Rainfall data for the Netherlands may be obtained from the website of the Royal Dutch Meteorological Society KNMI . Daily rainfall for the weather station Rotterdam in 2012 is stored in the file `rotterdam_rainfall_2012.txt`. First open the file in a text editor to see what the file looks like. At the top of the file, an explanation is given of the data in the file. Read this. Load the data file with the `read_csv` function of `pandas`. Use the keyword `skiprows` to skip all rows except for the row with the names of the columns. Use the keyword `parse_dates` to give either the name or number of the column that needs to be converted to a datetime. Don't forget the `skipinitialspace` keyword, else the names of the columns may start with a bunch of spaces. Perform the following tasks:\n", "* Some rainfall values in the dataset may be -1 (read the header of the file to learn why); set all rainfall values that are less than zero to zero. \n", "* Use the `plot` function of `pandas` to create a line plot of the daily rainfall with the number of the day (so not the date) along the horizontal axis. \n", @@ -1096,7 +1096,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 4, Compute monthly rainfall from daily rainfall\n", + "###Exercise 4, Compute monthly rainfall from daily rainfall\n", "In this exercise we are going to compute the total monthly rainfall for 2012 in the City of Rotterdam using the daily rainfall measurements we loaded in the previous Exercise; later on in this Notebook we learn convenient functions from `pandas` to do this, but here we are going to do this with a loop. Create an array of 12 zeros to store the monthly totals and loop through all the days in 2012 to compute the total rainfall for each month. The month associated with each row of the DataFrame may be obtained with the `.month` syntax, as shown above. Print the monthly totals (in mm/month) to the screen and create a bar graph of the total monthly rainfall (in mm/month) vs. the month using the `plt.bar` function of matplotlib. " ] }, @@ -1275,7 +1275,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 5, Resample weather data\n", + "###Exercise 5, Resample weather data\n", "The file `rotterdam_weather_2000_2010.txt` contains daily weather data at the weather station Rotterdam for the period 2000-2010 (again from the KNMI). Open the data file in an editor to see what is in it. Perform the following tasks:\n", "* Load the data making sure the dates are used as index. \n", "* Convert the rain and evaporation to mm/day, and the temperature to degrees Celcius. \n", diff --git a/old_notebooks/notebook_s3/py_exp_comp_s3.ipynb b/old_notebooks/notebook_s3/py_exp_comp_s3.ipynb index cdb5172..747dff2 100644 --- a/old_notebooks/notebook_s3/py_exp_comp_s3.ipynb +++ b/old_notebooks/notebook_s3/py_exp_comp_s3.ipynb @@ -85,7 +85,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 1. Histogram of the means of datasets with 100 values\n", + "###Exercise 1. Histogram of the means of datasets with 100 values\n", "Generate 1000 datasets each with 100 values drawn from a normal distribution with mean 4 and standard deviation 2; use a seed of 22. Compute the mean of each dataset and store them in an array of length 1000. Compute the mean of the means and the standard deviation of the means, and print them to the screen. Draw a boxplot of the means. In a separate figure, draw a histogram of the means and make sure the horizontal axis extends from 3 to 5. Recall that you can start a new figure with the `figure()` function. " ] }, @@ -109,7 +109,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 2. Histogram of the means of datasets with 1000 values\n", + "###Exercise 2. Histogram of the means of datasets with 1000 values\n", "Repeat exercise 1 but now generate 1000 datasets each with 1000 values (rather than 100 values) drawn from the same normal distribution with mean 4 and standard deviation 2, and again with a seed of 22. Make sure that the limits of the horizontal axis of the histogram go from 3 to 5, so that the histogram can be compared to the histogram you created above. Is the spread of the mean much smaller now as compared to the the datasets consisting of only 100 values?" ] }, @@ -189,7 +189,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 3. Count the number of means outside 95 percentile\n", + "###Exercise 3. Count the number of means outside 95 percentile\n", "Go back to Exercise 1. Generate 1000 datasets each with 100 values drawn from a normal distribution with mean 4 and standard deviation 2; use a seed of 22. For each dataset, evaluate whether the sample mean is within the 95 percentile of the $t$-distribution around the true mean of 4 (the standard deviation of the sample mean is different every time, of course). Count how many times the sample mean is so low that it is below the 5 percentile of the $t$ distribution around the true mean. If the theory is correct, it should, of course, be the case for about 5% of the datasets. Try a few different seeds." ] }, @@ -213,7 +213,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 4. $t$ test on dataset of 20 values\n", + "###Exercise 4. $t$ test on dataset of 20 values\n", "Generate 20 datapoints from a Normal distribution with mean 39 and standard deviation 4. Use a seed of 2. Compute and report the mean and standard deviation of the dataset and the standard deviation of the mean." ] }, @@ -253,7 +253,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 5. Hypothesis tests on Wooden beam data\n", + "###Exercise 5. Hypothesis tests on Wooden beam data\n", "Load the data set of experiments on wooden beams stored in the file `douglas_data.csv`. First, consider the first 20 measurements of the bending strength. Compute the sample mean and the standard deviation of the sample mean. The manufacturer claims that the mean bending strength is only 50 Pa. Perform a $t$-test (significance level 5%) with null hypothesis that the mean is indeed 50 Pa and alternative hypothesis that the mean is not 50 Pa using the approach applied in Exercise 4." ] }, @@ -364,7 +364,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 6. Explore Central Limit Theorem for Gamma Distribution\n", + "###Exercise 6. Explore Central Limit Theorem for Gamma Distribution\n", "Generate $N$ datasets of 20 numbers randomly drawn from a Gamma distribution with shape parameter equal to 2 and scale equal to 1. Draw a histogram of the means of the $N$ datasets using 20 bins. On the same graph, draw the pdf of the Normal distribution using the mean of means and sample standard deviation of the mean; choose the limits of the $x$-axis between 0 and 4. Make 3 graphs, for $N=100,1000,10000$ and notice that the distribution starts to approach a Normal distribution. Add a title to each graph stating the number of datasets." ] }, @@ -395,7 +395,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -423,7 +423,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -451,7 +451,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -479,7 +479,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -525,7 +525,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -583,7 +583,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/old_notebooks/notebook_s3/py_exp_comp_s3_sol.ipynb b/old_notebooks/notebook_s3/py_exp_comp_s3_sol.ipynb index 9a7bf05..d96e847 100644 --- a/old_notebooks/notebook_s3/py_exp_comp_s3_sol.ipynb +++ b/old_notebooks/notebook_s3/py_exp_comp_s3_sol.ipynb @@ -103,7 +103,7 @@ "id": "29f9cd23", "metadata": {}, "source": [ - "###Exercise 1. Histogram of the means of datasets with 100 values\n", + "###Exercise 1. Histogram of the means of datasets with 100 values\n", "Generate 1000 datasets each with 100 values drawn from a normal distribution with mean 4 and standard deviation 2; use a seed of 22. Compute the mean of each dataset and store them in an array of length 1000. Compute the mean of the means and the standard deviation of the means, and print(them to the screen. Draw a boxplot of the means. In a separate figure, draw a histogram of the means and make sure the horizontal axis extends from 3 to 5. Recall that you can start a new figure with the `figure()` function. " ] }, @@ -133,7 +133,7 @@ "id": "2e83c815", "metadata": {}, "source": [ - "###Exercise 2. Histogram of the means of datasets with 1000 values\n", + "###Exercise 2. Histogram of the means of datasets with 1000 values\n", "Repeat exercise 1 but now generate 1000 datasets each with 1000 values (rather than 100 values) drawn from the same normal distribution with mean 4 and standard deviation 2, and again with a seed of 22. Make sure that the limits of the horizontal axis of the histogram go from 3 to 5, so that the histogram can be compared to the histogram you created above. Is the spread of the mean much smaller now as compared to the the datasets consisting of only 100 values?" ] }, @@ -250,7 +250,7 @@ "id": "e2e6abf9", "metadata": {}, "source": [ - "###Exercise 3. Count the number of means outside 95 percentile\n", + "###Exercise 3. Count the number of means outside 95 percentile\n", "Go back to Exercise 1. Generate 1000 datasets each with 100 values drawn from a normal distribution with mean 4 and standard deviation 2; use a seed of 22. For each dataset, evaluate whether the sample mean is within the 95 percentile of the $t$-distribution around the true mean of 4 (the standard deviation of the sample mean is different every time, of course). Count how many times the sample mean is so low that it is below the 5 percentile of the $t$ distribution around the true mean. If the theory is correct, it should, of course, be the case for about 5% of the datasets. Try a few different seeds." ] }, @@ -280,7 +280,7 @@ "id": "43c77a4b", "metadata": {}, "source": [ - "###Exercise 4. $t$ test on dataset of 20 values\n", + "###Exercise 4. $t$ test on dataset of 20 values\n", "Generate 20 datapoints from a Normal distribution with mean 39 and standard deviation 4. Use a seed of 2. Compute and report the mean and standard deviation of the dataset and the standard deviation of the mean." ] }, @@ -331,7 +331,7 @@ "id": "7773d469", "metadata": {}, "source": [ - "###Exercise 5. Hypothesis tests on Wooden beam data\n", + "###Exercise 5. Hypothesis tests on Wooden beam data\n", "Load the data set of experiments on wooden beams stored in the file `douglas_data.csv`. First, consider the first 20 measurements of the bending strength. Compute the sample mean and the standard deviation of the sample mean. The manufacturer claims that the mean bending strength is only 50 Pa. Perform a $t$-test (significance level 5%) with null hypothesis that the mean is indeed 50 Pa and alternative hypothesis that the mean is not 50 Pa using the approach applied in Exercise 4." ] }, @@ -549,7 +549,7 @@ "id": "1df135a2", "metadata": {}, "source": [ - "###Exercise 6. Explore Central Limit Theorem for Gamma Distribution\n", + "###Exercise 6. Explore Central Limit Theorem for Gamma Distribution\n", "Generate $N$ datasets of 20 numbers randomly drawn from a Gamma distribution with shape parameter equal to 2 and scale equal to 1. Draw a histogram of the means of the $N$ datasets using 20 bins. On the same graph, draw the pdf of the Normal distribution using the mean of means and sample standard deviation of the mean; choose the limits of the $x$-axis between 0 and 4. Make 3 graphs, for $N=100,1000,10000$ and notice that the distribution starts to approach a Normal distribution. Add a title to each graph stating the number of datasets." ] }, @@ -587,7 +587,7 @@ "id": "ec1090d0", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -659,7 +659,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -731,7 +731,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -776,7 +776,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, { @@ -862,7 +862,7 @@ "source": [ "Back to Exercise 4\n", "\n", - "Answers to Exercise 5" + "Answers to Exercise 5" ] }, { @@ -985,7 +985,7 @@ "source": [ "Back to Exercise 5\n", "\n", - "Answers to Exercise 6" + "Answers to Exercise 6" ] }, { diff --git a/old_notebooks/notebook_wm1/py_exp_comp_wm1.ipynb b/old_notebooks/notebook_wm1/py_exp_comp_wm1.ipynb index 7a8e2da..808af89 100644 --- a/old_notebooks/notebook_wm1/py_exp_comp_wm1.ipynb +++ b/old_notebooks/notebook_wm1/py_exp_comp_wm1.ipynb @@ -183,7 +183,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 1. Average annual rainfall by country\n", + "###Exercise 1. Average annual rainfall by country\n", "The file `annual_precip.csv` contains the average yearly rainfall and total land area for all the countries in the world (well, there are some missing values); the data is available on the website of the world bank. Open the data file to see what it looks like. Load the data with the `read_csv` function of `pandas` and perform the following tasks:\n", "\n", "* Report the average annual rainfall for Panama\n", @@ -290,7 +290,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 2, Compute monthly rainfall from daily rainfall\n", + "###Exercise 2, Compute monthly rainfall from daily rainfall\n", "In this exercise we are going to compute the total monthly rainfall for 2012 in the city of Delft in The Netherlands using daily rainfall measurements. The data is obtained from the website of the Royal Dutch Meteorological Society (KNMI) and is given in the file `rainfall_delft_2012.dat`. First open the file in a text editor to see what the file looks like (for example, you can do File and Open in CanopyExpress to open the file in the Canopy editor). At the top of the file, an explanation is given of the data in the file (in Dutch). Read this (if you speak Dutch). Load the data file with the `read_csv` function of `pandas`. Use the keyword `skiprows` to skip all rows except for the row with the names of the columns. Use the keyword `parse_dates` to give either the name or number of the column that should be converted to a datetime. Next, create an array of 12 zeros to store the monthly totals and loop through all the days in 2012 to compute the total rainfall for each month. Print the monthly totals to the screen and plot the total monthly rainfall vs. the month. Use a `bar` plot (type `help(bar)`). " ] }, @@ -363,7 +363,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 3, Rainfall in March\n", + "###Exercise 3, Rainfall in March\n", "As you can see from the previous exercise, rainfall can vary dramatically from month to month. You probably won't be surprised that rainfall also varies from year to year. The file `rainfall_delft_all.dat` contains daily rainfall measurements in Delft since 1952. Your task is to compute the total daily rainfall for the month of March for each year. You need to think a bit on how to do this. For example, you may want to loop through the data first and extract all the March data and then compute the monthly totals. Report the wettest March in the dataset. Make a bar graph of the total rainfall in March for each year. Plot a blue bar when the total rainfall in March is more than 100 mm, a green bar when the total rainfall in March is less than 100 mm but more than 50 mm, and a black bar when the total rainfall in March is less than 50 mm. Note the units on the rainfall data. The color of a bar may be specified with the color keyword." ] }, @@ -394,7 +394,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -491,7 +491,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -525,7 +525,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -541,7 +541,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { diff --git a/old_notebooks/notebook_wm1/py_exp_comp_wm1_sol.ipynb b/old_notebooks/notebook_wm1/py_exp_comp_wm1_sol.ipynb index 78b3adf..e26092a 100644 --- a/old_notebooks/notebook_wm1/py_exp_comp_wm1_sol.ipynb +++ b/old_notebooks/notebook_wm1/py_exp_comp_wm1_sol.ipynb @@ -346,7 +346,7 @@ "id": "0d4d616c", "metadata": {}, "source": [ - "###Exercise 1. Average annual rainfall by country\n", + "###Exercise 1. Average annual rainfall by country\n", "The file `annual_precip.csv` contains the average yearly rainfall and total land area for all the countries in the world (well, there are some missing values); the data is available on the website of the world bank. Open the data file to see what it looks like (Notepad, Textedit, even Word if you have to). Load the data with the `read_csv` function of `pandas`, making sure that the names of the countries can be used to select a row, and perform the following tasks:\n", "\n", "* Print the first 5 lines of the DataFrame to the screen with the `.head()` function.\n", @@ -618,7 +618,7 @@ "id": "d986817b", "metadata": {}, "source": [ - "###Exercise 2. Average annual rainfall by country continued\n", + "###Exercise 2. Average annual rainfall by country continued\n", "Continue with the average yearly rainfall and total land area for all the countries in the world and perform the following tasks:\n", "\n", "* Add a new column that stores the total average annual freshwater influx in km$^3$/year for each country. Make sure you convert your units correctly. \n", @@ -785,7 +785,7 @@ "id": "2dcafffa", "metadata": {}, "source": [ - "###Exercise 3, Load and plot daily rainfall\n", + "###Exercise 3, Load and plot daily rainfall\n", "Rainfall data for the Netherlands may be obtained from the website of the Royal Dutch Meteorological Society KNMI . Daily rainfall for the weather station Rotterdam in 2012 is stored in the file `rotterdam_rainfall_2012.txt`. First open the file in a text editor to see what the file looks like. At the top of the file, an explanation is given of the data in the file. Read this. Load the data file with the `read_csv` function of `pandas`. Use the keyword `skiprows` to skip all rows except for the row with the names of the columns. Use the keyword `parse_dates` to give either the name or number of the column that should be converted to a datetime. Don't forget the `skipinitialspace` keyword, else the names of the columns may start with a bunch of spaces. Perform the following tasks:\n", "* Some rainfall values in the dataset may be -1 (read the header of the file to learn why); set all rainfall values that are less than zero to zero. \n", "* Use the `plot` function of `pandas` to create a line plot of the daily rainfall with the number of the day (so not the date) along the vertical axis. \n", @@ -862,7 +862,7 @@ "id": "1e9b7c76", "metadata": {}, "source": [ - "###Exercise 4, Compute monthly rainfall from daily rainfall\n", + "###Exercise 4, Compute monthly rainfall from daily rainfall\n", "In this exercise we are going to compute the total monthly rainfall for 2012 in the City of Rotterdam using the daily rainfall measurements we loaded in the previous Exercise; later on in this Notebook we learn convenient functions from `pandas` to do this, but here we are going to do this with a loop. Create an array of 12 zeros to store the monthly totals and loop through all the days in 2012 to compute the total rainfall for each month. The month associated with each row of the DataFrame may be obtained with the `.month` syntax, as shown above. Print the monthly totals (in mm/month) to the screen and create a bar graph of the total monthly rainfall (in mm/month) vs. the month using the `plt.bar` function of matplotlib. " ] }, @@ -1063,7 +1063,7 @@ "id": "1c92fee1", "metadata": {}, "source": [ - "###Exercise 5, Resample weather data\n", + "###Exercise 5, Resample weather data\n", "The file `rotterdam_weather_2000_2010.txt` contains daily weather data at the weather station Rotterdam for the period 2000-2010 (again from the KNMI). Open the data file in an editor to see what is in it. Perform the following tasks:\n", "* Load the data making sure the dates are used as index. \n", "* Convert the rain and evaporation to mm/day, and the temperature to degrees Celcius. \n", @@ -1214,7 +1214,7 @@ "id": "e09a9485", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -1319,7 +1319,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -1400,7 +1400,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -1522,7 +1522,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -1543,7 +1543,7 @@ "id": "2c9c5587", "metadata": {}, "source": [ - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { diff --git a/old_notebooks/notebook_wm2/py_exp_comp_wm2.ipynb b/old_notebooks/notebook_wm2/py_exp_comp_wm2.ipynb index f80fc21..5f29a49 100644 --- a/old_notebooks/notebook_wm2/py_exp_comp_wm2.ipynb +++ b/old_notebooks/notebook_wm2/py_exp_comp_wm2.ipynb @@ -35,7 +35,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Exercise 1: Manning's equation\n", + "###Exercise 1: Manning's equation\n", "The average velocity of water flowing in a canal may be estimated with Manning's equation. Manning's equation for the average velocity $v$ is\n", "\n", "$v=\\frac{1}{n}R_h^{2/3}S^{1/2}$\n", @@ -216,7 +216,7 @@ "metadata": {}, "source": [ "***\n", - "###Exercise 2. Emptying a reservoir \n", + "###Exercise 2. Emptying a reservoir \n", "Consider a reservoir filled with water that is emptied by opening a gate at the bottom of the reservoir. \n", "The water level in the reservoir is measured with a pressure transducer on the bottom of the reservoir while it is being emptied. \n", "The rate of change of the water level $h$ in the reservoir is theoretically related to the water level in the reservoir as\n", @@ -331,7 +331,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -451,7 +451,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { diff --git a/old_notebooks/notebook_wm2/py_exp_comp_wm2_sol.ipynb b/old_notebooks/notebook_wm2/py_exp_comp_wm2_sol.ipynb index de77cb9..fdc5d36 100644 --- a/old_notebooks/notebook_wm2/py_exp_comp_wm2_sol.ipynb +++ b/old_notebooks/notebook_wm2/py_exp_comp_wm2_sol.ipynb @@ -36,7 +36,7 @@ "id": "b8a7f22f", "metadata": {}, "source": [ - "###Exercise 1: Manning's equation\n", + "###Exercise 1: Manning's equation\n", "The average velocity of water flowing in a canal may be estimated with Manning's equation. Manning's equation for the average velocity $v$ is\n", "\n", "$v=\\frac{1}{n}R_h^{2/3}S^{1/2}$\n", @@ -316,7 +316,7 @@ "metadata": {}, "source": [ "***\n", - "###Exercise 2. Emptying a reservoir \n", + "###Exercise 2. Emptying a reservoir \n", "Consider a reservoir filled with water that is emptied by opening a gate at the bottom of the reservoir. \n", "The water level in the reservoir is measured with a pressure transducer on the bottom of the reservoir while it is being emptied. \n", "The rate of change of the water level $h$ in the reservoir is theoretically related to the water level in the reservoir as\n", @@ -458,7 +458,7 @@ "id": "8821f0b7", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -696,7 +696,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { diff --git a/old_notebooks/notebook_wm3/py_exp_comp_wm3_sol.ipynb b/old_notebooks/notebook_wm3/py_exp_comp_wm3_sol.ipynb index a52787c..3becbc9 100644 --- a/old_notebooks/notebook_wm3/py_exp_comp_wm3_sol.ipynb +++ b/old_notebooks/notebook_wm3/py_exp_comp_wm3_sol.ipynb @@ -49,7 +49,7 @@ "id": "c73b3199", "metadata": {}, "source": [ - "###Exercise 1 Pressure drop in a pipe. \n", + "###Exercise 1 Pressure drop in a pipe. \n", "Write a Python function that returns the pressure drop in a pipe. Input arguments are $f$, $L$, $Q$, and $D$. All input arguments need to be keyword arguments. The discharge needs to be in m$^3$/s. Output is the pressure drop in meters of water column. The function needs to be called `pdrop`. Test your function for a discharge $Q=200$ m$^3$/hour through a pipe with length $L=1000$ m, diameter $D=300$ mm, and friction factor $f=0.02$. Compute by hand what the value should be, then check whether your Python function gives the same value." ] }, @@ -147,7 +147,7 @@ "id": "60fd377a", "metadata": {}, "source": [ - "###Exercise 2 A series of pipes\n", + "###Exercise 2 A series of pipes\n", "Consider a simple water distribution system consisting of 11 nodes and 10 pipes. The system starts at node $A$ and ends at node $K$. The pipes are numbered 0 through 9. All pipes are connected one after the other (so pipe 0 goes from node $A$ to $B$, pipe 1 from $B$ to $C$, etc., and pipe 9 goes from node $J$ to node $K$). Each pipe is 1000 m long and has a friction coefficient of 0.02. The diameter of pipes 0-4 is 380 mm, and the diameter of pipes 5-9 is 300 mm. Water enters the system at node $A$ where the pressure is fixed to 40 m. 50 m$^3$/hour is taken from all nodes except node $A$. \n", "\n", "You are asked to write code to determine the pressure in each node. Create arrays of length 10 for the friction factor $f$, pipe length $L$, diameter $D$, and discharge in the pipe $Q$. Compute the discharge in each pipe form continuity (i.e., starting at the end, the discharge in pipe 9 should be equal to the discharge taken out at node $K$; the discharge in pipe 8 should equal the discharge taken out at node $J$ plus the discharge in pipe 9, etc.). Compute the pressure in each node with a `for` loop. The pressure in node $i+1$ is equal to the pressure in node $i$ minus the pressure drop over the pipe between nodes $i$ and $i+1$. Use the function you wrote for Exercise 1. Plot the pressure (on the vertical axis) for each node (on the horizontal axis) from $A$ though $K$. Label the nodes $A$ through $K$ with the `xticks` command and label the axes. When you do things correctly, the pressure in node $K$ should be around 19.5 m." @@ -358,7 +358,7 @@ "id": "19cff927", "metadata": {}, "source": [ - "###Exercise 3. A rectangular pipe network\n", + "###Exercise 3. A rectangular pipe network\n", "Consider flow through the following pipe system. Water enters at node $A$ where the pressure is maintained at 40 m. 200 m$^3$/hour is used at node B, and 300 m$^3$/hour is used at node C. Pipes 0 and 2 are 1600 m long, while pipes 1 and 3 are 1200 m long. $f=0.02$ and $D=300$ mm for all pipes. Use the method of Hardy Cross to compute the discharges in all pipes (in m$^3$/hour) and compute the pressure drops in all pipes. Finally, compute the pressure in node C. \n", "\n", "" @@ -390,7 +390,7 @@ "id": "26d3c64a", "metadata": {}, "source": [ - "###Exercise 4. A water distribution system with two loops\n", + "###Exercise 4. A water distribution system with two loops\n", "An additional pipe is installed in the distribution system of Exercise 3, as shown in the figure below. Pipe 4 runs from node A to node C and is 2000 m long. The other properties are the same as for the other pipes. Water can now flow from Node A to Node C in three different ways. This redundancy is a good feature of a water distribution system, but it also makes it more complicated to compute the discharge in each pipe and the pressure in each node. To find a solution, we need to iterate between the two loops: solve for $\\Delta Q_1$ and adjust discharge estimates $Q_0$, $Q_1$, and $Q_4$ accordingly, solve for $\\Delta Q_2$ and adjust discharge estimates $Q_2$, $Q_3$, and $Q_4$ accoringly, then back to solve for $\\Delta Q_1$ and adjust discharge estimates $Q_0$, $Q_1$, and $Q_4$ accordingly, etc., until the solution converges. The solution has converged when the values of $\\Delta Q_1$ and $\\Delta Q_2$ approach zero, which means that the discharges in the pipes don't change anymore from iteration to iteration. We will solve for the discharges and pressure distribution in the system shown below in three steps.\n", "\n", "\n", @@ -484,7 +484,7 @@ "id": "3261b2f1", "metadata": {}, "source": [ - "Answers to Exercise 1" + "Answers to Exercise 1" ] }, { @@ -523,7 +523,7 @@ "source": [ "Back to Exercise 1\n", "\n", - "Answers to Exercise 2" + "Answers to Exercise 2" ] }, { @@ -582,7 +582,7 @@ "source": [ "Back to Exercise 2\n", "\n", - "Answers to Exercise 3" + "Answers to Exercise 3" ] }, { @@ -638,7 +638,7 @@ "source": [ "Back to Exercise 3\n", "\n", - "Answers to Exercise 4" + "Answers to Exercise 4" ] }, {