Skip to article frontmatterSkip to article content

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:

x=1Ni=1Nxi\overline{x} = \frac{1}{N} \sum_{i=1}^N x_i
σx=1N1i=1N(xiμx)2\sigma_x = \sqrt{\frac{1}{N-1} \sum_{i=1}^N (x_i - \mu_x)^2 }

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 NumberTotal Height (m)
1141.02
2148.15
3157.27
4115.81
5149.22
6207.52
7180.20
8161.92
9164.28
10206.80
1142.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:

erf(x)=2π0xet2dterf(x) = \frac{2}{\sqrt{\pi}} \int_0 ^x e^{-t^2} dt

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.

Erf(x,x,σx)=12[1+erf(xx2σx)]Erf(x,\overline{x},\sigma_x) = \frac{1}{2} [1+erf(\frac{x-\overline{x}}{\sqrt{2}\sigma_x})]

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 σx\sigma_x and x\overline{x} 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)αI\alpha_I (μA)V (mV)αV\alpha_V (mV)
303446704
149426364
756.10.86044
384.90.45724
199.50.35414
100.60.25054
39.930.054653
20.110.034323
10.230.024003
5.000.013653
2.5560.0083313
1.2690.0072962
0.6010.0072572
0.2950.0062212
0.1370.0061812
0.0670.0061452

The diode is expected to behave according to the following relation: I=a(ebV1)I = a(e^{bV}-1), where aa and bb 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:

Z(x)=x1x+1Z(x)=\frac{x-1}{x+1}

With x=3.2±0.2 x = 3.2 \pm 0.2

Z(x)=ex2Z(x)=e^{x^2}

With x=8.745±0.005 x = 8.745 \pm 0.005

Exercise 8

The gravitational force between two bodies can be described with Newton’s law of universal gravitation: F=Gm1m2r2F = \frac{Gm_1m_2}{r^2}, where GG is the gravitational constant, mim_i the masses of the bodies and rr the distance between the bodies.

Suppose that a meteorite of mass (4.739±0.154)108(4.739\pm0.154)\cdot10^8kg at a distance of (2.983±0.037)106(2.983\pm0.037)\cdot10^6m 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 FF and compare the results. You can use the following values:

Earth mass: (5.9722±0.0006)1024(5.9722\pm0.0006)\cdot10^{24}kg

Gravitational constant: (6.67259±0.00030)1011(6.67259\pm0.00030)\cdot10^{-11}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: PV=nRTPV=nRT, where PP is the pressure, VV the volume of the gas, nn the amount of substance in moles, TT the temperature of the gas. RR 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⋅mol1^{-1} .

A student performs an experiment with a closed containter of 3.23±0.013.23\pm0.01 litres filled with 0.172±0.0010.172\pm0.001 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.2128.57
304.4134.44
313.8137.93
327.3145.33
335.0187.97
348.3155.16
359.1158.85
371.6164.08

The uncertainty in TT is 0.2 K for all values.

a Calculate the uncertainty in PP 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: T=2πmCT = 2\pi \sqrt{\frac{m}{C}}. Here mm is the mass and CC 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: T=2πm+ΔmCT = 2\pi \sqrt{\frac{m + \Delta m}{C}}, where Δm\Delta m 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 mm 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 Δm\Delta m and CC 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.

errror in the error=12N2\text{errror in the error} = \frac{1}{\sqrt{2N - 2}}

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))