September 6, 2005 --- Class 3 --- AWK, Intro to Euler Method Activities: AWK AWK is a useful scripting language that can help you to manipulate information in a file. "The AWK Programming Language" is by Alfred Aho, Brian Kernighan and Peter Weinberger. You should have already read the tutorial I asked you to print and read in the first class. You can also find useful information at www.oreilly.com for a variety of topic regarding computers and computing. PERL is another useful scripting language which is probably more popular than AWK now. However, AWK is so simple that it is very convenient for class. You may eventually decide to learn PERL. If you have \delta t values in a file and you want to have \delta t and 0.5*(\delta t)^2 on each line, it is easy to do this with an awk command. Assume the \delta t are in a file with one value per line and the file is called col. awk '{print $1,0.5*$1*$1}' col will result in two columns with the desired numbers. The output may be redirected to a file if desired. awk may be used to create a list of numbers, as well awk 'END{for(i=1;i<$1; ++i) print 0.01*i}' can be used and you may interactively type a number and then on the next line ^D (which stands for pressing d, while holding down the control key). AWK is a very versatile pattern matching utility that can be used to manipulate files. Each AWK command consists of a pattern followed by an action. If the pattern is matched, the action is performed. The action is placed inside curly braces. A very simple awk command is: awk '{print}' file_name The single quotes protect the curly braces from being interpreted by the shell as special characters. In this AWK command, there is no pattern to match, so the print command is always invoked. awk '/1/ {print}' file_name has a pattern that matches any line containing the character 1. Only such lines will be printed. awk '/^1/ {print}' file_name In this example, "^" is a special character to indicate the beginning of a line. Thus, only lines that begin with "1" will be printed. awk '{print $2}' file_name will result in the second word of each line being printed. (Words are separated by white space, i.e., spaces or tabs.) awk '{print $2,$1}' file_name prints the second word followed by the first word. You might like to try the above examples with the file cols that you are welcome to copy from my home directory. Introduction to the Euler Method Tea or coffee cooling problem (CSM, Sec. 2.1) This is the problem discussed in chapter 2. Should you add the milk to the tea as soon as it is poured, or just before you want to drink it in order to cool it as quickly as possible? To solve this problem, we are going to learn to integrate a differential equation numerically via the Euler method. 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. I 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. It would be useful if you read Chapter 2 before the next class. You don't have to solve the problems! (Yet) Some will be assigned for homework. 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). We saw that to make a graph with the results from two separate runs, it was 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 the running so that we can easily plot the final result as we vary the step size. In the next class, we will examine the shell script ~sg/chap2/ans_vs_step.csh which solves this problem.