2 intro igor - cirescires.colorado.edu/jimenez/chem-5181/lect/2_intro_igor.pdf · igor-used terms...
TRANSCRIPT
8/30/2011
1
Intro to Igor Pro: Beyond “Getting Started”y g
(Adapted from Ingrid Ulbrich 2009 lecture)
CHEM-5181Mass Spectrometry & Chromatography
Prof. Jose L. Jimenez
1Last update: Aug 2011
A Note about Jose’s Style (from Ingrid)From a past syllabus:
“If you don't go through the effort of understanding and solving every problemunderstanding and solving every problem yourself (after discussing it with others if you want) you'll find yourself at a large disadvantage in the exams where you have to solve new problems very quickly.”
2
8/30/2011
2
Goals for First 2 Lectures• Become familiar with Igor Pro software
– Storing data– Doing calculations– Making graphs– Writing simple functions
• Homeworks to work on these tasks– Start with examples that should already be familiar
“M d l” th i l t MS ( l t i t )– “Model” the simplest MS (electric sector)
• Very important to put in the effort, or you will be lost in later homeworks!
3
Why do I have to use Igor instead of software I already know?
• Powerful analysis package, can be very useful later– Superior to spreadsheets! Especially good for
• Complex data analysis: reproducible and traceable calculations• Complex data analysis: reproducible and traceable calculations• Graphs with more options that are easier to manipulate
– Much cheaper than Matlab, similar capabilities– Excellent email support, good user mailing list
• But always try hard first before emailing them (2 hr rule)
• Used almost exclusively in Tolbert, Volkamer, Jimenez, several other groups for data analysis
• Good tool for learning programming– Menus, command line, and programs to do the same thing
• Consistent tool for the class, makes it possible to discuss code in class, and for Jose and others to help 4
8/30/2011
3
Why should I learn programming? (1)• Functions create a record of the steps used in a
calculationHelp find errors in multistep calculationsHelp find errors in multistep calculations
• Functions make it easy to repeat the same calculation with different data
Standardized analysis
5
Why should I learn programming? (2)• Functions are easier to share with others than in a
spreadsheetRepeatable calculations for your colleaguesRepeatable calculations for your colleagues
• Slice and dice big datasets– Our Aerosol Mass Spectrometer saves 60,000 mass
spectral values every 2.5 minutes• 106 values per hour 3x107 per day 2x109 per month• 106 values per hour 3x107 per day 2x109 per month• And that’s on “slow mode”, now we often save every 1s or
100 ms!Way too much data to look at everything by hand!
6
8/30/2011
4
Did you do the Igor “Getting Started” Tour?
a) Yes, all of it.
b) Yes, most of it.
c) Yes, some of it.
d) No, or not very much of it.
e) I need a coffee before I understand this question
7
Igor File Vocabulary• Experiment: an Igor file where you store data
and graphs (.pxp) Let’s open a new experiment.• DataFolder: subdirectory in an experiment• DataFolder: subdirectory in an experiment
– Red Arrow in Data Browser shows current DataFolder• Notebook: “file” in an experiment where you can
write notes, paste graphs– Windows New Notebook (Formatted Text)
• Procedures: functions written by you (or others) – Some exist only in the experiment they are in– Can be shared with friends/colleagues– “Local” procedure window (ctrl+m)
8
8/30/2011
5
More Igor File Vocabulary• Data Browser
– Can set Preferences to sort by “Name and Type”• Help Browser (F1)• Help Browser (F1)
– Look for help on functions– Can also get function help by right-clicking on a
function name, choosing “Help on functionName”– Most functions have Examples– Many functions have Related Functions
9
Data is usually stored in Waves• Wave: a vector (array) containing data
– Exists until you “kill” it (even if you’re not looking at it in a graph or table)it in a graph or table)
• Contrast to e.g. Excel, data only exists if it is on the sheet
– Numbers (single precision by default) or text• Every wave has some number of “points” (length)• Waves have inherent “x” values = point number
(0 1 2 )(0, 1, 2…)– When you display a wave, it’s plotted against the
internal x values (unless you tell it to plot vs. something else)
10
8/30/2011
6
Wave Names have Rules• Wave names
– Cannot include spaces, operators (+ - * /), special characterscharacters
• Can use underscore ( _ )– Cannot begin with numbers– Must be ≤ 32 characters– Are not case sensitive
C h h f i i bl– Cannot have the same name as functions, variables, Igor-used terms
11
Making Waves• Let’s make a really simple wave so that we can
practice some simple operationsMake/N=20 y valsMake/N 20 y_vals
• Now let’s look at the values in the wave in a table:
Igor function that creates a wave
Flag for makethat sets Number of points in wave
Name of wave
edit y_vals.id(special way to see the “x” and “data” values”)
12
8/30/2011
7
Giving values to y_vals• Now we can take advantage of the inherent “x”
values and make y_vals = f(x)
Y vals = x_Display y_vals
Fix the axes to have range -10, 40 for x and yAdd “zero lines” (Ticks and Grids tab)
Copy and Paste this plot in your Notebook
Change your line by changing the equation in the commandChange your line by changing the equation in the command line (e.g., y_vals = 2*x y_vals = 2*x + 3)
Copy and Paste a new plot in your Notebook
Note that the y_vals.d changes in the table, too!
13
Kill the table!• Check for the wave in the Data Browser
– Waves exist until you kill them, even if you’re not looking at the them.looking at the them.
– Very different from Excel, of course!– Important for homeworks, as you try various things,
remember to kill old LARGE waves so that your experiment doesn’t become huge
• Make table again if o ant to atch the data• Make table again if you want to watch the data change
14
8/30/2011
8
Can you make the line go into the x<0 range?
• Recall from “Getting Started” that you can change the inherent x-scaling
Data Change Wave ScalingData Change Wave Scaling
• The command for this change was printed in the History in the Command Window!– Useful for using this command in a function, since g ,
you can’t use the pulldown menu in a function.
15
Our Sample Function
Function Name Function Arguments
Function LineValues()make/O/N=20 y_vals = 0.1 * x + 1
endGotta end
the function
Stuff for the function to do
And let’s compile!And let’s run it (from the command line).Change the values. Does the line change?
16
8/30/2011
9
Our Sample Function, with Variables
Function LineValues2()i bl 0 1 b 1variable m = 0.1, b = 1
make/O/N=20 y_vals = m * x + bend
17
Our Sample Function, with Inputs
Function LineValues_input(m,b)i bl bvariable m, b
make/O/N=20 y_vals = m * x + bend
18
8/30/2011
10
Our Sample Function, Print Equation
Function LineValues(m,b)i bl bvariable m, b
make/O/N=20 y_vals = m * x + bprint “Equation is y=“, m, “*x + “, b
end
19
Our Sample Function, Print Equation Sometimes
Function LineValues(m, b, printFlag)variable m, b, printFlagmake/O/N=20 y_vals = m * x + bif(printFlag == 1)
print “Equation is y=“, m, “*x + “, bp q yendif
end
20
8/30/2011
11
Our Sample Function, with Wave Scaling
Function LineValues(m,b, x min, x max, printFlag)( , , _ , _ , p g)variable m, b, x_min, x_max, printFlagsetScale/I x x_min, x_max,"", y_valsmake/O/N=20 y_vals = m * x + bif(printFlag == 1)
print “Equation is y “ m “*x + “ bprint “Equation is y=“, m, “*x + “, bendif
end
21
Our Sample Function, with Outputs
Function LineValues_input(m,b,x)_ p ( )variable m, b, xreturn m * x + b
end
• More versatile function, independent of wave• Calculate the wave on the command line, e.g.
y_vals = LineValues_input(0.1, 1, x)22
8/30/2011
12
Other Examples
• When you make a lot of waves with the same x
800
600
Trav
elle
d (m
i)
car bike
waves with the same x data (e.g., time_hr), clearer to just use a separate x wave– Calculate the distance that
a car goes (50 mph) and that a bike goes (15 mph)
10 h
400
200
0
Dis
tanc
e T
151050time (hr)
display dist_car_mi, dist_bike_mi vs time_hr
Plotted vs. over 10 hours
• Plot ln(x), warn user if x_min < 0
same x wave
23
Check out Ingrid’s Igor Quick Reference Wiki
• http://cires.colorado.edu/jimenez-group/wiki/index.php/Igor_Quick_Reference
• Still a work in progress! Please make suggestions for improvement tosuggestions for improvement to <[email protected]>
24
8/30/2011
13
Intro to Igor Pro Part 2:Numerical Integration in Igor
Tossing a ball in the airor
What goes up must come down
CHEM-5181Mass Spectrometry & Chromatography
Fall 2011 25
Simple Case: Toss a Ball Straight Up in the Air
Clicker Q: When the ball is in the air, what is the force diagram for the ball?
A) B) C) D)
E) None of These
26
8/30/2011
14
Simple Case: Toss a Ball Straight Up in the Air
• What will the trajectory of the ball look like?
x vs. t y vs. t x vs. y
27
Let’s work to plot y vs. t1. Make a wave of time in seconds.
L t’ t t f tiLet’s start a function:Make/O/N=10 time_s // time in secondstime_s = x // for now 1s increments
28
8/30/2011
15
Simple Case: Toss a Ball Straight Up in the Air
Clicker Q: Acceleration is…
Acceleration is equal to…L ’ k CONSTANT b h f i
A) constant B) proportional to t C) proportional to t2
D) none of these E) I really need a coffee
Let’s make a CONSTANT above the functionNow we can make a wave for accel_m_s2
29
Simple Case: Toss a Ball Straight Up in the Air
Clicker Q: Velocity is…
A) constant B) proportional to t C) proportional to t2
D) none of these E) Please add milk and sugar
30
8/30/2011
16
Integrating Acceleration Velocity
We will use Euler’s Method: y(i) = y(i-1) + y’(i)*dt
y'
y(0)
y‘(0) = 0y’*dt
t
y‘(1) = -somethingy’*dt
∫ line = parabolay( )
y‘(2) = -something morey’*dt
31
Since we already “know” how to calculate the answer…
• Let’s make a table to calculate (check) what we expect to get for velocityexpect to get for velocity
Acceleration (m/s2) Time (s) Velocity (m/s)0123
32
8/30/2011
17
Since we already “know” how to calculate the answer…
• Let’s make a table to calculate (check) what we expect to get for velocity
N d i iti l diti !
Acceleration (m/s2) Time (s) Velocity (m/s)-9.8 0-9.8 1-9.8 2
Need an initial condition!Let’s just drop the ball. Then v0 = ?
-9.8 3
33
Since we already “know” how to calculate the answer…
• Let’s make a table to calculate (check) what we expect to get for velocity
Acceleration (m/s2) Time (s) Velocity (m/s)-9.8 0 0.0-9.8 1-9.8 2-9.8 39.
34
8/30/2011
18
Since we already “know” how to calculate the answer…
• Let’s make a table to calculate (check) what we expect to get for velocity
Acceleration (m/s2) Time (s) Velocity (m/s)-9.8 0 0.0-9.8 1 - 9.8-9.8 2 -19.6-9.8 3 -28.4
35
How do we tell the computerhow to do this calculation?
1. Use the initial condition to fill in the first element of the wave.
2. For every row in the rest of the wave, calculate vi = vi-1 + ai*dt
In the function we can do step 1:In the function we can do step 1:Create a variable v_0_m_s make vel_m_sset zeroth point of vel_m_s = v_0_m_s
36
8/30/2011
19
Writing the “for each row” part
Only happens when
Only happens at the end of the stuff inside the
“for” loop:
for(start counter; do it condition; update counter)stuff to do each time
entering the “for” loope.g. index = 0
Do stuff until this isn’t true anymoree.g. index < 100
pready to do the next onee.g. index = index + 1
ffusually depends on the counter
endfor
37
The “index” for our caseIgor Row Number
Acceleration (m/s2)
Time (s) Velocity (m/s)
0 -9.8 0 0.0
• Row 0 was special, and we’ve already handled that• Vel_m_s[0] = v_0_m_s
– So we want to start our integration with row 1• Make counter start at index = 1
1 -9.8 1 - 9.82 -9.8 2 -19.63 -9.8 3 -28.4
Make counter start at index 1• We want to operate on every row
• Increase our counter by index = index + 1• We want to keep going while we have rows
• Stop Condition is index < 10
38
8/30/2011
20
Writing the “for each row” part
Only happens when entering the “for” loop Do stuff until this
isn’t true anymore
Only happens at the end of the stuff inside the
“for” loop: ready to do the next onee g index = index + 1
for(index = 1; index < 10; index = index + 1)stuff to do each timeusually depends on the counter
isn t true anymoree.g. index < 100
e.g. index = index + 1
usually depends on the counterendfor
Now, what’s the stuff to do each time?vi = vi-1 + ai*dt
39
Vel m s accel m s2
A closer look at our equation
Vel_m_s accel_m_s2
can get dt from time_s
v a and t already exist in our function!
vi = vi-1 + ai*dt
v, a, and t already exist in our function!
So how can we write the equation?
40
8/30/2011
21
Now Run with Debugger• Procedure Enable Debugger
• In the new column at the left, click in a row in the functionfunction.– This will stop execution of the function and start the debugger.
• Above the top center box in the debugger, chose “Local and Global Variables”
• The yellow arrow in the function shows the next line to be• The yellow arrow in the function shows the next line to be executed.– Proceed 1 line at a time by pressing enter or using the yellow
arrow button at the top of the debugger.– Proceed to next stop (or end) by pressing the green arrow
button at the top of the debugger41
Simple Case: Toss a Ball Straight Up in the Air
Clicker Q: Position is…
A) constant B) proportional to t C) proportional to t2
D) none of these E) I need a refill, please
42
8/30/2011
22
How can we implement the position calculation?
Clicker Q:A. Position must be calculated in a different
f ifunction.
B. Position can be calculated in a separate for loop before calculating the velocity.
C. Position can be calculated in a separate for loop ft th l f l l ti l itafter the loop for calculating velocity.
D. Velocity and position can be calculated in the same for loop.
43
Let’s make a plot!
• Make your axes like thisS i li t
-9.80
atio
n (m
/s2 )
– Spacing, alignment– Axis labels– Zero line for velocity,
position
• Markers are nice but -100
0
n (m
)
-80
-60
-40
-20
0
Vel
ocity
(m/s
)A
ccel
era
Markers are nice, but not necessary -300
-200
Pos
ition
86420
Time (s)
44
8/30/2011
23
Next Steps• Change v0 and s0 and see changes
• Generalize the function with input variablesGeneralize the function with input variables
• Assume that – s0 > 0– the ground = 0
Af h b ll hi h d i h f ll– After the ball hits the ground it stays there for all remaining time
45
Advanced further steps• Generalize function to allow variable number of
points in waves (hint: look up help for numpntscommand in igor)command in igor)
• Allow variable time steps
46