You are on page 1of 16

DCEE23 – Numerical Methods and Analysis

Practical Examination – Linear and Polynomial Regression

Objectives:
At the end of the exercise, the students should be able to:

● compute linear regressions using matrix operations in Google Colab.


● compute polynomial regressions using matrix calculations in Google Colab.
● perform linear regression analysis by computing matrices and plotting the results
in Google Colab.
● familiarize students with the process of computing polynomial regressions
through matrix operations and generating visual plots using Google Colab.
Materials:

● Android phone or computer with internet access


● CvSU-provided Google account (to access Google Colab)

Instruction:
Solve the following problems analytically (manual computation) and by creating a
code on Google Colab. You will be required to plot data points and the linear /
polynomial model equation. Each problem must have an analytical solution and the
code used in solving the given problem. You can put your solution and coding below the
“Analytical Solution:” and “Coding on Google Colab:” titles, respectively.
Note: You will be using Google Colab to perform matrix operation to find the
determinants, coefficients and the model equation, and plotting data along with the
model equation.
DCEE23 – Numerical Methods and Analysis

Problem 1:
A study was done to compare the speed x (in miles per hour) with the mileage y
(in miles per gallon) of an automobile. The results are shown in the table.
Fit a linear model and a quadratic model for the data. Determine which model
better fits the data. Plot the data along with the linear and quadratic model.
DCEE23 – Numerical Methods and Analysis

Analytical Solution:
DCEE23 – Numerical Methods and Analysis
DCEE23 – Numerical Methods and Analysis

Coding on Google Colab:


DCEE23 – Numerical Methods and Analysis

Output for Problem 1:


DCEE23 – Numerical Methods and Analysis

Code Used for Problem 1:


import numpy as np

import matplotlib.pyplot as plt

# Define the data

x = np.array([15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75])

y = np.array([22.3, 25.5, 27.5, 29, 28.8, 30, 29.9, 30.2, 30.4, 28.8, 27.4, 25.3, 23.3])

# Define the matrices A, A1, A2, and A3

A = np.array([[13, 585, 30875], [585, 30875, 1798875], [30875, 1798875, 111434375]])

A1 = np.array([[13, 585, 30875], [585, 30875, 1798875], [30875, 1798875, 111434375]])

A2 = np.array([[13, 585, 30875], [585, 30875, 1798875], [30875, 1798875, 111434375]])

A3 = np.array([[13, 585, 30875], [585, 30875, 1798875], [30875, 1798875, 111434375]])

b = np.array([358.4, 16165, 844270])

# Calculate the determinants of A, A1, A2, and A3

det_A = np.linalg.det(A)

det_A1 = np.linalg.det(np.column_stack((b, A[:, 1:])))

det_A2 = np.linalg.det(np.column_stack((A[:, 0], b, A[:, 2])))

det_A3 = np.linalg.det(np.column_stack((A[:, :2], b)))

# Calculate the coefficients

coefficients = np.array([det_A1/det_A, det_A2/det_A, det_A3/det_A])

# Print the results

print("Determinant of A:", det_A)

print("Determinant of A1:", det_A1)

print("Determinant of A2:", det_A2)

print("Determinant of A3:", det_A3)


DCEE23 – Numerical Methods and Analysis

print("Coefficients:", coefficients)

print("Model Equation: y = {:.4f}x^2 + {:.4f}x + {:.4f}".format(coefficients[2], coefficients[1],


coefficients[0]))

# Plot the data and models

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

plt.plot(x, coefficients_linear[1][0] * x + coefficients_linear[0][0], label='Linear Model')

plt.plot(x, coefficients_quadratic[2][0] * x ** 2 + coefficients_quadratic[1][0] * x +


coefficients_quadratic[0][0], label='Quadratic Model')

plt.xlabel('Speed (x)')

plt.ylabel('Mileage (y)')

plt.title('Comparison of Linear and Quadratic Models')

plt.legend()

plt.grid(True)

plt.show()

Problem 2:
A basketball is dropped from a height of about 5.25 feet. The height of the
basketball is recorded 23 times at intervals of about 0.02 second. The results are shown
in the table.
Fit a linear model and a quadratic model for the data. Determine which model
better fits the data. Plot the data along with the linear and quadratic model.
DCEE23 – Numerical Methods and Analysis
DCEE23 – Numerical Methods and Analysis

Analytical Solution:
DCEE23 – Numerical Methods and Analysis
DCEE23 – Numerical Methods and Analysis

Coding on Google Colab:


DCEE23 – Numerical Methods and Analysis

Output for Problem 2:


DCEE23 – Numerical Methods and Analysis

Code Used for Problem 2:

import numpy as np
import matplotlib.pyplot as plt

# Given data
height = np.array([5.23594, 5.20353, 5.16031, 5.0991, 5.02707, 4.95146, 4.58062, 4.74979,
4.63096, 4.50132,
4.35728, 4.19523, 4.02958, 3.84593, 3.65507, 3.44981, 3.23375, 3.01048, 2.76921, 2.52074,
2.25786,
1.98058, 1.63488])
time = np.array([0, 0.02, 0.04, 0.06, 0.08, 0.099996, 0.119996, 0.139992, 0.159988, 0.179988,
0.199984,
0.219984, 0.23998, 0.25993, 0.27998, 0.299976, 0.319972, 0.339961, 0.359961, 0.379951,
0.399941,
0.419941, 0.439941])

# Define the matrices A, A1, A2, and A3


A = np.array([[23, 5.059, 1.518], [5.059, 1.518, 0.512], [1.518, 0.512, 0.184]])
A1 = np.array([[23, 5.059, 1.518], [5.059, 1.518, 0.512], [1.518, 0.512, 0.184]])
A2 = np.array([[23, 5.059, 1.518], [5.059, 1.518, 0.512], [1.518, 0.512, 0.184]])
A3 = np.array([[23, 5.059, 1.518], [5.059, 1.518, 0.512], [1.518, 0.512, 0.184]])
b = np.array([90.081, 16.565, 4.428])

# Calculate the determinants of A, A1, A2, and A3


det_A = np.linalg.det(A)
det_A1 = np.linalg.det(np.column_stack((b, A[:, 1:])))
det_A2 = np.linalg.det(np.column_stack((A[:, 0], b, A[:, 2])))
DCEE23 – Numerical Methods and Analysis

det_A3 = np.linalg.det(np.column_stack((A[:, :2], b)))

# Print the results


print("Determinant of A:", det_A)
print("Determinant of A1:", det_A1)
print("Determinant of A2:", det_A2)
print("Determinant of A3:", det_A3)
print("Coefficients:", coefficients)
print("Model Equation: y = {:.4f}x^2 + {:.4f}x + {:.4f}".format(coefficients[2], coefficients[1],
coefficients[0]))

# Linear model
linear_coeffs = np.polyfit(time, height, 1)
linear_model = np.polyval(linear_coeffs, time)

# Quadratic model
quadratic_coeffs = np.polyfit(time, height, 2)
quadratic_model = np.polyval(quadratic_coeffs, time)

# Plotting the data and models


plt.figure(figsize=(8, 6))
plt.scatter(time, height, color='b', label='Data')
plt.plot(time, linear_model, color='r', label='Linear Model')
plt.plot(time, quadratic_model, color='g', label='Quadratic Model')
plt.xlabel('Time (seconds)')
plt.ylabel('Height (feet)')
DCEE23 – Numerical Methods and Analysis

plt.title('Basketball Drop Data in Linear and Quadratic Models')


plt.legend()
plt.grid(True)
plt.show()

Python Google Colab Notebook link:


https://colab.research.google.com/drive/1xHuggqOLdfovJ1ZDdQb3dKbXbVcdzLU6?usp=sharing

You might also like