Computational Methods Junior Honours : Checkpoint 1
The purpose of this
checkpoint is to refresh you on the mechanics of editing, compiling and running
java code, which you learned in
Physics
2 Scientific Programming
You should refresh
your memory of with the Physics 2
Scientific Programming course and associated
documents.
A list of the most
needed Unix commands is here
Particular attention should be payed to
· Loops
· Methods
· Objects
Some useful Java documentation (increasingly advanced).
The checkpoint will also teach you how to
write numerical data into text files, and display them as presentation quality graphs
using the popular xmgrace
tool.
Example: CosinePlotter class
- Make a
directory for this course, we'd suggest
- mkdir
CompMeth
- cd
CompMeth
- We
recommend that you use a single directory for all your java code in this
course.
- You
should keep copies of your plots and datafiles in a subdirectory for each
checkpoint
- Download
java files CosinePlotter.java
- Read
it using the "emacs" editor
CosinePlotter.java
defines a class that outputs the values of cos(x) for one period.
The cosine is evaluated in a method that is accessed by a
method (aka function) call.
- Compile
it using the following
javac CosinePlotter.java
java CosinePlotter
- Understand how the output is generated.
Your task:
- Create
a file called HarmonicPlotter.java in
a similar style to CosinePlotter.java. Write a class HarmonicPlotter to
- PrintWriter
out = new PrintWriter( new FileWriter("harmonic.dat"));
- Here
we declare a variable out, of type PrintWriter.
- PrintWriter
is a class defined in java.io, which is imported at the top of the program
- out
is assigned with a new PrintWriter value.
- We
could have called “out” anything we want – variable names are the programmers
choice
- The
PrintWriter constructor method is called with a new FileWriter class.
- The
FileWriter constructor is
called to make it write a file called “harmonic.dat”
- NOTE
– once classes are written they can then be used as a type of variable.
- You
can now write to this "PrintWriter" the same way you print to
the screen
- out.println(A+"
"+B);
- System.out
is related to PrintWriter (actually it is a member variable of type
PrintStream in class System)
- Remember
to close the file using out.close() or the last data written may not
appear
- View
the file using the graphing tool xmgrace
- Modify
the function computed
- The
function printed now should be the sum sin x + 1/3 sin 3x + 1/5
sin 5x + 1/7 sin 7x
- Use
a for loop to add together the terms (i.e. the order).
- Remember
that a for loop consists of
- for(
initial-assignment ; loop-end-check ; variable-update ) { loop-body }
- e.g.
for ( int i=0;i<10;i++){
System.out.println(“i
= “+i);
}
- Write
this in a style you can change easily to compute 100 , or 1000
terms.
- Math.sin(n*x)
is likely required
- Use
xmgrace to label the axes (menu plot/axis properties), set the
symbols (double click on the lines) and change colors
Save the file as a postscript file
(harmonic.ps) using the print menu and print it out.
o Play
with the zoom (magnifying glass) and autoscale (AS) buttons on the left hand
side
o Try
playing with logarithmic scales, and change the plot title.
- Produce
a plots containing only the first one, two, three and four terms of the
sum by inserting an outer loop over the order.
- Printing
a spare blank line between each order will enable xmgrace to plot these
overlayed in different colours
· Can
you tell what is happening?
- This
is the lowest terms in the fourier series for a square wave
- As
more terms are added it gets more accurate and therefore more square-like
- You
will cover Fourier series later this term in Physical Mathematics
- For
the purposes of this Checkpoint it is sufficient to see this as
practical proof that an arbitrary function can be very well described by
the right set of sines and cosines.
- This
should provide you with a concrete experience when you cover Fourier
transforms in detail.
-
· Advanced:
- Compute
by hand the fourier coefficents for a step function centred at the
origin.