Week 1.3: Programming Tutorial¶
CEGM1000 MUDE: Week 1.3. September 16, 2024.
This notebook was prepared by Berend Bouvy and used in an in-class demonstration on Monday (the first of several programming tutorials).
In [74]:
def add(a, b):
result = a+b
return result
def gen_xhat(A, y):
x_hat = np.linalg.inv(A.T @ A) @ A.T @ y
return x_hat
In [6]:
a = 1
b = 2
result = add(a, b)
result
Out[6]:
In [25]:
import numpy as np
import matplotlib.pyplot as plt
In [11]:
# Replace 'file.csv' with the path to your CSV file
data = np.genfromtxt('data.csv', delimiter=',', skip_header=1) # 'skip_header=1' skips the first row (header)
In [37]:
t = data[:,0]
y = data[:,1]
n_rows = data.shape[0]
n_cols = data.shape[1]
In [61]:
plt.plot(t, y,'o')
plt.title('t vs y')
plt.xlabel('t')
plt.ylabel('y')
Out[61]:
In [39]:
one_vector = np.ones(n_rows)
In [42]:
print(one_vector+ t)
In [79]:
A = np.column_stack((one_vector, t))
In [75]:
x_hat = gen_xhat(A, y)
y_hat = A @ x_hat
In [76]:
plt.plot(t, y,'o')
plt.plot(t, y_hat)
plt.title('t vs y')
plt.xlabel('t')
plt.ylabel('y')
Out[76]:
In [77]:
A_new = np.column_stack((one_vector, t, t**2))
x_hat_new = gen_xhat(A_new, y)
y_hat_new = A_new @ x_hat_new
plt.plot(t, y,'o')
plt.plot(t, y_hat_new)
plt.title('t vs y')
plt.xlabel('t')
plt.ylabel('y')
Out[77]:
In [73]:
e_hat = y-y_hat_new
End of notebook.
© Copyright 2024 MUDE TU Delft.
This work is licensed under a CC BY 4.0 License.