Assignments for PHYS2020

Submitting assignments for PHYS2020

You will be required to complete a relatively small programming assignment each week. Note that you must e-mail the program and its output to me (Michael Ashley) before the deadline indicated for each assignment. You also must send the e-mail with a subject heading like "PHYS2020 week=2 student=3012345". Do not use attachments, put the program and the output from the program in the e-mail message itself. Late assignments will have marks deducted (2/10 off per day). Assignments are marked out of 4% (with a resolution of 0.4%) each week, with full marks being reserved for efforts that are outstanding.

Here is an example of how to submit the "hello.c" program, from a GNU/Linux or FreeBSD (e.g, ugrad.phys.unsw.edu.au) system using the bash shell (if you are not using bash, please change your shell to /usr/local/bin/bash using the chsh command; to determine whether you are using bash, type ps -x and see if you have a line containing "-bash (bash)"):

cat > hello.c <<EOF
#include <stdio.h>
int main(void) {
printf("hello world\n");
return 0;
}
EOF

gcc hello.c
./a.out &> output

cat hello.c output | mail -s "PHYS2020 week=2 student=3012345" mcba@phys.unsw.edu.au

Notes:

If you wish to e-mail me with a question about the course, don't use "PHYS2020" in the subject, since it will disappear into my automated assignment processing program, from where it is unlikely to reappear...

Assignment submission checklist:

Common problems that cost people marks:

IMPORTANT NOTE: the assignment should be your own work. It is OK to seek advice from other students, and solve problems as part of a team, but you should submit programs that you constructed yourself, and you should understand them completely. If you submit a program written in large part by someone else, then this is plagarism, and can result in severe consequences.


Week 2 assignment - due 9am Friday 19 March 2004

Note: if you don't have a username/password for ugrad.phys.unsw.edu.au, please see Kristien Clayton in Room 101 next to the Microcomputer Lab on level 1 of the Old Main Building (actually, it's in the Newton Building...). Kristien is available after 2:30pm each day.

Login to ugrad.phys.unsw.edu.au (you can do this from the Microcomputer Lab, or from any computer on the internet (to login, use ssh on GNU/Linux or putty on Windows)). Your username is your student number, and the password was e-mailed to your @student e-mail address.

Experiment with creating directories, creating files, deleting files, and so on. See the on-line course notes (updated since Monday's lecture) for details of GNU/Linux commands.

Write a program to output a simple character string, or the results of a simple calculation. Your program can be as simple as hello.c above (although this would only give you 5/10 due to a startling lack of originality).

With your program, submit some evidence that you have been successful at creating directories, files, etc. This evidence might be as simple as a listing of your directories (although you should be more imaginative to achieve full marks).

Solution.


Week 3 assignment - due 10am Friday 26 March 2004

Write a program to produce an ASCII representation of some geometrical shape, along the lines of, but more interesting than, the circle program from lectures.

Solution.


Week 4 assignment - due 10am Friday 2 April 2004

Write a program to produce a table of values of one or more mathematical functions. The table should be nicely formatted (using printf) and have appropriate headings. Your program could be as simple as a table of the sine function for every 10 degrees from 0 to 90 (worth maybe 6/10) or multiple functions across the page, or a vector function depending on two input numbers, arranged horizontally and vertically.

Solution.


Week 5 assignment - due 10am Friday 23 April 2004

The due date has been extended till Friday due to extended downtime of the physics website.

The data file http://www.phys.unsw.edu.au/~mcba/mass0.dat contains 25344 bytes of data. These data are the pixel values from an image with dimensions 176x144. The data is stored with the first coordinate varying most rapidly (i.e., the order is (0,0), (1,0), ... (175,0), (1,1), (2,1) ... (175,143)). The image was taken by the UNSW MASS experiment at Dome C in Antarctica on 31 March 2004.

Write a program to read this data file into a 176x144 array (note that you can input the file into your program using, e.g., "./a.out < ~mcba/mass0.dat").

Now, calculate and print the (x,y) coordinate and value of (1) the minimum-valued pixel, and (2) the maximum-valued pixel. For an additional 3 marks out of 10, calculate the centroid of the data in a 10x10 pixel box around the position of the maximum-valued pixel. For a definition of "centroid", see http://mathworld.wolfram.com/GeometricCentroid.html.

Here is an example program that will get you started:

#include <stdio.h>
#include <unistd.h>

const int xdim = 176;
const int ydim = 144;

int main(void) {

// A program to read the mass0.dat file, put the data into an
// unsigned char array, and calculate the total.

unsigned char image[ydim][xdim];
int i, j;
long total;

// Read the data from standard input.

if (xdim*ydim != read(STDIN_FILENO, image, xdim*ydim)) {
fprintf(stderr, "read failure\n");
return 1;
}

// Calculate the total.

total = 0;
for (j = 0; j < ydim; j++) {
for (i = 0; i < xdim; i++) {
total += image[j][i];
}
}

// Print the result.

printf("total = %ld\n", total);
return 0;
}

Compile with, e.g., "gcc example.c", run with "./a.out < ~mcba/mass0.dat", and you should see the output:

total = 1604377

Solution.


Week 6 assignment - due 10am Friday 30 April 2004

The purpose of this assignment is to use Monte Carlo techniques (explained below) to integrate a simple function.

Your task is to write a program to calculate the integral of sin(x) from 0 to pi/2 using Monte Carlo integration. You should print out your estimates of the integral as the number of iterations increases.

Notes: Monte Carlo integration works by randomly choosing points within an area A completely enclosing the function, and determining which points are below the function and which points are above. The ratio of points below the function to the total number of points, multiplied by the area A, is an estimate of the integral of the function.

A suitable rectangular area to solve this problem is 0 <= x <= pi/2, and 0 <= y <= 1. So, you need to begin by calculating two random numbers that will form an (x,y) coordinate that will uniformly covered this area. Here is an example of calculating random numbers. Then keep track of whether the coordinate is above or below sin(x).

The following plot shows sin(x) from 0 to pi/2, with 1000 random points superimposed.

Solution.


Week 7 assignment - due 10am Friday 14 May 2004

Redo last week's assignment using the 2-dimensional Sobel quasi-random number generator discussed in lectures, and show that it approaches the correct answer more rapidly than a random generator. For full marks you could compare the error (i.e., the difference between the correct answer and the Monte-Carlo approximation) in both cases (i.e., random and quasi-random), with 1/N and 1/sqrt(N), where N is the number of points used.


Week 8 assignment - due 9am Monday 17 May 2004

Write a program to sort something using qsort. What you sort is up to you, but higher marks will be awarded for innovation (as a rough guide, 5/10 if you make only a slight variation on the example provided in the section on sort in the notes; 8/10 if you do something like sort coordinate pairs in order of distance from the origin).


Week 9 assignment - due 10am Friday 28 May 2004

Write a function that, given the semi-major axis, eccentricity, and period, of a closed elliptical orbit in a simple, central, gravitational potential returns the maximum velocity that the particle reaches on the orbit.

You should think about (and document in your C function) what to do if the orbit is parabolic or hyperbolic.

Give the maximum velocities for the earth and some other bodies in our solar system.

HINTS: the semi-major axis of an ellipse is one-half of the maximum distance across the ellipse. The eccentricity of an ellipse if a number between 0 and 1 that measures how far the ellipse is from being circular (a circle has an eccentricity of 0, a parabola has an eccentricity of 1, and a hyperbola has an eccentricity greater than 1). In order to solve the assignment you will need to know something about Kepler's 3 Laws of planetary motion; a search with Google revealed the following URL: http://www.ac.wwu.edu/~vawter/PhysicsNet/Topics/Gravity/KeplersLaws.html which is one of many on the net that will get you started.

Solution.


Week 10 assignment - due 10am Friday 4 June 2004

Write a function to return the real roots of the quadratic function ax^2 + bx + c = 0. Use double precision floating-point numbers throughout.

Your function should do something appropriate if there are no real roots or only one. Please include documentation within the function to describe the action taken.

Your function should minimize round-off errors by using the formula for the roots given in lectures (i.e., q = -0.5(b+sgn(b)sqrt(b^2-4ac)); x1=q/a; x2=c/q; where sgn(b) is +1 if b is >=0, else -1).

Test your function using the following quadratics, printing the results with the full precision of the double result:

   x^2 + 123456789x - 0.000000123456789 = 0
x^2 + 2x + 1 = 0
x^2 + 2.00000000001x + 1 = 0
x^2 + 1.99999999999x + 1 = 0
2x + 1 = 0
1 = 0

Solution.


Week 11 assignment - due 5pm Monday 7 June 2004 (however, I won't mind if you are several days late...)

For 70% marks write a function to return the mean, average deviation, and standard deviation of an array of double numbers. Use the formula given in lectures that minimises rounding errors, i.e., var=(1/(n-1))(sum((xj - mean)^2) - (1/n)(sum(xj-mean))^2). A suitable function prototype might be:

   void stats(double *data, int n, double *mean, double *avDev, double *stDev)

You should use "assert" to do sanity-checks on the arguments (e.g., n = -1 is obviously not valid).

For the remaining 30% marks add "sigma-clipping" to your function, i.e., have an additional argument, sclip, to the function that removes any data from the calculation that is more than sclip standard deviations away from the first estimation of the mean.

Show the results from your functions when presented with the following data:

10000000000001.23
10000000000001.27
10000000000001.02
10000000000001.16
10000000000001.19
10000000000002.98

If you write the sigma-clipping function, give the results for sclip of 1.5.

Solution.


Week 12 assignment - due 5pm Wednesday 16 June 2004 (and this is a hard deadline since I am leaving for overseas)

Write a function to return the median of an array of double-precision numbers. Test your function using an array of random numbers. For more marks, return the 5 and 95% points as well (the 5% point is the data value which is greater than 5% of the distribution).