Question: def uniform _ poly _ interpolation ( a , b , p , n , x , f , produce _ fig ) : ###

def uniform_poly_interpolation(a,b,p,n,x,f,produce_fig):
### Example of creating a figure object
fig = plt.figure() # This line is required
#plt.plot([0,1],[2,3]) # Delete this line and replace
#Remove the following line when you have completed the code
#interpolant = None
xhat = np.linspace(a, b, p +1)
# Call lagrange_poly with tol =1.0e-10
lagrange_matrix, error_flag = lagrange_poly(p, xhat, n, x,1.0e-10)
if error_flag ==1:
print("Error: Nodal points are not distinct.")
return None, None
# Calculate interpolant
interpolant = np.dot(f(xhat), lagrange_matrix)
if produce_fig:
# Plot the function f and the interpolant
fig, ax = plt.subplots()
ax.plot(x, f(x), label='f(x)')
ax.plot(x, interpolant, label=f'Interpolant (p={p})')
ax.legend()
plt.show()
else:
fig = None
return interpolant, fig
#%%
def nonuniform_poly_interpolation(a,b,p,n,x,f,produce_fig):
#Remove the following two lines when you have completed the code
#interpolant = None
#fig = None
xhat = np.cos((2* np.arange(p +1)+1)/(2*(p +1))* np.pi)
# Map nodal points to the interval [a, b]
xhat =0.5*(b - a)* xhat +0.5*(a + b)
# Call lagrange_poly with tol =1.0e-10
lagrange_matrix, error_flag = lagrange_poly(p, xhat, n, x,1.0e-10)
if error_flag ==1:
print("Error: Nodal points are not distinct.")
return None, None
# Calculate interpolant
interpolant = np.dot(f(xhat), lagrange_matrix)
if produce_fig:
# Plot the function f and the interpolant
fig, ax = plt.subplots()
ax.plot(x, f(x), label='f(x)')
ax.plot(x, interpolant, label=f'Interpolant (p={p})')
ax.legend()
plt.show()
else:
fig = NonePiecewise Polynomial Interpolation
Recall, given a function f:[a,b]R, we can construct its piecewise polynomial
interpolant of order p by splitting a,b up into uniform subintervals and applying the
Lagrange interpolant of order p on each subinterval. Hence, using m subintervals
{[tilde(x)i-1,tilde(x)i]}i=1m, the piecewise interpolant Spm(x) satisfies:
Spm(x)|[tilde(x)i-1,tilde(x)i]=ppi(x),i=1,dots,m
where ppi(x) is the polynomial interpolant of f(x) on tilde(x)i-1,tilde(x)i.
In polynomial_interpolation.py you will find a function with definition
def piecewiseinterpolation(a,b,p,m,n,x,f,producefig)
The function should return as output:
p_u_interpolant, p_nu_interpolant - two numpy.ndarrays, each of
shape ,
return interpolant, fig
Need the python code
 def uniform_poly_interpolation(a,b,p,n,x,f,produce_fig): ### Example of creating a figure object fig =

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!