You are on page 1of 10

# ECM6 Computational Methods :

Slide 1 of 7

Lecture 9
Manipulating Data Using Rules
Brian G. Higgins
Department of Chemical Engineering & Materials Science
University of California, Davis
April 2014, Hanoi, Vietnam

ECM6Lecture9Vietnam_2014.nb

## Topics for Lecture 9

In this lecture we give a brief over view of the following topics

## 1. Overeview Rule based programming

2. Draining Tank Experiment
Evaluate the following function to avoid function clashes.
Remove@p, b, x, f, myList, myTestQ, myList3, myExp2, data,
rawdata, tankdata, tankdata2, TankArea, QhData, LogQLoghData, hD;

ECM6Lecture9Vietnam_2014.nb

## Mathematica Preliminaries: Rule-Based Programming

In rule-based programming a set of rules are used to transform functions and expressions.
Many operations in mathematics are based on a set of transformation rules, e.g. differentiation, integral
tables, limits etc.
In Mathematica transformations based on rules is accomplished with the use of patterns, and Mathematicas ability to pattern-match expressions.

Patterns
The simplest pattern in Mathematica is the function Blank (also represented by the underscore _).
The Blank is a pattern that represents (matches) any expression.
The pattern _h represents any expression with the head h.
Often replacement rules use a pattern on the lhs.

Example 1
Consider the following example. Suppose we have the list
myList = 82.3, 3, 4.5, b, 6.7, 67, f@xD<;

We would like to replace all real variables in this list with the symbol p.
This suggest the use of a replacement rule applied to the list myList, i.e., myList . lhs rhs.
For the lhs we construct a pattern to represent the elements of our list that we wish to transform.
Since we are seeking all elements that have the head Real we define our pattern to match real variables
Thus _Real will represent any variable with the head Real.
myList . _Real p

Example 2:
Suppose we wanted to transform all real variables by taking the cube root.
Our rule in this case needs to be a little more specific. We can accomplish this by giving the pattern on
the lhs a name so that we can refer to it on the rhs
myList . x_Real x13

In the execution of this rule anything that matches the pattern on the lhs, called "x", is transformed
according to the expression on the rhs. If we use x_ as our pattern, then this will match any expression
and so x_ x2 will square all elements of our list

ECM6Lecture9Vietnam_2014.nb

In the execution of this rule anything that matches the pattern on the lhs, called "x", is transformed
according to the expression on the rhs. If we use x_ as our pattern, then this will match any expression
and so x_ x2 will square all elements of our list

Example 3:
Thus if we use the named pattern x_f, we will match up all elements in the list that have the head f. In
this case it is just f[x]. So if we want to square the function , the following rule will achieve thedesired
result:
myList . y_f y2
92.3, 3, 4.5, b, 6.7, 67, f@xD2 =

Example 4:
We can also use a rule that has a condition associated with it.
One can also use the function PatternTest ( shorthand ?}
? PatternTest
p ? test is a pattern object that stands for any
expression which matches p, and on which the application of test gives True.

## We can illustrate its use with the function NumericQ

? NumericQ
NumericQ@exprD gives True if expr is a numeric quantity, and False otherwise.

## Suppose we want to square all the numeric numbers in our list

myList . x_ ? NumericQ x2

Our pattern test can also be expressed as a pure function as shown here
myList . x_ ? HNumericQ@D &L x2

Note the importance of the parenthesis: the parentheses MUST enclose the pure function as well as
ampersand (&) .

ECM6Lecture9Vietnam_2014.nb

## Tank Draining Experiment

Problem Statement
When water drains from a tank, as shown in Figure 1, the volumetric flow rate can usually be expressed
as
Q = CD A0

2gh

(1)

Here h is the instantaneous height of the liquid in the tank, g is the gravitational constant, A0 is the cross
sectional area of the orifice and CD is called the discharged coefficient.

In this experiment, the change in depth of the water in the draining tank is measured as a function of
time and the discharge coefficient CD is determined by making use of the draining data. In ECH51 you
learnt how to analyze the draining tank from first principles and derive an expression for the change in
height h with time. If we select a moving control volume that encompasses the liquid in the tank at any
given time the conservation of mass gives

r V = r v n A
V HtL

(2)

If the cross sectional area of the tank is AT then the volume of liquid in the tank is given by AT h. Thus
(2) becomes
AT

h
t

=Q

(3)

## where Q is the volumetric flow rate through the orifice.

Experimental Data
rawdata =
First@Import@"TeachingBGHTeaching1ECM6ECM6ClassNotesVersion7TankData.xls"DD

The data is imported into Mathematica as a list. We can remove the heading information by using the
Drop command

ECM6Lecture9Vietnam_2014.nb

The data is imported into Mathematica as a list. We can remove the heading information by using the
Drop command
The data is imported into Mathematica as a list. We can remove the heading information by using the
Drop command
tankdata = Drop@rawdata, 1D

It is often convenient to organize this data in the form of a table as shown below
TableForm@tankdata,
TableHeadings 8None, 8"HeightHinchesL", "TimeHsL"<<, TableAlignments -> CenterD

For the analysis, we need to convert the height measurements from inches to meters. Thus each
height measurement must be multiplied by a conversion factor. In this case the conversion factor is
25.4/1000. We use the following replacement rule to make the conversion:
8h_, t_< -> 8h 25.4 1000, t<
where h_ and t_ in the above replacement rule are placeholders for the {height, time} ordered pair in
your table of data. The conversion is done below and the converted data is stored in the new variable
rawdata2. Shown below is a table of the converted data
Remove@hD;
tankdata2 = tankdata . 8h_, t_< 8h 25.4 1000, t<; TableForm@tankdata2,
TableHeadings 8None, 8"HeightHmL", "timeHsL"<<, TableAlignments -> CenterD

ECM6Lecture9Vietnam_2014.nb

## Tank Draining Experiment: Data Analysis

From the lab notes we are told that you will need to plot Ln(Q) versus Ln(h), where Q is the estimated
flow rate through the orifice in m3 s. We will use the raw data to estimated Q. This is done using the
formula:
Q = AT Hhi+1 - hi L Hti - ti+1 L
where AT is the cross-sectional area of the tank Im2 M and hi is the measured liquid height at time ti .
To make this calculation you can partition your raw data into sublists with an offset of 1. Thus the
structure of your new data would look like
888h1 , t1 <, 8h2 , t2 <<, 88h2 , t2 <, 8h3 , t3 <<, 88h3 , t2 <, 8h4 , t4 <<, <

This operation can be readily done using The partition function with offset of 1
Partition@tankdata2, 2, 1D

When we have the data in this form we can apply a replacement rule to each sub-list
88hi , ti <, 8hi+1 , ti+1 <<that accomplishes the data transformation:
88h1_, t1_<, 8h2_, t2_<< 8Log@TankArea Hh2 - h1L Ht1 - t2LD, Log@h1D<

where h1_ , h2_, t1_, t2_ etc., are placeholders for height and time. The code that accomplishes
this transformation is given below:
TankArea = p

H0.465L2

; LogQLoghData = Partition@tankdata2, 2, 1D .
4
88h1_, t1_<, 8h2_, t2_<< 8Log@TankArea Hh2 - h1L Ht1 - t2LD, Log@h1D<

ECM6Lecture9Vietnam_2014.nb

## Tank Draining Experiment: Plotting the Data

The list of ordered pairs 8Ln HQi L, Ln Hhi L< can be plotted using the ListPlot function. Since we
want Ln Hhi Lon the abscissa and Log HQi Lon the ordinate of our plot we need to interchange the
ordered pair. Again this is done with a simple replacement rule
ListPlot@LogQLoghData .8q1_, h1_< 8h1, q1<,
Frame True, PlotStyle 8PointSize@0.02`D, Red<,
FrameLabel 8"LnHhL", "lnHQL"<, RotateLabel False, AspectRatio 1D

In the above plot we took the natural log of Q and h and then plotted the result on a graph with linear
axes (note the tick marks have equal spacing).
Alternatively we could plot Q versus h on a graph that has log axes. First we prepare our data as
ordered points of Qi and hi
QhData = Partition@tankdata2, 2, 1D .
88h1_, t1_<, 8h2_, t2_<< 8TankArea Hh2 - h1L Ht1 - t2L, h1<

Mathematica has function called LogLogListPlot that allows you to plot data as a log-log plot.
ListLogLogPlot@QhData .8q1_, h1_< 8h1, q1<,
Frame True, PlotStyle 8PointSize@0.02`D, RGBColor@0, 0, 1D<,
FrameLabel 8"hHmL", "QHm^3sL"<, RotateLabel False, AspectRatio 1D

There are some important differences to note in these two plots. In the first plot the tick marks on the
abscissa and ordinate are log quantities, while in the second plot the tick marks are the actual values of
our data. Clearly the second plot is more readable. However, as expected the two plots are identical in
form. It is just a question of how we read data from the plots. Note that is does not make sense to add
units to the captions on the axes in the first plot! Also please check Lecture 8 to see how to change the
tick marks so that your plot is equivalent to Mathematica's

Summary
In this notebook we have shown how you can use Mathematica to organize and manipulate your data
for plotting. We have made no reference to error analysis, or how many significant figures we should be
carrying through the analysis.
In our original data time was measured to within a tenth of a second, while height was measured in
inches. Based on these facts we can use the rules for how to determine the number of significant
figures we should report in the manipulated data. We will discuss error analysis of our data in a subsequent notebook.

ECM6Lecture9Vietnam_2014.nb

## Exploring Patterns from a Roulette Wheel

Background
In this example, we consider a roulette wheel with numbers (0->36).
We are interested in determining how many spins would it take typically to obtain two zeros in two
consequence spins.
We can use Mathematica 's random number generator Random@D to generate integers between 0 and
36. The following function generates a random integer between 0 and 36
Random@Integer, 80, 36<D

Since we are seeking an expectation value, we would need to create many realizations. One way to do
this is to generate a long sequence of numbers (spins) and then search through the sequence until we
found the first occurrence of "0,0".
The location of this occurrence, is the desired number of spins for that realization. Our goal then is to
define a pattern that will locate the position of the first "0,0".

Mathematica Code
Generating Random Spins of Rowlette Wheel
To see how we construct such a rule, let us generate a set of random spins from a roulette wheel, and
store the sequence in the list called data
data = RandomInteger@80, 36<, 100D

## Analyzing the Random Spins

Suppose now we want to determine the location where we first have the integer i1 followed by some
specified number, say i2 .
Step 1:
The pattern we want to match is any sequence of numbers (which we will call our pre-sequence) followed by i1 , i2 and then followed by any other sequence (our post-sequence).
The pattern: 8x__ , i1 , i2 , __<will match a sequence of one or more integers we use the BlankSequence (__)
Step 2
The outcome of the rule is to replace the sequence preceding the "i1 , i2 " with its Length+1. Since we
need to evaluate the length of our pre-sequence, we name the pattern for the pre-sequence x
Then on the rhs we determine the length of the pre-sequence.The delayed assignment is needed to
ensure that the length of the sequence is determined after x is calculated.

10

ECM6Lecture9Vietnam_2014.nb

Then on the rhs we determine the length of the pre-sequence.The delayed assignment is needed to
ensure that the length of the sequence is determined after x is calculated.
data . 8x__, 12, 31, __< :> Length@8x<D + 1

Consider the sequence 36,15. This sequence of numbers occurs twice in data. The first occurrence is
at location 57 and the second occurrence is at location 76. When we apply the pattern, only the first
pattern is caught as we have not specified anything regarding the structure of the post-sequence.
data . 8x__, 36, 15, __< :> Length@8x<D + 1

Here is the location of the "0,0" in a hypothetical set of spins of a roulette wheel:
RandomInteger@80, 36<, 10 000D .8x__, 0, 0, __< Length@8x<D + 1