September 4, 2007 --- 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. Euler method The Euler method is based on a very simple approximation. You know that dy/dx = lim_dx->0 (y[x+dx]-y[x])/dx. In the Euler method, we just don't take the limit as dx->0, we use a small value of dx. In this case, if dy/dx = f[x], (y[x+dx]-y[x])/dx = f[x] (approximately) and so, y[x+dx] = y[x] + dx * f[x] In this way we can update y to successive values of x by updating dx at each step.