The idea is to fit an appropriate $V = IR$ line with the measured $V$ and $I$ values. How do we do this? The primary idea is to define an
Fitting a straight line $y = px + q$ through the data points is particularly simple. Let the experimental data be in the form of $(x_i, y_i)$ pairs where $x_i$ is the $i^{th} x-$value and $y_i$ is the $y$ value corresponding to $x_i$.
import numpy as np
from matplotlib import pyplot as plt # For plotting data
np.set_printoptions(precision=2, suppress=True)
######### COPY GAUSS ELIMINATE AND BACK SUBSTITUTE METHODS
######### FROM EARLIER LABS INTO THIS CELL
We assume that $x_i$ and $y_i$ values are given as two arrays $X$ and $Y$. Test data samples are given to you in the cell below.
X = np.arange(10)
Y = np.array([-1.12, 0.7, 3.53, 5.47, 7.08, 9.65,\
12.95, 16.65, 18.13, 20.82])
Generally, it is a good idea to
########### YOU RUN THIS CODE!
plt.grid(True) # Gives a background grid
plt.plot(X, Y, 'bo') # Plot data using blue dots for points
plt.show()
Let us now create the $A$ and $B$ matrices.
def create_A(X) :
A = np.zeros((2,2))
# Fill your code here as per the steps given above.
return A
def create_B(X, Y) :
B = np.zeros((2,1))
# Fill your code here as per the steps given above.
return B
Call
############ SOLVE THE EQUATION AX = B TO GET
############ p and q
We can verify if our answer is correct by plotting the data and the line we now have as
## This cell is only for plotting the data so that you can
## visually see whether the answer you have is correct.
plt.grid(True) # Draws a grid on the plot
plt.plot(X, Y, 'bo') # Plot the original data as points
plt.plot(X, X * p + q, 'g--') # Draw a green dotted line as
plt.show() # your answer
x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
y | 7.1 | 7.73 | 11.14 | 10.15 | 11.74 | 12.59 | 15.19 | 14.76 | 18.55 | 17.44 |
The derivations in this case are harder and beyond the scope of the current class. The solution is obtained as follows.
Assume that there are $N$ data points $(x_i, y_i), i = 1, 2, \ldots, N$.
Code skeletons are given in the cells below. Fill the rest of the code.
def create_comb_A(X, m) :
A = np.zeros((X.shape[0], m)) # m is no. of functions
##
## Fill your code here
##
return A
Write the function that actually solves the equation given above and returns the $C$ matrix.
def solve_comb(X, Y, m) : # m is no. of functions
A = create_comb_A(X, m)
##
## Fill code here to create a matrix G = Transpose(A) time A
##
## Fill code here to create a matrix H = Transpose(A) times Y
## Use gauss_eliminate and back_substitute to solve the eqns
RM = gauss_eliminate() # Fill appropriate
C = back_substitute() # matrices as arguments
return C
For this example, you can run the code in each of the cells below and see what happens.
X = np.arange(-3,7)
Y = np.array([-71.67, -20.93, 4.95, 11.8, 10.87,\
4.65, 0.69, 9.29, 33.45, 79.75]) # Fill the data
plt.grid(True)
plt.plot(X, Y, 'b.')
plt.show()
Clearly we cannot fit a straight line through the data.
The pattern is quite different. The curve we are trying to fit is $y = c_0 + c_2x + c_2x^2 + c_3x^3$. Thus, $f_O(x) = 1, f_1(x) = x, f_2(x) = x^2$ and $f_3(x) = x^3$. So, define the functions in the cell below.
def f0(x) :
return 1
def f1(x) :
return x
def f2(x) :
return x**2
def f3(x) :
return x**3
We already have the $Y$ matrix because it contains the $Y$ values. The only thing to do is to create the $A$ matrix. See how we do it with the code skeleton in the cell below. The way we are defining $Y_0, Y_1, Y_2$ and $Y_3$, they automatically are $N\times1$ vectors where $N$ is the number of data points. In this case, $N = 10$. You can use this as a template for solving the problem given as an exercise.
Y0 = np.array([[f0(x) for x in X]])
Y1 = np.array([[f1(x) for x in X]])
Y2 = np.array([[f2(x) for x in X]])
Y3 = np.array([[f3(x) for x in X]])
A = np.concatenate((Y0.T, Y1.T, Y2.T, Y3.T), axis=1)
print(A)
Once we have the $A$ matrix, we need two other matrices before doing
G = A.T.dot(A)
H = A.T.dot(Y).reshape(A.shape[1], 1) # To get a proper vector
print(G)
print(H)
RA = gauss_eliminate(np.concatenate((G, H), axis=1))
ans = back_substitute(RA)
print(ans.T)
The curve that fits the data with the least error is thus $$ y = 12.71 + 1.91x -5.28x^2 + 1.14x^3 $$ How good is this? Let us plot the original data points, i.e. $X$ and $Y$ values along side the curve. Look at the plot and make your own judgement!
plt.grid(True)
plt.plot(X, Y, 'bo')
plt.plot(X,12.71 + 1.91*X - 5.28*X**2 + 1.14*X**3, 'g-')
plt.show()
A rubber band is plucked and left to vibrate. A snap-shot of the vibrating rubber band is taken with a high-speed camera and its positions are measured. The data is given as $(x_i, y_i)$ pairs in the arrays below. As it is a vibrating motion, we try and fit sine waves through the data. Such a curve is given mathematically by the equation $$ y = c_0 + c_1\sin(x) + c_2\sin(2x) + c_3\sin(3x) $$ Do exactly the same things we did in the worked out example and show the obtained curve.
Code cells are given below to help you.
# The data points are given below in X and Y vectors
X = np.array([0., 0.2, 0.41, 0.61, 0.81, 1.02,\
1.22, 1.42, 1.63, 1.83, 2.03, 2.24,\
2.44, 2.64, 2.85, 3.05, 3.25, 3.46,\
3.66, 3.86, 4.07, 4.27, 4.47, 4.68,\
4.88, 5.08, 5.29, 5.49, 5.69, 5.9, \
6.1, 6.31, 6.51, 6.71, 6.92, 7.12,\
7.32, 7.53, 7.73, 7.93, 8.14, 8.34,\
8.54, 8.75, 8.95, 9.15, 9.36, 9.56,\
9.76, 9.97, 10.17, 10.37, 10.58, 10.78,\
10.98, 11.19, 11.39, 11.59, 11.8, 12.])
Y = np.array([11.89, 14.94, 16.17, 15.11, 17.19, 16.28,\
13.9, 15.16, 14.96, 15.25, 17.79, 19.24,\
17.77, 18.61, 17.06, 13.55, 11.15, 8.88,\
6.28, 7.12, 7.77, 7.1, 10.34, 11.08,\
9.43, 10.74, 10.22, 8.22, 9.43, 10.08,\
10.39, 12.99, 14.99, 15.25, 16.72, 16.89,\
14.57, 14.72, 15., 14.19, 16.46, 17.71,\
17.47, 19.5, 18.99, 15.62, 14.44, 11.4,\
7.28, 7.63, 7.06, 6.81, 8.4, 9.83,\
9.44, 11.59, 10.33, 8.11, 9.47, 8.94])
# Use this cell to plot the data
# Define the functions f0, f1, f2 and f3 in this cell
from math import sin # Needed for the functions
# Use this cell to create the A matrix
# First create four vectors Y0, Y1, Y2 and Y3
# Concatenate them to form Y
# Define G and H matrices in this cell
# Use the G and H matrices to do gaussian elimination and
# get the coefficients (the answer)
# Plot the original data points and the curve from your answer
# in this cell to see if your answer appears correct