This exercise has to be done on a linux machine! You can use the university workstations for this.
Here is the C code sample from the lecture:
#include <stdio.h>
int main ()
{
int a = 2;
int b = 3;
int c = a + b;
printf ("Sum of two numbers : %d \n", c);
}
Write this code in a C code file, compile and run it.
Now, replace the line int b = 3
with char b = 't'
. Compile and run the program again. Does the output match your expectations? Can you explain what happens? Compare this behavior to python's, and try to explain why this behavior can lead to faster execution times.
A simple way to estimate $\pi$ using a computer is based on a Monte-Carlo method. By drawing a sample of N points with random 2D coordinates (x, y) in the [0, 1[
range, the ratio of points that fall within the unit circle divided by the total number of points (N) gives an estimate of $\pi / 4$.
Provide two implementations of the monte-carlo estimation of $\pi$: a pure python version (standard library) and a vectorized version using numpy (no for-loop). Time their execution for N = [1e2, 1e3, ..., 1e8]. Plot the numpy speed-up as a function of N.
Tip: you can try to mimic %timeit
in your code by running each function at least three times and taking the fastest execution of all three.
Optional: try the numpy version with N=1e9 and above. Make conclusions about a new trade-off happening for large values of N.
Monthly averages of temperature data at Innsbruck can be downloaded from this lecture's github via:
from urllib.request import Request, urlopen
import json
# Parse the given url
url = 'https://raw.githubusercontent.com/fmaussion/scientific_programming/master/data/innsbruck_temp.json'
req = urlopen(Request(url)).read()
# Read the data
data = json.loads(req.decode('utf-8'))
(original data obtained from NOAA's Global Surface Summary of the Day)
Using numpy/scipy, matplotlib, and the standard library only, compute and plot the mean monthly annual cycle for 1981-2010 and the mean annual temperature timeseries for 1977-2017. Compute the linear trend (using scipy.stats.linregress) of the average annual temperature over 1977-2017. Repeat with winter (DJF) and summer (JJA) trends.
Back to the table of contents