0% found this document useful (0 votes)
1 views

PH3094D Computational Lab_Exercise3

The document outlines a series of exercises for a computational lab focused on various tasks using Python's NumPy and Matplotlib libraries. Key activities include simulating a random walk, performing matrix operations, manipulating images, finding polynomial roots, and conducting statistical analyses. Additional exercises involve array manipulation, filtering, sorting, and creating various visualizations such as sine waves, bar charts, and histograms.

Uploaded by

haris mk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

PH3094D Computational Lab_Exercise3

The document outlines a series of exercises for a computational lab focused on various tasks using Python's NumPy and Matplotlib libraries. Key activities include simulating a random walk, performing matrix operations, manipulating images, finding polynomial roots, and conducting statistical analyses. Additional exercises involve array manipulation, filtering, sorting, and creating various visualizations such as sine waves, bar charts, and histograms.

Uploaded by

haris mk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PH3094D Computational Lab

Exercise-3

1. Random Walk Simulation:


●​ Objective: Simulate a 1-dimensional random walk.
●​ Instructions:
○​ Create a NumPy array to store the positions of the random walk.
○​ Initialize the starting position to 0.
○​ In a loop:
■​ Generate a random step: -1 (move left) or 1 (move right) with equal
probability.
■​ Update the current position by adding the random step.
■​ Store the updated position in the array.
○​ Visualize the random walk using matplotlib.pyplot.plot().
2. Matrix Operations:
●​ Objective: Perform matrix multiplication and inversion.
●​ Instructions:
○​ Create two random 3x3 matrices using np.random.rand().
○​ Perform matrix multiplication using np.dot().
○​ Calculate the inverse of one of the matrices using np.linalg.inv().
○​ Verify that the product of a matrix and its inverse is approximately the identity
matrix.
3. Image Manipulation (Basic):
●​ Objective: Load and manipulate a grayscale image.
●​ Instructions:
○​ Load a grayscale image using plt.imread() (ensure you have an image file).
○​ Convert the image to a NumPy array.
○​ Invert the image by subtracting the pixel values from 255.
○​ Display the original and inverted images using plt.imshow().
4. Polynomial Root Finding:
●​ Objective: Find the roots of a polynomial.
●​ Instructions:
○​ Define the coefficients of a polynomial (e.g., [1, -2, 1]).
○​ Use np.roots() to find the roots of the polynomial.
○​ Verify the results by substituting the roots back into the polynomial equation.
5. Statistical Analysis:
●​ Objective: Calculate basic statistical measures for a dataset.
●​ Instructions:
○​ Create an array of 100 random numbers from a normal distribution.
○​ Calculate the mean, median, standard deviation, and variance of the array using
NumPy functions (e.g., np.mean(), np.median(), np.std(), np.var()).
○​ Create a histogram of the data using plt.hist().

6. Array Manipulation:
●​ Objective: Create an array of 10 random integers between 1 and 50.
●​ Reshape the array into a 2x5 matrix.
●​ Find the minimum and maximum values in the array.
●​ Calculate the mean and standard deviation of the array.
7. Array Indexing and Slicing:
●​ Objective: Create an array of 20 random numbers.
●​ Extract the first 5 elements of the array.
●​ Extract every third element of the array.
●​ Create a new array containing only the even-indexed elements.
8. Array Filtering:
●​ Objective: Create an array of 15 random numbers.
●​ Find all elements in the array that are greater than 5.
●​ Find all elements in the array that are between 10 and 20.
9. Array Sorting and Searching:
●​ Objective: Create an array of 12 random numbers.
●​ Sort the array in ascending order.
●​ Find the indices of the sorted array.
●​ Find the index of the value 7 in the array (if it exists).
10. Sine Wave Visualization:
●​ Objective: Create a plot of the sine function over a specified interval.
●​ Instructions:
○​ Use numpy.linspace() to generate an array of x-values.
○​ Calculate the corresponding y-values using numpy.sin().
○​ Use matplotlib.pyplot.plot() to create a line plot.
○​ Add labels to the x-axis and y-axis, and a title to the plot.
11. Bar Chart for Student Grades:
●​ Objective: Create a bar chart to visualize the distribution of student grades in a
class.
●​ Instructions:
○​ Create a list or array of student grades.
○​ Use matplotlib.pyplot.bar() to create a bar chart.
○​ Set appropriate labels for the x-axis (e.g., student names) and y-axis
(grades).
○​ Add a title to the plot.
12. Scatter Plot for Height and Weight:
●​ Objective: Create a scatter plot to visualize the relationship between height and
weight of a group of individuals.
●​ Instructions:
○​ Create two lists or arrays: one for heights and one for corresponding
weights.
○​ Use matplotlib.pyplot.scatter() to create a scatter plot.
○​ Set appropriate labels for the x-axis (height) and y-axis (weight).
○​ Add a title to the plot.
13. Histogram for Age Distribution:
●​ Objective: Create a histogram to visualize the distribution of ages in a population.
●​ Instructions:
○​ Create a list or array of ages.
○​ Use matplotlib.pyplot.hist() to create a histogram.
○​ Set appropriate labels for the x-axis (age) and y-axis (frequency).
○​ Add a title to the plot.
14. Subplots for Multiple Plots:
●​ Objective: Create a figure with multiple subplots to visualize different aspects of a
dataset.

●​ Instructions:
○​ Create a figure using matplotlib.pyplot.figure().
○​ Add subplots using matplotlib.pyplot.subplot().
○​ In each subplot, create a different type of plot (e.g., line plot, bar chart,
scatter plot) using the appropriate data.
○​ Set appropriate labels and titles for each subplot.

You might also like