You are on page 1of 4

In [1]:

import numpy as np

In [4]:

#Defining Matrix A
A = np.array([[0, 0, 0, 1],
[1, 1, 1, 1],
[4, 2, np.sqrt(2), 1],
[16, 4, 2 ,1],
[4, 2, np.sqrt(2), 1]])

In [5]:

#Define b array containing Y values


b = np.array([0, 2, 3, 4, 4])

In [8]:

#Compute A Transpose A
ATA = np.matmul(np.transpose(A),A)

In [12]:

#Compute A Transpose b
ATb = np.matmul(np.transpose(A),b)

In [13]:

np.linalg.solve(ATA, ATb)

Out[13]:

array([-4.43364770e-01, 3.10355339e+00, -6.60188621e-01, -1.3989580


3e-14])

In [14]:

import matplotlib.pyplot as plt

In [15]:

#Define data points


x = np.array([0, 1, 2, 4 ,2])
y = np.array([0, 2, 3, 4, 4])
In [22]:

#Plot data points


fig, ax = plt.subplots(figsize = (7, 7))
plt.axis([-1, 5, -1, 5])
plt.grid()

plt.scatter(x, y, label = 'Data Points')


plt.xlabel('x')
plt.ylabel('y', rotation = 0)
plt.title('Question 2')
plt.legend()
plt.show()
In [24]:

#Plot data points and line of best fit


fig, ax = plt.subplots(figsize = (7, 7))
plt.axis([-1, 5, -1, 5])
plt.grid()

plt.scatter(x, y, label = 'Y = aX^2 + bX + cX^(1/2) + d')


#Define line of best fit equation
xx = np.arange(-1, 5, 0.0001)
Y = -4.43364770e-01*np.square(xx) + 3.10355339e+00*xx + -6.60188621e-01*np.sqrt(
xx) + -1.39895803e-14
plt.plot(xx, Y)
plt.xlabel('x')
plt.ylabel('y', rotation = 0)
plt.title('Question 2')
plt.legend()
plt.show()

/Users/feihuyan/anaconda3/lib/python3.7/site-packages/ipykernel_laun
cher.py:7: RuntimeWarning: invalid value encountered in sqrt
import sys
In [ ]:

You might also like