lecture

© All Rights Reserved

0 views

lecture

© All Rights Reserved

- Dynamic Simulation of Off-road Tracked Vehicles
- ContextCapture Camera Model
- Final Presentation TOTAL
- Restriction Orifice RO Flow Control Instrument
- M911 LES31
- Morris Levy Roulette Buddy Holly
- foam chamber.pdf
- flow_def2
- Forward Kinematics
- 16 Foamchambermodel f
- 2010 Mathematical Methods (CAS) Exam 2
- AREA CLASSIFICATION for Landfill Gas Extraction Utilisation and Combuction
- Fractionation Column Calc
- Foam Chamber Model F
- midterm_04.pdf
- General Hydraulic Cal
- UNIT 5 Coordinate Systems
- Spray Chapter2
- Momentum Transfer Reviewer
- FMHM EEE

You are on page 1of 10

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

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

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

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.

? NumericQ

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

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

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)

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

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

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

Comments

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

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

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

- Dynamic Simulation of Off-road Tracked VehiclesUploaded byNattapon Maitree
- ContextCapture Camera ModelUploaded byGugan
- Final Presentation TOTALUploaded byTahrizi Andana
- Restriction Orifice RO Flow Control InstrumentUploaded bymsr22
- M911 LES31Uploaded bygemgirl6686
- Morris Levy Roulette Buddy HollyUploaded bycover02plain
- foam chamber.pdfUploaded byAinur Rohman
- flow_def2Uploaded bySanjay Soni
- Forward KinematicsUploaded byBabak GH
- 16 Foamchambermodel fUploaded byMihir Jha
- 2010 Mathematical Methods (CAS) Exam 2Uploaded bypinkangel2868_142411
- AREA CLASSIFICATION for Landfill Gas Extraction Utilisation and CombuctionUploaded byanac_math
- Fractionation Column CalcUploaded byzamijaka
- Foam Chamber Model FUploaded byMaulia Sabrina
- midterm_04.pdfUploaded byOlga Joy Labajo Gerasta
- General Hydraulic CalUploaded byFerdian Ali Rahman
- UNIT 5 Coordinate SystemsUploaded byAgnihothra Sarma Oruganti
- Spray Chapter2Uploaded byjulioagricola35
- Momentum Transfer ReviewerUploaded byNeil Dominic Dasallas Careo
- FMHM EEEUploaded bybalabooks
- wiUploaded byLuck
- P3 SCHEMEUploaded byPavitra Paramesuvaran
- AutoCAD Tutorial _ Using Co-ordinates _ CADTutor.pdfUploaded byEnteng D Invinceble
- fynd it is useful kkkkkk.pdfUploaded byKrushna Rama
- lab5reportUploaded byapi-405394814
- CE-2202Uploaded bySujon Ahmmed
- Engg Drawing Question PaperUploaded bySuvroAdxChattopadhyay
- HW02 Ch03 Equiv ForcesUploaded byTulong Zhu
- METERRUN Technical Guide Danieenior Orifice Fitting en 44048 31Uploaded byestudiemos
- Flow through a venturi meter & Orifice rotameter.pdfUploaded byDigvijay Singh

- ECM6Lecture14cVietnam_2014.npdfUploaded byduyvu
- ECM6Lecture14bVietnam_2014Uploaded byduyvu
- ECM6Lecture14aVietnam_2014Uploaded byduyvu
- ECM6Lecture11dVietnam_2014Uploaded byduyvu
- ECM6Lecture11cVietnam_2014Uploaded byduyvu
- ECM6Lecture11bVietnam_2014Uploaded byduyvu
- ECM6Lecture11aVietnam_2014Uploaded byduyvu
- ECM6Lecture8Vietnam_2014Uploaded byduyvu
- ECM6Lecture6Vietnam_2014Uploaded byduyvu
- ECM6Lecture5Vietnam_2014Uploaded byduyvu
- ECM6Lecture4Vitenam_2014Uploaded byduyvu
- ECM6Lecture2Vietnam_2014Uploaded byduyvu
- ECM6Lecture3Vietnam_2014Uploaded byduyvu

- 111 Mathematics Quiz Questions Answers.docxUploaded byleizel
- 162 - San Luis v RojasUploaded byHannah Plopinio
- tyleia sharpsUploaded byapi-308998487
- Genetics Harry Potter PptUploaded byGuille Paz
- Advanced Placement and International CredentialsUploaded bytrellon_devteam
- 7) Design of One-Way SlabsUploaded byVanessa Rosal Paler
- financial modeling examplesUploaded byapi-242403196
- Problems of Corporate Governance in USAUploaded byNikhil Mohanan
- Lesson Project 7th GradeUploaded byfrom_dusk
- Advanced - Vocabulary, Idioms and MetaphorsUploaded byjustttka
- Bias v. Advantage International, Inc (1990) Nonmoving Party Must Do More Than Show That There is Metaphysical Doubt but Must Provide Specific Facts to Create Genuine Issue During SJUploaded byJohnson Don
- mfdsUploaded byferasallan
- Actilyse_PIUploaded bymaster
- Conducting Semi-structured InterviewsUploaded byOxfam
- OU SYLLABUS BA 2YR SEM4 - Economics.pdfUploaded byRajiv Vyas
- 10085 Coal-RDF Co-Firing Final ReportUploaded bymjbotelho
- 2020 Tsa Catalogue DigitalUploaded byAnonymous 41I2MkV3Z
- Direct Object Pronouns in SpanishUploaded byxShiroi
- C. Gerardo Rojas v. Tai Shan Kang, Ye Mai Kang, and Hwa Lin Tai, 101 F.3d 108, 2d Cir. (1996)Uploaded byScribd Government Docs
- Thrown Under The Bus InterviewUploaded byDavid Welch
- Lecture Plan Tri 2 1415 - BAE1044 -English for Business Communication 14Oct2014Uploaded bycharlie simo
- Conduct Competency Assessment 2012Uploaded byArthur F Francisco
- Classification of Assurance Engagement.docxUploaded byJennybabe Peta
- AdjunctsUploaded byManh Trang
- Digital Dc Motor Speed Control Using Hall Effect PhenomenonUploaded byijteee
- Ulexite Nacab5o6(Oh)6•5h2oUploaded bysupertierra
- Comparison of Humanistic and Existential PsychologyUploaded byCarol Ann
- ccgps ela grade8 standardsUploaded byapi-247470989
- Article IVUploaded byPaul Reymon Torregosa Macanip
- Language and LinguisticsUploaded byDeden Syefrudin

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.