In [1]:
import numpy as np
import matplotlib.pyplot as plt
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
plt.rcParams.update({'font.size':14}) 

Introduction to Jupyter Notebooks - Exercises

Overview:

This last section has some more exercises for you to practice your new Jupyter notebook skills. Not all of these exercises have linked solutions.

Distances

In the second half of your Relativity and Astro unit this semester you're going to come across the distance modulus equation:

$$ \mu = 5 \log_{10} d - 5 $$

where $d$ is the distance in parsecs and $\mu$ is the distance modulus (in magnitudes), defined as $$ \mu = m - M $$ where $m$ is the apparent magnitude and $M$ is the absolute magnitude of an object.

Using markdown cells, write out an equation for $d$ as a function of $\mu$.

Write a function that takes a distance in parsecs as input and gives the distance modulus as output. Write another function that does the reverse.

Use your function to work out the distance in kpc ($10^3$ pc) of the Large Magellanic Cloud ($\mu = 18.5$ mag).

What is the distance modulus of the Andromeda galaxy ($d = 778 $ kpc).

If Andromeda's apparent magnitude is 3.4 mag, what is it's absolute magnitude?

Hint: The magnitude system is backwards (thanks, Ancient Greeks) so a magnitude of -10 is brighter than a magnitude of 0, which is brighter than a magnitude of 10.

solution

The central limit theorem

Use the np.random.normal function to generate an array of 10 random numbers with a mean of 10 and a standard deviation of 3. Using density=True in plt.hist plot the probability density of the results.

Repeat this for samples of 100 and 1000 random numbers and plot the histograms of the results. Choose an appropriate bin size for your histogram.

On the same plot as each of your histograms, plot a Normal (Gaussian) distribution:

$$ f(x) = \dfrac{1}{\sigma\sqrt{2\pi}} e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2}$$

for $\mu = 10$, $\sigma = 3$.

Are the results what you expect? Write a few sentances about how the results compare to your expectations for 10, 100 and 1000 random numbers.

Write a function that takes the sample size ($n$), mean ($\mu$), and standard deviation ($\sigma$) as input and generates a sample of $n$ normally distributed random numbers. Your function should plot the probability density of the sample along with the Normal distribution. The plot should have a title that states the input values of $n$, $\mu$ and $\sigma$ and should have appropriate axis labels.

Look at how the plot changes as you increase $n$. Is it doing what you expect?

Hint: Take a look at the documentation for np.random.normal if you're not sure where to start.

solution

Pulse on a wire

In this exercise we're looking at how to apply your coding skills beyond this lab unit. You can use the same techniques that we use to look at concepts in the lab to help you visualise problems from your other units This question adapted from one of your Semester 1 Vibrations Waves and Optics problem sheets.

Consider a transverse wave pulse on a wire that, at $t=0$, can be described by

$$ y(x) = \dfrac{4 \times 10^{-3}}{\left(100 x\right)^2 + 4} $$

where $x$ and $y$ are both in metres.

Plot the pulse for $-0.1$ m $< x < 0.1$ m. Your plot should have appropriate axis labels and units.

Write down an expression for $y(x, t, v)$ for this wave travelling in the positive $x$ direction at a velocity $v$. Convert this expression into a function.

Plot the displacement, $y$, as a function of time at $x= 0.1$ m for a wave travelling with $v = 2$ m s$^{-1}$.

What is the earliest time that the displacement caused by the wave reaches 0.5 mm?

solution

In [ ]: