Interpolate Polynomials in Python 3! Compute: Generating Polynomial Interpolants
ID: 3705055 • Letter: I
Question
Interpolate Polynomials in Python 3!
Compute: Generating Polynomial Interpolants 10 points For this problem, interpolate f)2 1252 with Pn (z), where pn (z) has the form: Construct pn (z) for n 3, 5, 15. For nodes of interpolation, use evenly spaced points from -1 to 1 (including the end-points). Setup the corresponding Vandermonde matrix and solve for the coefficients c of the nterpolant. You should return the coefficients in three different 1d numpy arrays namely coefficients 3, coefficients 5 and coefficients 15 corresponding to the degree of the interpolant, the ordering should be such that the coefficient c corresponds to the i-th index of the array. Plot the exact function (f(x)) and the interpolants in a single figure. Use numpy.polyval and numpy. linspace to generate 50 evenly spaced points for plotting the interpolants and the exact function. Include a title and a legend. NOTE When you use numpy.polyval, you will need to reverse the order of your coefficients. You can use numpy.linalg. solve() to get the solutions for the linear systems .You are NO T allowed to use numpy.polyfit(), numpy.vander() or any other library functions that constructs the system or gets the interpolants. OUTPUT .coefficients_3: 1d numpy array .coefficients 5: 1d numpy array coefficients 15 1d numpy array A plot containing the interpolants and f()Explanation / Answer
import scipy.interpolate as sp
import numpy
import pylab
# 50 points of sin(x) in [0 10]
xx = numpy.linspace(3, 5, 15)
yy = numpy.sin(xx)
# 10 sample of sin(x) in [0 10]
x = numpy.linspace(3, 5, 15)
y = numpy.sin(x)
# interpolation
fl = sp.interp1d(x, y,kind='linear')
fc = sp.interp1d(x, y,kind='cubic')
# fl and fc are the interpolating functions
# defined in the interval [0 10]
# fl uses linear interpolation
# and fc uses cubic interpolation
xnew = numpy.linspace(3, 5, 15)
pylab.subplot(211)
# the real sin(x) function plot
pylab.plot(xx, yy)
pylab.legend(['sin(x)'], loc='best')
pylab.subplot(212)
# the interpolation
pylab.plot(x, y, 'o', xnew, fl(xnew), xnew, fc(xnew))
pylab.legend(['sample', 'linear', 'cubic'], loc='lower left')
pylab.show()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.