Introduction to Computational Physics HS 2019 Exercise sheet 00 Lucas B¨ottcher

General information You might receive additional information and guidance at the beginning of every exercise class. Afterwards, you should start working on the tasks, so that you can ask questions and get help. Since hand-in is voluntary, we will not provide detailed corrections. You are free to use your favorite programming language (python, ++,...). Keep in mind that the teaching assistant is not meant to (and also cannot) assist with debugging and setup on every specific platform. However, he will do his best to help with the conceptual design and logic of your code.

The procedure for handing in your results (if you want to do so) is: 1. Create a new directory which is named in the format ex##_lastname_firstname.

2. Copy the solution files into this directory. The files should be formatted in a readable way i.e. source code (*.py, *.cpp,...) as well as a scientific report (one pdf-file) including figures and comments.

3. Zip the directory ex##_lastname_firstname and send it to [email protected].

Exercise 1. Plotting a function Goal: We start by getting familiar with the basic features of the programming language of our choice and learn how to plot a simple mathematical function. For most students this is meant to be a short revision. However, we still expect that some students are absolute beginners. In this case you will probably need more time and dedication to get the most out of the lectures and exercise classes.

Choose whatever mathematical function you want (e.g. sin(x), log(x), exp(x), ...) and plot it. For most people this will be achieved by using a combination of python and or C++ and . But as mentioned before you are free to use any other programming language that you like and that you are (in the best case) familiar with.

Hint: You will need to add the line import numpy as np in python and #include in C++.

1 Additional instructions

We expect you to have already some, at least basic, knowledge about and programming and experience working with a programming language like python, C++, etc. and tools like Mathematica, Matlab, etc.

While any familiarity with such languages and tools is highly favorable and makes it much easier for you to keep pace with this course with a lesser amount of time and effort, being an absolute beginner will not disqualify you from taking the course. We will do our best to help you establish a solid and comprehensive background in this field which you will be able to further develop in the future and from which you will have benefits for the rest of your career.

Linux as

We generally regard as the most convenient operating system and python (or C++) as the most suitable programming language(s) under which we are going to develop our programs and simulations. The reason for this is to have a uniform working environment and to avoid diversity facilitating an easy and fast interaction between the students. However, everyone is free to choose his/her operating system and programming language as long as the goal of the lectures and the exercises is reached.

In the following sections we will help you to write simple programs in python and C++ and to run them under Linux.

Logging in and getting things to work

If you do not have your own computer ETH provides you with an access to the ETH computers. The ETH computers are double boot machines i.e. you may have to restart for Linux.

1. Log in with your account information.

2. Open a browser. Under the following address you can find all the information about this course: https://moodle-app2.let.ethz.ch/course/view.php?id=11254.

3. Open a terminal (shell). A shell is a program that provides the traditional, text-only user interface for Linux and other -like operating systems. Its primary function is to read commands that are typed into a console (i.e. an all-text display mode) or ter- minal window (an all-text window) and then execute (i.e. run) them. (Definition from http://www.linfo.org.)

4. Create a new directory named e.g. comphys: mkdir comphys

5. Enter the created directory: cd comphys

6. Start an editor (e.g. gedit) to write a program in python: gedit hello.py (In C++: gedit hello.cpp) (Always use reasonable names that tell the user a little bit about the content of the file.)

7. Write whatever you like, save and exit the editor.

2 Some useful commands

COMMAND USAGE pwd shows the path of the current directory mkdir name creates the directory ’name’ cd name changes to the directory ’name’ gedit starts the GNU-Editor (recommended) (x)emacs starts the (X)EMACS editor ls lists the files and directories in the current directory cp, mv copies, moves files and directories man displays the documentation to a command python3 name.py runs the python program ’name’ g++ name.cpp -o name.out compiles the C++ program ’name’ ./name runs the executable file ’name’ command & moves the command ’command’ to the background exit exits shell/window

Hello, World!

In the following we show you how to write a basic program in python and C++. python

1. Navigate (using the cd command) to the folder comphys that we already created.

2. Check (using the ls command) that the file hello.py is still inside.

3. Open the editor to edit the file hello.py: gedit hello.py 4. Write the following code and save it:

print(’Hello, World!’)

5. Run the program: python3 hello.py

C++

1. Navigate (using the cd command) to the folder comphys that we already created.

2. Check (using the ls command) that the file hello.cpp is still inside.

3. Open the editor to edit the file hello.cpp: gedit hello.cpp. 4. Write the following code and save it:

#include

int main(){ std::cout << "Hello, World!" << std::endl; return 0; }

3 5. Compile the program: g++ hello.cpp -o hello

6. Run the program: ./hello

Plotting a parabola

Finally, we show you how to create an elementary plot with python and C++. python

1. Create a new file parabola.py.

2. Write the following code and save it:

import matplotlib.pyplot as plt

x = [i for i in range(10)] y = [i**2 for i in range(10)]

plt.figure() plt.plot(x, y) plt.show()

3. Run the program: python3 parabola.py

C++

1. Create a new file parabola.cpp.

2. Write the following code and save it:

#include

int main(){ for(int i = 0; i < 10; ++i) std::cout << i << "\t" << i*i << std::endl; return 0; }

3. Compile the program: g++ parabola.cpp -o parabola

4. Run the program and redirect the output to a separate file: ./parabola > parabola.dat.

5. Plot the data in parabola.dat using e.g. gnuplot. To do so, start it: gnuplot Then, type: plot ’parabola.dat’

4