January 21, 2016 --- Class 4---Error Analysis for the Euler Method, Shell Scripts
Activities:
A simple differential equation first
In ~sg/chap2 you will find a file euler.c that you can use
to integrate the differential equation
dy/dx = f(x)
where f(x)=2x. There is another code euler_3xsq.c to solve
the case f(x)=3x^2. Both codes use the Euler algorithm, i.e.,
y( x + dx) = y(x) + dx * f(x)
We use the boundary condition y(1)=1 and want to find y(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
or ~sg/chap2/ans_vs_step.bash.
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
Previously, I described how to set up an alias in the .alias
file to save some typing. You can also put such lines directly
in .cshrc or .tcshrc (depending on which you use):
alias xplot 'plot -T X'
or
alias xplot='plot -T X'
The former for tcsh and the latter for bash.
In the future, I will use an xplot alias.
We used awk to help us create a blank label. Then, we made
a shell script compare_two.csh to allow us to plot y(x) for
two different integration step sizes.
The script compare_two.csh looks like this:
#!/bin/csh
set step1=$1
set step2=$2
(euler $step1; euler $step2) | awk '{print}/^2/{print $0,"\"\""}'|axis -lx x -ly y -lt "comparison of $step1 and $step2" -lty 1.05 h .9 w .9|xplot
In bash syntax, the first three lines are
#!/bin/bash
step1=$1
step2=$2
It makes use of the ( ) to combine the output of the two runs
of the euler command. That output is piped to awk so that
a blank label can be added when a line begins with the character
2. (This is very specfic to this problem in which the last
line of output from each run of euler starts the value of x,
which is 2.) Note the use of the backslashes to escape the
usual significance of the double quotes. The output is then
piped to axis and we add some labels and a title. Note that
the title depends on the shell variables step1 and step2.
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.
This script is very similar in bash syntax. See ~sg/chap2/ans_vs_step.bash:
#!/bin/bash # anything after the hash mark or sharp is a comment
# the first comment is special in that it tells the shell
# to use /bin/bash to interpret or run the commands in this file
# such a comment starts with #!
# the word "for" starts a loop. The loop ends with the "end" line
for step in 0.5 0.2 .1 .05 .02 .01 .005 ;
do
echo -n $step " " # -n tells echo not to end with a new line
euler_3xsq $step |tail -1|awk '{print $2}'
done
As you can see, the syntax of bash's for loop is a little different from
the tcsh foreach loop.
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.