Some exercises to practice for the exam. The difficulty of the exercises are representative for the exam.
You don’t have to make all assignments, however, you should feel comfortable with Python and get well acquainted with measurement & uncertainty.
import matplotlib.pyplot as plt
import numpy as np
from scipy.special import erf
from scipy.optimize import curve_fit
Mean and standard deviation¶
You probably know the np.mean
and the np.std
commands. However you can also do them yourself using the definitions:
Use these on the following dataset and see if you get the same results as the np.mean
and the np.std
commands.
Exercise 1¶
The dataset is a measurement from 1 single flat by 11 different persons.
Measurement Number | Total Height (m) |
---|---|
1 | 141.02 |
2 | 148.15 |
3 | 157.27 |
4 | 115.81 |
5 | 149.22 |
6 | 207.52 |
7 | 180.20 |
8 | 161.92 |
9 | 164.28 |
10 | 206.80 |
11 | 42.50 |
Questions
a Calculate in two different ways the mean value, the standard deviation.
b How would you as a physicist write down the size of the flat?
If you notice a difference between the np.std
function and the manual function I would encourage you to look at the documentation of the np.std
function Here.
L = np.array([141.02,148.15,157.27,115.81,149.22,207.52,180.2,161.92,164.28,206.8,42.5])
Exercise 2¶
A shop owner wants to know how many people visit his shop. He installs a device that counts the number of people that enter the shop every minute. In total 1000 measurements were done.
Questions
a Import the data_1.dat file.
b Determine the average value as well as the standard deviation
c Plot the data in a histogram. What kind of distribution do you think the data is best described by, and why?
Data = np.genfromtxt("data_1.dat")
The Error Function and Chauvenaut’s theorem¶
For using Chauvenaut’s theorem you need to use the error function. The error function is defined as follows:
This is a so called non elementary function. It can be approximated numerically, luckily you don’t have to do this yourself because scipy
already has a function for this. The Erf is a function (more formally called the cdf function) which is more often used in statistics and is used in the Chauvenaut’s theorem.
Exercise 3¶
a Use from scipy.special import erf
and make a plot of the error function in the range [-3,3] to see how the function behaves.
b Use this to plot the Erf function, with the and found in exercise 1(for this you have to change the domain of your plot). This function is the same as the one that can be found on this site.
c Use the dataset from exercise 1 to see if the last value (with length 42.5 m) can be discarded. And if so, what kind of influence does this have on the mean and the std.
d What are the pros and cons of removing a point from a dataset?
from scipy.special import erf
#Erf function
def Erf(x,mean_u,sig):
return 0.5*(1+erf((x-mean_u)/(np.sqrt(2)*sig)))
#plot for error function
#plot for Erf function
#check whether the outlier can be discarded
Exercise 4 Height vs. weight¶
Attached is a csv file which contains the length (first column) and the weight (second column) of a (male) person.
a Convert the data to metric units as it is now in inches and in lbs.
b Make a scatter plot and see if you if you can fit a linear relation.
c Find the mean and std of both the height and weight.
d Make a histogram for both and overlay a Gaussian to see if the data follows a normal distribution.
from scipy.optimize import curve_fit
from scipy.stats import norm
#import data
data = np.genfromtxt('weight-height.csv',delimiter=',',dtype=float,skip_header=1)
height = data[]
weight = data[]
#convert to metric units
#calculate mean and std
#linear function to fit
#curvefit
#plot
plt.figure(figsize=(12,4))
plt.show()
#Histogram plot #1
#Histogram plot #2
Exercise 5¶
In an experiment 10 measurements were taken of the voltage over and current through a Si-diode. The results are displayed in the following table together with their uncertainties:
I (μA) | (μA) | V (mV) | (mV) |
---|---|---|---|
3034 | 4 | 670 | 4 |
1494 | 2 | 636 | 4 |
756.1 | 0.8 | 604 | 4 |
384.9 | 0.4 | 572 | 4 |
199.5 | 0.3 | 541 | 4 |
100.6 | 0.2 | 505 | 4 |
39.93 | 0.05 | 465 | 3 |
20.11 | 0.03 | 432 | 3 |
10.23 | 0.02 | 400 | 3 |
5.00 | 0.01 | 365 | 3 |
2.556 | 0.008 | 331 | 3 |
1.269 | 0.007 | 296 | 2 |
0.601 | 0.007 | 257 | 2 |
0.295 | 0.006 | 221 | 2 |
0.137 | 0.006 | 181 | 2 |
0.067 | 0.006 | 145 | 2 |
The diode is expected to behave according to the following relation: , where and are unknown constants .
a Use the curve_fit function to determine whether this is a valid model to describe the dataset.
Hint: use a logscale on the y-axis
I = np.array([3034,1494,756.1,384.9,199.5,100.6,39.93,20.11,10.23,5.00,2.556,1.269,0.601,0.295,0.137,0.067])*1e-6
a_I = np.array([4,2,0.8,0.4,0.3,0.2,0.05,0.03,0.02,0.01,0.008,0.007,0.007,0.006,0.006,0.006])*1e-6
V = np.array([670,636,604,572,541,505,465,432,400,365,331,296,257,221,181,145])*1e-3
a_V = np.array([4,4,4,4,4,4,3,3,3,3,3,2,2,2,2,2])*1e-3
#Function to fit
def current(V,a,b):
return a*(np.exp(b*V)-1)
#Make fit
Exercise 6¶
A student measures the position of a simple mass-spring system. Unfortunately, he accidently moves his measuring device during the experiment. He is not sure if the device was put back in the right position and wants to know if there is a systematic error in his data. The dataset consists of 400 position measurements (in cm) over the course of 5 seconds. The data is expected to follow a sine function with an amplitude of 4.5 and a period of 10π.
a Import the data_2.dat file.
b Plot the raw data, calculate and plot the residuals and determine whether there is indeed a systematic error in the data.
c If so, approximate the magnitude of the systematic error and the time at which the occurs.
t = np.linspace(0,5,400)
#sine function to fit the data
def sine(x):
return 4.5*np.sin(x*5*np.pi)
#plot of data and sine function
#calculate residuals
#plot of Residuals
Exercise 7 Functional vs. Calculus Approach¶
You have both seen the functional and the calculus method in calculating the propagation of an error.
Apply both methods on the following functions:
With
With
Exercise 8¶
The gravitational force between two bodies can be described with Newton’s law of universal gravitation: , where is the gravitational constant, the masses of the bodies and the distance between the bodies.
Suppose that a meteorite of mass kg at a distance of m is moving towards the earth. Determine the attracting force between the meteorite. Use both the functional and the calculus approach to calculate the uncertainty in and compare the results. You can use the following values:
Earth mass: kg
Gravitational constant: m3 s-2 kg-1
#function for gravitational force
def FG(G,m1,m2,r):
return G * m1 * m2 /(r*r)
#values
G = 6.6759e-11
u_G = 0.00030e-11
m1 = 4.739e8
u_m1 = 0.154e8
m2 = 5.9722e24
u_m2 = 0.0006e24
r = 2.983e6
u_r = 0.037e6
#value of gravitational force
#calculus approach
#Calculate difference between methods
Exercise 9¶
The behaviour of many gases can be well approximated by the ideal gas law: , where is the pressure, the volume of the gas, the amount of substance in moles, the temperature of the gas. is the ideal gas constant, which is actually just the Boltzmann constant multiplied by the Avogrado constant, with a value of 8.314 J⋅K-1⋅mol.
A student performs an experiment with a closed containter of litres filled with mol Helium gas. The volume remains constant throughout the experiment and no particles can enter or leave the container. The student slowly heats the gas and measures the change in pressure. The results are shown in the following table.
Temperature (K) | Pressure (kPa) |
---|---|
293.2 | 128.57 |
304.4 | 134.44 |
313.8 | 137.93 |
327.3 | 145.33 |
335.0 | 187.97 |
348.3 | 155.16 |
359.1 | 158.85 |
371.6 | 164.08 |
The uncertainty in is 0.2 K for all values.
a Calculate the uncertainty in using the calculus approach
b Fit the data to the model. What do you see? Is the data well described by the ideal gas law?
def Press(V,n,R,T): #ideal gas law
return n*R*T/V
#values
R = 8.314
n = 0.172
u_n = 0.001
V = 3.32e-3
u_V = 0.01e-3
#data
T = np.array([293.2,304.4,313.8,327.3,335.0,348.3,359.1,371.6])
u_T = np.ones(len(T))*0.2
P = np.array([128.57,134.44,137.93,145.33,187.97,155.16,158.85,164.08])*1e3
#model
#plot
c One data point seems to be an outlier. Use Chauvenet’s criterium to determine whether the point can be discarded.
Exercise 10¶
When measuring there is always a very real possibility of a systematic error. One of these systematic errors can be found in a mass-springsystem. Normally the period of a mass-spring system is given by: . Here is the mass and is the spring constant. However this formula assumes that you have a massless spring, this is not true unfortunately. This means that the mass of the spring is also vibrating, we should thus change the formula to take this into account. This gives the following equation: , where is the systematic error.
With the measurements that we have we can find both the spring constant and its uncertainties. The array m is an array with the values for the measured and the array T is an array with all the measured data for the period. You can disregard the invalid use of significant figures.
a Plot the data
b Find the parameters and with its corresponding uncertainties
c Plot the fitted function over the data and look at the residuals
from scipy.optimize import curve_fit
m = np.array([50,100,150,200,250,300])
T = np.array([2.47,3.43,4.17,4.80,5.35,5.86])
Error in the Error¶
There is always an error in the error. This number tells you whether you use 1 or 2 significant values for your uncertainty.
Exercise 11¶
See if you can reproduce the plot shown above, use a logarithmic scale on the x-axis and make the horizontal and vertical dashed lines (you can only do one point to show that you know how to do it).
Hint: use plt.hlines
and plt.vlines
to make it a bit easier (look at the documentation to see how the functions work).
def err_err(N):
return 1/(np.sqrt(2*N - 2))