ENEL2CM Assignment 2 (2025)
ENEL2CM Assignment 2 (2025)
ENEL2CM
ASSIGNMENT 2
OWETHU DLUDLA
221006252
28 MARCH 2025
1|Page
QUESTION 1
a) import numpy as np
# Store the yields in a NumPy array
b) import numpy as np
# Calculate and print the mean, standard deviation, and variance
mean_yield = np.mean(yields)
std_dev = np.std(yields)
variance = np.var(yields)
2|Page
c) import numpy as np
# Scale the yields up by a factor of 1.1
3|Page
d) import numpy as np
mean_yield = 50.80
QUESTION 2
x = symbols('x')
f = (x**2 + 1) * cos(x)
differential_f = diff(f, x)
integration_f = integrate(f, x)
4|Page
b) from sympy import symbols, diff, cos
x = symbols('x')
f = (x**2 + 1) * cos(x)
differential = diff(f, x)
5|Page
c) from sympy import symbols, integrate, cos
x = symbols('x')
f = (x**2 + 1) * cos(x)
6|Page
d) from sympy import symbols, diff, integrate, cos
import numpy as np
x = symbols('x')
f = (x**2 + 1) * cos(x)
df_dx = diff(f, x)
int_f = integrate(f, x)
plt.figure(figsize=(10, 6))
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid(True)
plt.show()
7|Page
QUESTION 3
inventory = [
("Water", 100),
("Acetone", 75),
("Ethanol", 60)
# Open the file in write mode (this will create the file if it doesn't exist)
# Iterate through the inventory list and write each substance and quantity
file.write(f"{substance}, {quantity}\n")
8|Page
b) # Open the file in read mode
contents = file.read()
print(contents)
# Open the file in append mode to add to the end of the file
file.write(new_substance + "\n")
9|Page
d) # Define the name of the inventory file
inventory_file = "inventory.txt"
inventory_file = "inventory.txt"
try:
file.write(new_substance + "\n")
lines = file.readlines()
10 | P a g e
print("\nContents of the inventory file:")
print(line.strip())
except FileNotFoundError:
except Exception as e:
11 | P a g e
QUESTION 4
a) import pandas as pd
data = {
df = pd.DataFrame(data)
df.to_csv('reactions.csv', index=False)
df_loaded = pd.read_csv('reactions.csv')
print(df_loaded)
12 | P a g e
b) import pandas as pd
df_loaded = pd.read_csv('reactions.csv')
average_yield = df_loaded['Product_Yield_Percentage'].mean()
13 | P a g e
c) import pandas as pd
df_loaded = pd.read_csv('reactions.csv')
yield_statistics = df_loaded['Product_Yield_Percentage'].describe()
print(yield_statistics)
d) import pandas as pd
#Load the CSV file using Pandas (assuming the CSV file already exists)
df_loaded = pd.read_csv('reactions.csv')
#Filter the dataset to include only reactions with high efficiency (e.g., yield
percentage >= 90)
high_efficiency_df.to_csv('high_efficiency_reactions.csv', index=False)
14 | P a g e
#Print the filtered dataset (optional)
print(high_efficiency_df)
15 | P a g e