Welcome to Computational Science, welcome to Python programming! In this course, you will implement various basic programming techniques in Python and apply them to solving physics problems.
In this course, you will use Python using a platform called "Jupyter Notebooks". Jupyter notebooks are a way to combine formatted text (like the text you are reading now), Python code (which you will use below), and the result of your code and calculations all in one place. Go through the notebooks and run the examples. Try to understand their functioning and, if necessary, add code (such as print statements or variable overviews) to make it clear for you. In addition, there are exercises and practice cells where you can program for yourself. Don't be afraid to start coding yourself, writing code and making mistakes is the best way to learn Python.
In this lecture, you will learn the basic concepts of programming in Python. To get started, we will first explain the basic concepts of what python is and how it works.
Learning objectives for this notebook:
%whos
to list the variables stored in the memory of the python kernelinput()
commandprint()
commandIn this notebook, we will explore the implementation of functions in Python.
Learning objectives for this notebook:
In this notebook, we will learn how to control the flow of execution of Python code using conditional statements and loops.
Learning objectives for this notebook:
if
, elif
, and else
==
, <
, >
and
and or
while
loop that exits on a particular conditionfor
loop that loops over a range of numbers using the range()
commandbreak
commandcontinue
Numpy (numerical Python) is a library designed for performing scientific computing in Python.
In this notebook, we will introduce numpy arrays, a data structure introduced in numpy for working with vectors and matrices. We will explore how to create them, how to manipulate them, and how to use them for efficient numerical calculations using numpy functions.
Learning objectives for this notebook:
np.zeros()
, np.linspace()
, np.random.random()
time()
In this notebook, we will explore how to load and save datafiles in Python using Numpy, and how to plot and explore data and functions with a library called Matplotlib.
Learning objectives for this notebook:
matplotlib
to create a plot that enables active zoomingcurve_fit
to perform a least-squares fit of a model to a datasetIn the previous lectures we focused on implementing small pieces of code to perform specific well-defined tasks for you. However, in general, computer programs can perform highly complicated data processing and can comprise of millions line of code (not in this course though). Usually, we say that a computer program is an *algorithm*, i.e., it is "a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of problems or to perform a computation". In order to develop these codes computer scientists use what is called algorithmic thinking. In essence, algorithmic thinking is the process of solving a problem in a systematic way. In this lecture notebook we develop some algorithmic thinking and go through the basic steps that are required to make a computer program from scratch.
Learning objectives for this notebook:
In this notebook, you will learn about additional programming concepts in Python. They are not part of the learning objectives of the course, but you may run into them at some point, or wonder what they are, or find them fun and useful if you already have some programming experience.
The topics we will cover include:
ipywidgets
In this notebook, we outline some example of "good coding practices": things you should do to make your code robust and understandable to others.
Learning objectives:
In this notebook, you will explore taking numerical derivatives and implementating various interpolation schemes in one and two dimensions.
Learning objectives: After finishing this notebook, you should be able to:
In this notebook, you will implement the different techniques for the numerical computation of integrals in python and explore aspects of numerical integration.
Learning objectives: After finishing this notebook, you should be able to:
In this lecture you will implement two techniques to determine the root of a non-linear function of one variable. The root of a linear functions can be solved analytically. For non-linear functions (e.g. sin$x = x/2$) this is sometimes possible, but in the most general case it is hard to write down a simple solution (sometimes a series expansions give you an analytical answer, but still this would require a computer to calculate it with the appropriate accuracy for you). In these cases you need a computer to do the job for you. In this notebook we will show you two methods that can be implemented to find the roots of a function of one variable, i.e., $f(x)=0$. In general, the sought solution can be multi-dimensional, which makes it more difficult to solve, but also similar methods can be implemented in that case. Root solving methods also can be used for finding the extrema of a function. Instead of solving for the roots of $f(x)$, solving the roots the derivative $f′(x)$ function, yields the local or global extrema of the function $f(x)$.
Learning objectives: After finishing this lecture, you should be able to:
In this lecture, you will implement linear algebra techniques in Python. You will see that for solving physics problems using linear algebra, the most complicated part is to translate the physical problem into a matrix-vector equation. When put in a standardized form, many linear algebra tools are at your disposal in the numpy package.
Learning objectives: After finishing this lecture, you should be able to:
In this lecture, we will explore how the Fast Fourier Transform works in python, with an emphasis of how to use it, what it does, and how to interpret the data it produces.
At the end of the notebook, we will also explore an example of using the FFT to find a weak singal in a noisy dataset.
Learning objectives: After finishing this lecture, you should be able to:
In the previous lecture notebook, we looked into detail about how the 1D FFT works in Python, and saw an example of using the FFT to detect a weak sinusoidal signal in a noisy dataset.
In this lecture notebook, you will explore the application of the 1D FFT for filtering signals, and also learn about the 2D FFT and and application of it in calculating diffraction patterns.
Learning objectives: After finishing this notebook, you should be able to:
In this lecture, you will implement random numbers in Python and learn how to make computations with them. First we characterize the statistical properties of random numbers by calculating the mean and standard deviation. Second, we perform numerical integration with random numbers.
Despite the fact that random numbers are not well defined, they vary between realizations, you can make calculations with them just as 'exact' as with normal numbers.
Learning objectives: After finishing this lecture, you should be able to:
In this lecture, you will implement Monte Carlo simulations in Python.
In this lecture, we will explore numerical methods of solving ordinary differential equations (ODEs).
Learning objectives: After completing this lecture, you should be able to:
solve_ivp()
routine from the scipy
libraryIn this lecture, we will explore applying the techniques for solving initial value problems that we learned in lecture 11. We will also learn how to use numerical integration of ODEs to solve boundary value problems.
Learning objectives: After completing this lecture, you should be able to:
In this lecture, we will explore numerically solving partial differential equations (PDEs) that have the form of boundary value problems using various techniques of relaxation.
Learning objectives: After completing this lecture, you should be able to:
In this lecture, we will explore numerically solving partial differential equations (PDEs) that have the form of an initial value problem using the forward-time centered-space (FTCS) and the spectral method.
Learning objectives: After completing this lecture, you should be able to: