0% found this document useful (0 votes)
17 views1 page

CSC 225 Compilation (Reviewed)

The document outlines various types of programming errors, including syntax, runtime, and logical errors, along with their definitions, detection methods, and examples. It also discusses Python's dynamic and strong typing, sources of numerical errors, and methods for numerical differentiation and interpolation. Additionally, it covers the Discrete Fourier Transform (DFT) and Fast Fourier Transform (FFT), explaining their significance and computational efficiency in signal processing.

Uploaded by

Abdullah Opadeji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

CSC 225 Compilation (Reviewed)

The document outlines various types of programming errors, including syntax, runtime, and logical errors, along with their definitions, detection methods, and examples. It also discusses Python's dynamic and strong typing, sources of numerical errors, and methods for numerical differentiation and interpolation. Additionally, it covers the Discrete Fourier Transform (DFT) and Fast Fourier Transform (FFT), explaining their significance and computational efficiency in signal processing.

Uploaded by

Abdullah Opadeji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Types of Errors in Programming

Syntax Errors
Definition: Violations of the rules that define the structure of a language.

Detection: Found during the parsing stage before program execution.

Example: Missing a colon at the end of a statement in Python.

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.

Detection: Detected only when the program runs.

Examples of Built-in Exceptions:

ZeroDivisionError: Division by zero.


TypeError: Incompatible types.
NameError: Using an undefined variable.
# ZeroDivisionError
result = 10 / 0

# 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.

Detection: Hard to find because the program runs without crashing.

Example: Incorrect formula implementation.

# Logical error in calculating the area of a rectangle


width = 5
height = 10
area = width + height # Should be width * height

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:

Equation: True Value = Approximation + Error


True Error: Et = True Value − Approximation

2. True Fractional Relative Error:

True Error
Equation: True Fractional Relative Error =
True Value

3. True Percent Relative Error:

Equation: True Percent Relative Error = True Fractional Relative Error × 100%

4. Approximate Error:

Current Approximation−Previous Approximation


Equation: Approximate Error = ( ) × 100%
Current Approximation

Sources of Errors in Numerical Methods


Mislaid Measurements: Errors from inaccurate input data.
Wobbly Algorithms: Errors from the methods used in calculations.

Round-off Errors
Definition: Errors introduced when numbers are rounded to fit a storage format.

Example:

# Representing 156.78 might introduce a small error in storage

Chopping (Truncating)
Definition: Cutting off digits beyond a certain point without rounding.

Example:

# Truncating 1.41421356 to 1.414213

Example Calculation Using Newton’s Method


Square root of 2:
True Value: Approximately 1.41421356
Approximated Value: 1.414213
True Error: 0.00000056 (difference due to round-off)

Matplotlib: Python Visualizer for Scientific Data


Matplotlib is a powerful library in Python used for creating static, animated, and interactive visualizations. It is especially useful in scientific computing for its ability to produce high-quality plots.

Basic Plotting
Line Plot
To create a simple line graph of y versus x:

import matplotlib.pyplot as plt

x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]

plt.plot(x, y) # Plots y = x^2 for x in [0, 4]


plt.show()
This connects the points (x[i], y[i]) from the lists x and y using a blue line by default.

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:

plt.plot(x, y, 'ro') # Plots y versus x as red circles

Colors
Short color codes can be used for different colors:

'b' for blue


'g' for green
'r' for red

Adding Titles and Labels


To add a title and labels to the axes:

plt.title('Title') # Sets the title of the plot


plt.xlabel('X Label') # Sets the label for the x-axis
plt.ylabel('Y Label') # Sets the label for the y-axis

Setting Figure Size


To set the width and height of the figure in inches:

plt.figure(figsize=(w, h)) # Sets the figure size to width w and height h

Subplots
The subplot() function is used to split a figure into multiple subplots. It takes three arguments:

1. nrows - The number of rows to split the figure into.


2. ncols - The number of columns to split the figure into.
3. index - The position of the subplot in the grid, starting at the top left corner with 1.

Example:
plt.subplot(2, 3, 4) # Places a subplot in the 4th position in a 2x3 grid

Displaying the Plot


To display your plot, use:

plt.show() # Displays the plot

Using Numpy with Matplotlib


To generate data using Numpy and plot it with Matplotlib:

import numpy as np

x = np.linspace(-5, 5, 100) # Generates 100 evenly-spaced numbers between -5 and 5


plt.plot(x, x**2)
plt.show()

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.

Setting Figure Size with Matplotlib


To create a plotting window of a specific size:

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.

Types of Spline Interpolation


1. Linear Spline Interpolation
2. Quadratic Spline Interpolation
3. Cubic Spline Interpolation

Why Use Spline Interpolation?


Spline interpolation is favored because it reduces oscillations and provides a smoother, more accurate representation of the original function compared to high-order polynomial interpolation.

Types of Spline Interpolation


Linear Spline Interpolation
Linear spline interpolation involves drawing straight lines between consecutive points. The interpolating linear spline, also called a spline of degree 1, is given by:

f(x1 )−f(x0 )
f (y) = f (y0 ) + (y − y0 ), y0 ≤ y ≤ y1
x1 −x0

Quadratic Spline Interpolation


Quadratic spline interpolation involves fitting a quadratic polynomial between each pair of data points. The quadratics of the spline are given by:

2
f (y) = b1 y + c1 y + d1 , y0 ≤ y ≤ y1

Cubic Spline Interpolation


Cubic spline interpolation is the most common degree of spline used. It involves fitting a cubic polynomial between each pair of data points. The interpolating cubic spline is given by the cubics as:

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.

Mathematically, it’s represented as:

f(xj+1 )−f(xj )

f (xj ) ≈
xj+1 −xj

Key points:

Uses xj and xj+1 .

Suitable for points at the beginning of the data set.

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.

Mathematically, it’s represented as:

f(xj )−f(xj−1 )

f (xj ) ≈
xj −xj−1

Key points:

Uses xj−1 and xj .

Suitable for points at the end of the data set.

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.

Mathematically, it’s represented as:

f(xj+1 )−f(xj−1 )

f (xj ) ≈
xj+1 −xj−1

Key points:

Uses xj−1 and xj+1 .

Provides a more accurate estimate, especially for evenly spaced data points.
Suitable for points not at the boundaries of the data set.

Discrete Fourier Transform (DFT)


The Discrete Fourier Transform (DFT) is a mathematical technique used to transform a discrete time-domain signal into its corresponding frequency-domain representation. The formula for the DFT of a sequence xn of length N is given by:

N −1 2πkn 2πkn
Xk = ∑ xn [cos( ) − i ⋅ sin( )]
n=0 N N

where:

Xk is the k-th frequency component.


xn is the n-th time-domain sample.
N is the total number of samples.
k ranges from 0 to N − 1.

Calculation of Xk

Let's calculate Xk for k = 0, 1, 2, 3 with the given xn values:

1. For k = 0:

X0 = ∑ xn [cos(0) − i ⋅ sin(0)]

n=0

X0 = x0 (1) + x1 (2) + x2 (3) + x3 (4)

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 = 1 ⋅ 1 + 2 ⋅ (−i) + 3 ⋅ (−1) + (4) ⋅ i

X1 = −2 + 2i

3. For k = 2:

X2 = ∑ xn [cos(πn) − i ⋅ sin(πn)]

n=0

X2 = x0 (1) + x1 (−1) + x2 (1) + x3 (−1)

X2 = 1 ⋅ 1 + 2 ⋅ (−1) + 3 ⋅ 1 + (4) ⋅ (−1)

X2 = −2

4. For k = 3:

3
6πn 6πn
X3 = ∑ xn [cos( ) − i ⋅ sin( )]
4 4
n=0

X3 = x0 (1) + x1 (i) + x2 (−1) + x3 (−i)

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

Fast Fourier Transform (FFT)


The Fast Fourier Transform (FFT) is an algorithm designed to efficiently compute the Discrete Fourier Transform (DFT) and its inverse. The DFT is a fundamental tool in signal processing and many other fields for analyzing the frequency
content of discrete signals.

Discrete Fourier Transform (DFT)


The DFT converts a sequence of complex or real numbers into a sequence of complex numbers. The computation of DFT requires N
2
complex multiplications and N (N − 1) complex additions, where N is the number of data points. This
computational cost is high in terms of both memory and time, especially when N is large, because the DFT does not utilize the symmetry and periodic properties inherent in the data.

Computational Complexity of DFT


Complex Multiplications: N
2

Complex Additions: N (N − 1)

Fast Fourier Transform (FFT)


The FFT is an optimized algorithm to compute the DFT in O(N log N ) time, making it significantly faster for large datasets. The FFT leverages the symmetry and periodicity properties of the DFT to reduce the number of required
computations.

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.

Why FFT is Faster than DFT


The FFT is faster than the DFT because it reduces the computational complexity from O(N
2
) to O(N log N ). This is achieved by:

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.

Manual Computation of FFT for a Small Dataset


To illustrate the FFT process, we will manually compute the FFT for a small dataset of size N = 4.

Example Dataset
Consider the dataset: x = [1, 2, 3, 4].

Step-by-Step FFT Calculation


1. Bit-reversal Permutation: Rearrange the input sequence based on bit-reversed indices.

Original indices: 0, 1, 2, 3

Bit-reversed indices: 0, 2, 1, 3

Rearranged sequence: [1, 3, 2, 4]

2. Butterfly Computations:

Stage 1:

Pair (1, 3): [1 + 3, 1 − 3] = [4, −2]

Pair (2, 4): [2 + 4, 2 − 4] = [6, −2]

Result after Stage 1: [4, 6, −2, −2]

Stage 2:

Combine results with twiddle factors WN = e


−2πi/N
:
[4, 6] and [−2, −2] with W4 = e
−2πi/4
= i

[4 + 6, 4 − 6, −2 + (−2i), −2 − (−2i)]

Result: [10, −2, −2 − 2i, −2 + 2i]

Final FFT Result


The FFT of x = [1, 2, 3, 4] is [10, −2, −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.

You might also like