Due Date: Tuesday after Thanksgiving
What to submit:
interpolation.py for problem 1.curvefit.py for problem 2Where to submit:
In class, we derived a linear system of equations for finding the coefficients of an order-$n$ polynomial that interpolates between (i.e., passes through) $n+1$ points.

Instructions
You will submit a single Python file for this problem. Put your import statements and function definitions at the top of the file, and write the rest of your code underneath. The graders should be able to run your file as-is.
Write a Python function that takes as input an array of x-values and an array of y-values, representing ordered pairs $(x_i,y_i)$, and returns (1) the system matrix, and (2) the right-hand-side vector, from the equations shown above.
def interpolation_system(x_values,y_values):
# x_values and y_values should be 1-dimensional arrays of the same size.
num = len(x_values)
A = np.zeros((num,num))
b = np.zeros(num)
return A,b
Download the text file named interpolate_these.txt from the Resources page. Write Python code that
numpy.loadtxt()numpy arrays, named, e.g., x_data and y_datainterpolation_system from above and stores the matrix and right-hand side.numpy function numpy.linalg.solve to solve the system of equations $Ax=b$, where $A$ is the matrix from interpolation_system and $b$ is the right-hand side from interpolation_system. Here, $x$, the unknown vector, contains the coefficients of our sought-for polynomial, ${ a_0, a_1, a_2, …, a_n }$.Now that you have found coefficients ${ a_0, a_1, a_2, …, a_n }$ that define the $n$-degree polynomial that interpolates between these points, write a new function that evaluates this polynomial for any $x$.
Recall that the mathematical form of this interpolating function is \(f(x; a_0, a_1, ..., a_n) = a_0 + a_1 x + a_2 x^2 + a_3 x^3 + ...\)
Your task is to write a Python function that evaluates this interpolating function at any value of $x$, for any given set of coefficients ${ a_0, a_1, a_2, …, a_n }$.
def interpolating_polynomial(x,array_a):
# x can be any real number (NOT an array)
# array_a will be an array containing the coefficients
# of the order-n polynomial: a_0, a_1, a_2, etc.
# the function should return
# a_0*x^0 + a_1*x^1 + a_2*x^2 + ... a_n*x^m
return 0.0
Note: Note that numpy already contains a function that does essentially this; it’s called numpy.polyval. You may choose to implement this task as a ‘wrapper’ around polyval. It should be straightforward to write your own code without using polyval, too.
Also Note: This function will be generic; it won’t contain any information from problem 1.1 or problem 1.2. Later, when you call the function, you’ll provide information about what exactly the coefficients are — and they will be the ones you found in problem 1.2.
Generate a plot that shows the data points together with the interpolating curve. The following snippets of code will help you with this.
import numpy as np
import matplotlib.pyplot as plt
# Assuming x_vals, y_vals contains the coordinates
# of the data points:
plt.scatter(x_vals,y_vals)
# Assuming you have solved 1.2 and you have an array
# of coefficients [a_0, a_1, a_2, ..., a_n] called "a"
# found after solving the linear system in problem 1.2:
# Generate 100 x-values between the first and last coordinate
x_continuous = np.linspace(x_vals[0],x_vals[-1],100)
# Evaluate your interpolating function at all 100 points
y_continuous = interpolating_polynomial(x_continuous,a)
# Make a line plot
plt.plot(x_continuous,y_continuous,label='Interpolating func')
plt.legend()
# Save it
plt.savefig("interpolation.png")
# Show the plot
plt.show()
Your plot should look substantially similar to the one below.

In class, we saw that the linear system for finding a best-fit curve takes the following form:

Instructions
You will submit a single Python file for this problem. Put your import statements and function definitions at the top of the file, and write the rest of your code underneath. The graders should be able to run your file as-is.
Complete the activity from class, i.e., write a Python function that takes as input an array of x values, an array of y values, and an integer $m$, where $m$ is the order of the polynomial that is to be fit to the data. The function should return a matrix of size $(m+1) \times (m+1)$ and an array of size $m+1$.
def curvefit_system(x_values,y_values,m):
# x_values and y_values should be 1-dimensional arrays of the same size.
A = np.zeros((m+1,m+1))
b = np.zeros(m+1)
return A,b
Download the text file that was introduced in class. Write Python code that
numpy.loadtxt()numpy arrays, named, e.g., x_data and y_datacurvefit_system from above, with m=2, and stores the matrix and right-hand side.numpy function numpy.linalg.solve to solve the system of equations $Ax=b$, where $A$ is the matrix from curvefit_system and $b$ is the right-hand side from curvefit_system. Here, $x$, the unknown vector, contains the coefficients of our sought-for polynomial, ${ a_0, a_1, a_2, …, a_m }$.Now that you have found coefficients ${ a_0, a_1, a_2, …, a_m }$ that define the $m$-degree polynomial that best fits these points, write a new function that evaluates this polynomial for any $x$.
Recall that the mathematical form of this polynomial function is \(f(x; a_0, a_1, ..., a_m) = a_0 + a_1 x + a_2 x^2 + a_3 x^3 + ...\)
Your task is to write a Python function that evaluates this function at any value of $x$, for any given set of coefficients ${ a_0, a_1, a_2, …, a_m }$.
def curvefit_polynomial(x,array_a):
# x can be any real number (NOT an array)
# array_a will be an array containing the coefficients
# of the order-m polynomial: a_0, a_1, a_2, etc.
# the function should return
# a_0*x^0 + a_1*x^1 + a_2*x^2 + ... a_m*x^m
return 0.0
Note: numpy already contains a function that does essentially this; it’s called numpy.polyval. You may choose to implement this task as a ‘wrapper’ around polyval. It should be straightforward to write your own code without using polyval, too.
Note: This function will be generic; it won’t contain any information from problem 2.1 or problem 2.2. Later, when you call the function, you’ll provide information about what exactly the coefficients are — and they will be the ones you found in problem 2.2.
Generate a plot that shows the data points together with a quadratic best-fit curve. The following snippets of code will help you with this.
import numpy as np
import matplotlib.pyplot as plt
# Assuming x_vals, y_vals contains the coordinates
# of the data points:
plt.scatter(x_vals,y_vals)
# Assuming you have solved 2.2 and you have an array
# of coefficients [a_0, a_1, a_2, ..., a_m] called "a"
# found after solving the linear system in problem 2.2:
# Generate 100 x-values between the first and last coordinate
x_continuous = np.linspace(x_vals[0],x_vals[-1],100)
# Evaluate your interpolating function at all 100 points
y_continuous = curvefit_polynomial(x_continuous,a)
# Make a line plot
plt.plot(x_continuous,y_continuous,label='Best Fit')
plt.legend()
# Save it
plt.savefig("bestfit.png")
# Show the plot
plt.show()
Your plot should look substantially similar to the one below.

Note: This problem is to be turned in as a PDF, NOT as code.
Find the values of coefficients ${a,b,c,d}$ such that the function \(f(x) = a + b e^{x} + cx^2 + d \sin x\) passes through the points
| x | y |
|---|---|
| 0.0 | 0.2 |
| 1.0 | 2.0 |
| 2.0 | 1.2 |
| 3.0 | 3.0 |
using the following procedure
numpy.linalg.solve, or you can use Gaussian elimination, etc.Turn in all of your work as part of the PDF submission for this HW.
Note: To get full credit, your values of a, b, c and d must be correct. You can check whether they are by plotting $f(x)$ and ensuring it passes through the points given.
Download the files data1.csv and data2.csv, as well as the Python files newtonPoly.py and polyFit.py.
newtonPoly.py uses Newton’s polynomial to determine the (unique) interpolating polynomial that connects a set of points together. Newton’s polynomial is a technique for interpolating between points with a polynomial curve. Since polynomial interpolation is typically unique, Newton’s method of finding a polynomial is equivalent to the method that you used in problem 1. You are provided all of the code needed to use Newton’s method for interpolation, so you should not need to write much code for problem 4. You can read about Newton’s polynomial interpolation technique here.polyFit.py implements a curve-fitting procedure using ap olynomial of a specified degree.Your task is to perform polynomial interpolation as well as polynomial curve-fitting on the two given sets of data.
Note: No modifications whatsoever are needed in newtonPoly.py or polyFit.py.
Note: You will not turn in the code for this problem.
Make a single plot that contains:
Use the following code to get started.
# Import functions. Make sure that your downloaded files are in the present working directory.
import numpy as np
from newtonPoly import coeffts, evalPoly
from polyFit import polyFit, evalPolyFit
import matplotlib.pyplot as plt
x1 = np.loadtxt('data1.csv')[:,0]
y1 = np.loadtxt('data1.csv')[:,1]
# Create x-axis to plot
xs = np.linspace(x1[0],x1[-1],100)
# Find coefficients of Newton's Polynomial (interpolation)
a = coeffts(x1,y1)
# Evaluate Newton's polynomial at values 'xs'
y_interp = evalPoly(a,x1,xs)
# Curve fit using polynomials of different degrees.
b1 = polyFit(x1,y1,1)
b2 = polyFit(x1,y1,2)
# Evaluate curve-fit at values 'xs'
y_curvefit1 = evalPolyFit(xs,y1,b1)
y_curvefit2 = evalPolyFit(xs,y1,b2)
# Make plot
plt.plot(x1,y1,'o',label='data',color='black')
plt.plot(xs,y_interp,'-',label='Interpolation',color='black')
plt.plot(xs,y_curvefit1,'r--',label='Fit, m = 1')
plt.plot(xs,y_curvefit2,'b--',label='Fit, m = 2')
plt.legend()
plt.show()
Turn in your resulting plot.
Make a single plot that contains:
Use the following code to get started:
# Import functions. Make sure that your downloaded files are in the present working directory.
import numpy as np
from newtonPoly import coeffts, evalPoly
from polyFit import polyFit, evalPolyFit
import matplotlib.pyplot as plt
x1 = np.loadtxt('data2.csv')[:,0]
y1 = np.loadtxt('data2.csv')[:,1]
# Create x-axis to plot
xs = np.linspace(x1[0],x1[-1],100)
# Newton Polynomial (interpolation)
a = coeffts(x1,y1)
# Evaluate Newton's polynomial at values 'xs'
y_interp = evalPoly(a,x1,xs)
# Curve fit with order m = 1
b1 = polyFit(x1,y1,1)
# Evaluate curve-fit at values 'xs'
y_curvefit1 = evalPolyFit(xs,y1,b1)
# Make plot
plt.plot(x1,y1,'o',label='data',color='black')
plt.plot(xs,y_interp,'-',label='Interpolation',color='black')
plt.plot(xs,y_curvefit1,'r--',label='Fit, m = 1')
plt.legend()
plt.show()
Turn in your resulting plot.
Explain which curve fit, out of the ones you have plotted, you would choose to represent each of these datasets. You should write a short paragraph each for data set 1 and data set 2.