CSC 225 Compilation (Reviewed)
CSC 225 Compilation (Reviewed)
Syntax Errors
Definition: Violations of the rules that define the structure of a language.
if x == 10
print("x is 10")
# Correct syntax:
if x == 10:
print("x is 10")
Runtime Errors
Definition: Errors that occur during program execution.
# TypeError
result = '2' + 2
# NameError
print(undefined_variable)
Handling: Using try-except statements to handle exceptions gracefully.
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
Logical Errors
Definition: Errors in the logic of the program that produce incorrect results.
Python Variables
Dynamic Typing
Definition: Python variables can change type during execution.
Example:
x = 10 # x is an integer
x = "Hello" # x is now a string
Strong Typing
Definition: Python enforces type compatibility in operations.
Example:
x = 10
y = "Hello"
z = y + str(x) # x is converted to a string before concatenation
# z equals "Hello10"
Numerical Errors
Accuracy and Precision
Accuracy: How close a calculated value is to the true value.
Precision: How close calculated values are to each other.
Significant Figures
Definition: Digits in a number that carry meaningful information about its precision.
Example: In 53,800, there are 3 significant figures: 5, 3, and 8.
Error Definitions
1. True Value and Error:
True Error
Equation: True Fractional Relative Error =
True Value
Equation: True Percent Relative Error = True Fractional Relative Error × 100%
4. Approximate Error:
Round-off Errors
Definition: Errors introduced when numbers are rounded to fit a storage format.
Example:
Chopping (Truncating)
Definition: Cutting off digits beyond a certain point without rounding.
Example:
Basic Plotting
Line Plot
To create a simple line graph of y versus x:
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
Example:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
plt.plot(x, y)
plt.show()
Customizing Plots
Plotting with Different Styles
To plot with different styles, such as using red circles:
Colors
Short color codes can be used for different colors:
Subplots
The subplot() function is used to split a figure into multiple subplots. It takes three arguments:
Example:
plt.subplot(2, 3, 4) # Places a subplot in the 4th position in a 2x3 grid
import numpy as np
Explanation:
np.linspace(-5, 5, 100) generates 100 evenly-spaced numbers between -5 and 5.
np is shorthand for Numpy.
linspace indicates the "linear space" function.
-5, 5 are the interval endpoints.
100 is the number of evenly-spaced points.
plt.figure(figsize=(12, 10)) # Creates a plotting window of size 12 inches wide and 10 inches tall
This command uses the pyplot module (imported as plt), and figsize=(12, 10) sets the window size in inches.
Interpolation
Interpolation is a mathematical tool used to estimate values based on known data points. It is like filling in blanks in a story or connecting dots in a picture. For instance, if we know some points (e.g., (1,2) and (2,3)), we can draw a line
through these points to estimate the values between them (e.g., the value at 1.5). This technique helps us estimate unknown values using known values.
Spline Interpolation
Spline interpolation involves dividing data into subintervals and fitting a low-degree polynomial to each subinterval. This method creates a smooth path through the data points, akin to a well-drawn map. For example, given points (0, 0),
(1, 1), (2, 0), and (3, 1), we can fit lines y = x, y = 2 − x, and y = x − 2 to the subintervals [0, 1], [1, 2], and [2, 3] respectively, forming a piecewise linear function. This function provides a smooth path between points.
f(x1 )−f(x0 )
f (y) = f (y0 ) + (y − y0 ), y0 ≤ y ≤ y1
x1 −x0
2
f (y) = b1 y + c1 y + d1 , y0 ≤ y ≤ y1
3 2
f (y) = b1 y + c1 y + d1 y + e1 , y0 ≤ y ≤ y1
For more detailed information on spline interpolation, you can refer to the Numerical Methods Textbook.
Numerical Differentiation
Numerical differentiation is a technique used to estimate the derivative of a function at a given point using discrete data points. Here, we explore three common methods: forward difference, backward difference, and central difference.
Forward Difference
The forward difference method estimates the slope of the function at xj using the line that connects xj and xj+1 . This method is useful when you have data points moving forward from the current point.
f(xj+1 )−f(xj )
′
f (xj ) ≈
xj+1 −xj
Key points:
Backward Difference
The backward difference method estimates the slope of the function at xj using the line that connects xj−1 and xj . This method is useful when you have data points moving backward from the current point.
f(xj )−f(xj−1 )
′
f (xj ) ≈
xj −xj−1
Key points:
Central Difference
The central difference method estimates the slope of the function at xj using the line that connects xj−1 and xj+1 . This method provides a more accurate estimate compared to forward and backward differences, as it averages the
forward and backward slopes.
f(xj+1 )−f(xj−1 )
′
f (xj ) ≈
xj+1 −xj−1
Key points:
Provides a more accurate estimate, especially for evenly spaced data points.
Suitable for points not at the boundaries of the data set.
N −1 2πkn 2πkn
Xk = ∑ xn [cos( ) − i ⋅ sin( )]
n=0 N N
where:
Calculation of Xk
1. For k = 0:
X0 = ∑ xn [cos(0) − i ⋅ sin(0)]
n=0
X0 = 1 + 2 + 3 + 4 = 10
2. For k = 1:
3
2πn 2πn
X1 = ∑ xn [cos( ) − i ⋅ sin( )]
4 4
n=0
−iπ/2 −i3π/2
X1 = x0 (1) + x1 (e ) + x2 (−1) + x3 (e )
X1 = −2 + 2i
3. For k = 2:
X2 = ∑ xn [cos(πn) − i ⋅ sin(πn)]
n=0
X2 = −2
4. For k = 3:
3
6πn 6πn
X3 = ∑ xn [cos( ) − i ⋅ sin( )]
4 4
n=0
X3 = 1 ⋅ 1 + 2 ⋅ i + 3 ⋅ (−1) + 4 ⋅ (−i)
X3 = −2 − 2i
Summary
The calculated DFT coefficients Xk for k = 0, 1, 2, 3 are:
X0 = 10
X1 = −2 + 2i
X2 = −2
X3 = −2 − 2i
Complex Additions: N (N − 1)
Principles of FFT
The FFT is based on two main principles:
1. Symmetry: The DFT of a sequence exhibits symmetry properties that can be exploited to reduce computations.
2. Periodicity: The DFT of a sequence is periodic, which means certain computations repeat and can be reused.
FFT Algorithms
There are two main types of FFT algorithms based on the order in which they break down the DFT computations:
1. Decimation-in-Time (DIT): An example of DIT is the Cooley-Tukey algorithm. This approach recursively divides the input sequence into smaller sequences, computes the DFT of the smaller sequences, and combines the results.
2. Decimation-in-Frequency (DIF): An example of DIF is the Sande-Tukey algorithm. This approach works by dividing the frequency domain representation of the sequence and recursively computing the DFT.
Breaking down the DFT computation into smaller, more manageable pieces.
Reusing computations due to the periodicity and symmetry properties of the DFT.
Reducing the number of necessary operations significantly.
Example Dataset
Consider the dataset: x = [1, 2, 3, 4].
Original indices: 0, 1, 2, 3
Bit-reversed indices: 0, 2, 1, 3
2. Butterfly Computations:
Stage 1:
Stage 2:
[4 + 6, 4 − 6, −2 + (−2i), −2 − (−2i)]
Conclusion
The FFT is a powerful algorithm for efficiently computing the DFT, leveraging the properties of symmetry and periodicity to significantly
reduce the computational complexity. By understanding and implementing FFT, we can analyze large datasets more efficiently, making it
an essential tool in various applications, including signal processing, image analysis, and more.