Jan. 19, 2012 --- Class 4---Error Analysis for the Euler Method, Shell Scripts
Activities:
A simple integral first (CSM, Secs. 2.2-2.5)
In ~sg/chap2 you will find a file euler.c that you can use
to do a numerical integral. It might be set up with the
function 2*x or 3*x^2.
To create an executable either type:
make euler.c
or
cc -o euler euler.c
We spent some time going over the code in euler.c. If you are not a
C programmer, you might not understand all of the intracacies,
but you should be able to follow the flow of the algorithm.
We looked at two step sizes 0.1 and 0.2. The numerical output
is more accurate for smaller step size.
We have several objectives:
1) Make a graph of the result as a function of x.
2) Try different step sizes and see that the answer varies, but
converges as the step size decreases.
3) Create (understand) the script ~sg/chap2/ans_vs_step.csh
4) Plot the result as a function of step size.
We ran the program for different steps sizes and found that
the final answer (and all intermediate values) vary with
step size. However, the value converges as the step size decreases.
We were easily able to accomplish goals (1) and (2). Note that
to make a graph with the results from two separate runs, it is useful
to have a "blank label", that is, two double quotes at the end of the
data for the first line. Otherwise, there is a straight line connecting
the end of the first plot with the beginning of the second plot. This
is because axis by default just connects successive points on the input
file.
cat out_.[12] |axis |plot -Tx
In the last last class I described how to set up an alias in your .cshrc
file to save some typing:
alias xplot 'plot -T X'
In the future, I will use this xplot alias.
We would like to automate this procedure so that we can easily
plot the final result as we vary the step size.
The shell script ~sg/chap2/ans_vs_step.csh which solves
this problem. Here is ans_vs_step.csh with extra comments:
#!/bin/csh # anything after the hash mark or sharp is a comment
# the first comment is special in that it tells the shell
# to use /bin/csh to interpret or run the commands in this file
# such a comment starts with #!
# the word foreach starts a loop. The loop ends with the "end" line
foreach step ( 0.5 0.2 .1 .05 .02 .01 .005 )
echo -n $step " " # -n tells echo not to end with a new line
euler $step |tail -1|awk '{print $2}'
end
The script above uses the tail command to look at the last
line of output. There is also a head command to look at the
beginning of a file or standard input. head -9 will look at the
first 9 lines. head +3 will look at all of the file starting
with line 3. Consult the man pages for head and tail
to learn more.
We plotted the result as a function of step size. It is
clear that as the step size approaches zero we approach the
analytic answer. In our next class, we will examine how we
approach the correct answer.