Document 14539472

advertisement
Physics 2660: Fundamentals of Scientific Computing Lecture 13 Notes
•  Only 2 weeks left in the semester, 2 lectures left including this one:
–  26 April – TODAY!
–  3 May
•  Labs: one lab left
–  28 April – THURSDAY!
•  Upcoming homeworks: –  HW12 due Monday 2 May at midnight
–  HW13 due Wednesday 4 May at midnight
–  HW14 due Wednesday 4 May at midnight
2
Notes
•  Final exam is coming:
–  Take-­‐‑home projects
•  3 or 4 problems
–  Like a more involved, longer multipart homework assignment
–  Assigned last week of semester on Tuesday 3 May
–  Due Thursday May 12:
•  electronic copies by 9:00am •  hard-­‐‑copies must be submiUed Thursday 12 May between 08:00-­‐‑10:00 in room 022-­‐‑C, our computer lab
Due Thursday May 12,
electronically by 9:00am
and hard copies between
08:00 and 10:00 in 022-C
3
Notes
•  Office hours reminder:
–  My office hours are in Room 022-­‐‑C (our computer lab) from 3:30-­‐‑5pm on Tuesdays or by appointment
•  NOTE: Today they are CANCELED due to an unfortunate conflict with a faculty meeting L
•  If you want to meet with me at some other time this week SEND ME EMAIL and we’ll work something out!
–  TA office hours, also in Room 022-­‐‑C
•  Mondays 5-­‐‑8pm
•  Tuesdays 5-­‐‑8pm
4
Review and Today’s Outline
•  Recall, last time:
–  Comparing two models
–  Tuning a model/theory to best match the data
•  Today:
– 
– 
– 
– 
– 
– 
– 
– 
More on parameter determination
Manipulating binary data (1’s and 0’s)
Numerical solutions to differential equations
Searching
Sorting
Dynamic memory allocation
Linked lists
Further topics
5
Tuning a Model to Best Match Some Data
6
Tuning a Model
7
Tuning a Model
Can we figure out which model – which values of a and b – the data most favors?
8
Probability of Some Observation
9
Probability of Multiple Observations
The probability of the collection of data – 3 observations – is just the product of the three individual probabilities
10
Probability of Multiple Observations
The probability of the collection of data – k observations – is just the product of the k individual probabilities
11
P: The χ2 Likelihood Function
12
Minimizing the χ2
13
Minimizing the χ2
14
Minimizing the χ2
15
Minimizing the χ2
16
More Powerful Application: An Arbitrary Theory
17
FiPing with Gnuplot
P(x;a, b, c) =
1
2π c
2
e
−
(ax−b)2
2c 2
18
Assessing the Quality of a Fit
19
Assessing the Quality of a Fit
Trivial case
Consequence: If the number of fit parameters is greater than or equal to the number of data points the χ2 is undefined.
20
Assessing the Quality of a Fit
P(x;a, b, c) =
1
2π c
2
e
−
(ax−b)2
2c 2
21
Assessing the Quality of a Fit
So there is a 90% probability that, if the data were consistent with the model (here a Gaussian-­‐‑like thing with 3 params), the data would have a higher chi2 value. Too good to be true? Why are the points so close to the model? Did the fit procedure cheat in some way? Are the uncertainties over-­‐‑estimated?
22
FiPing is Done EVERYWHERE
23
Curve FiPing
24
Deviations from the Model
25
The Pull Distribution
26
Bias – Is the Prediction In Accord with the Data?
27
Clusters of Data Above/Below
28
Clusters of Data Above/Below
29
More Testing of Compatibility
30
Cumulative Distribution Function
31
Cumulative Distribution Function
32
Example: PDF and CDF
33
Empirical Distribution Function
34
Empirical Distribution Function
•  The ECDF is made from “unbinned” data
–  not from a binned histogram
–  use raw measured values
•  Do this by:
1.  say you have N values, xi
2.  sort the N values in order of increasing value
3.  plot each of the N values with xi on the x-­‐‑axis and i/
N on the y-­‐‑axis
• 
Now, compare model’s CDF and the data’s ECDF…
35
Testing Compatibility
36
Testing Compatibility
37
Manipulating Binary Data
38
Bits and Bytes
39
Flipping Bits
40
Bitwise Operators
41
Bitwise AND
42
Bitwise OR
43
Bitwise Left Shift
44
Bitwise Right Shift
45
Bitwise Inverse
46
Bitwise Shift on Constants
47
Testing and SePing Bits
(
)
48
Testing and SePing Bits: Masks
“Mask”
(
)
49
Testing and SePing Bits: Masks
(
)
50
Clearing a Bit
51
Manipulating Binary Data: Example
52
Hexadecimal Representation
53
Hexadecimal Representation
54
Hexadecimal Representation
55
Data Storage
56
Example
57
Example
58
Example
59
The Exclusive Or (XOR) Operation
60
The Exclusive Or (XOR) Operation
61
XOR Application
62
XOR Application
Say you want to send this plain-­‐‑text “message”:
1 1 1 0 0 0 0 0 You and the recipient have a copy of some pre-­‐‑
determined “key”
^ ^ ^ ^ ^ ^ ^ ^
1 0 1 0 1 0 1 0 key ^ message = encrypted message:
0 1 0 0 1 0 1 0 And you send this encrypted message to your intended recipient….
63
XOR Application
Your recipient receives your encrypted message:
0 1 0 0 1 0 1 0 and applies the key:
^ ^ ^ ^ ^ ^ ^ ^
1 0 1 0 1 0 1 0 1 1 1 0 0 0 0 0 and gets a copy of the plain-­‐‑text message. Successful secret communication!
64
XOR Application
One flaw: if someone intercepts both the coded and plain message:
1 1 1 0 0 0 0 0 ^ ^ ^ ^ ^ ^ ^ ^
0 1 0 0 1 0 1 0 1 0 1 0 1 0 1 0 They can derive the key!
This a problem if the sender/recipient ever re-­‐‑use a key.
65
Review: Bitwise Operators
66
Review: Testing and SePing Bits
67
Storage of Binary Data
68
The fputc Function
69
The fputc Function
1 byte
4 bytes
1 byte
4 bytes = 4 chars
70
Comparison of ASCII and Binary Storage
10 bytes = 10 chars
4 bytes = 4 chars
71
More on Characters
72
Signed v. Unsigned Vars
73
Signed vs. Unsigned Vars
•  Example:
–  Say a = 1d = 00000001b and
b = -1d = 10000001b
74
Signed vs. Unsigned Vars
•  Example:
–  Say a = 1d = 00000001b and
b = -1d = 10000001b
–  Then
a+b = 1d + (-1d) = ?
75
Signed vs. Unsigned Vars
•  Example:
–  Say a = 1d = 00000001b and
b = -1d = 10000001b
–  Then
a+b = 1d + (-1d) = 00000001b + 10000001b
76
Signed vs. Unsigned Vars
•  Example:
–  Say a = 1d = 00000001b and
b = -1d = 10000001b
–  Then
a+b = 1d + (-1d) = 00000001b + 10000001b
= 10000010b
77
Signed vs. Unsigned Vars
•  Example:
–  Say a = 1d = 00000001b and
b = -1d = 10000001b
–  Then
a+b = 1d + (-1d) = 00000001b + 10000001b
= 10000010b
= -2d
78
Signed vs. Unsigned Vars
•  Example:
–  Say a = 1d = 00000001b and
b = -1d = 10000001b
–  Then
a+b = 1d + (-1d) = 00000001b + 10000001b
= 10000010b
= -2d
–  That looks wrong to me….
79
Trick: Two’s Complement Notation
80
Trick: Two’s Complement Notation
•  To form the two’s complement of any negative number:
1.  Convert |number| to N-­‐‑bit representation
2.  Subtract |number| from 2N
3.  Add one to the number
• 
Examples, 8-­‐‑bit negative numbers:
– 
– 
If a = -­‐‑1 then |-­‐‑1| in 8-­‐‑bit representation is 00000001
a2c = (11111111 – 00000001) + 00000001
= 11111111
If a = -5 then
a2c = 11111111 – 00000101 + 00000001
= 11111011
81
Signed vs. Unsigned Vars
•  Example:
–  Say a = 1d = 00000001b and
b = -1d = 11111111b (using 2’s complement)
–  Then
a+b = 1d + (-1d) = 00000001b + 11111111b
= 00000000b
= 0d
–  That’s beUer!
82
More On Binary Data Output
83
More On Binary Data Output
84
More On Binary Data Output
85
More On Binary Data Input
86
More On Binary Data Output
87
More On Binary Data Output
88
More On Binary Data Input
89
More On Binary Data Input
90
fopen: File I/O Modes
91
fopen, fwrite and fread
92
rewind
93
fseek
94
fseek
95
ftell
96
feof
97
Writing Binary for Arbitrary Structs
98
Filesize Comparison of Binary v. ASCII Formats
99
Solving Differential Equations
100
A Trajectory
101
A Trajectory
We don’t know v(t) but we know v(t0) and the time derivative.
We seek v(t).
102
A Trajectory
result:
one step:
not very accurate!!!
All we know is the derivative of v! Here v=y and t=x. 103
A BePer Method
104
A BePer Method
All we know is the derivative! 105
A BePer Method
All we know is the derivative! 106
A BePer Method
107
A BePer Method
108
Passing Pointers to Structures
109
Unintended Consequences
110
Unintended Consequences
Warning tells you to rewrite this!
111
Optional Function Arguments
112
Default Argument Values in Functions: C++
113
Default Argument Values in Functions: C++
114
Default Argument Values in Functions: C++
Example:
115
Searching in Arrays
116
Searching for a Particular Item
117
Method: Linear Search
118
Method: Binary Search
119
Method: Binary Search
120
Method: Binary Search
121
Method: Binary Search
122
Method: Binary Search
123
Search Speed: Linear Method
124
Search Speed: Binary Method
125
Sorting
126
Sorting Methods
127
Method: Selection Sort
128
Method: Selection Sort
129
Method: Selection Sort 130
Method: Quicksort 131
Method: Quicksort 132
Method: Quicksort 133
Method: Quicksort 134
Method: Quicksort 135
Method: Quicksort 136
Download