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

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(xhat, f(xhat),'ro', label='Nonuniform Nodes')
ax.plot(x, interpolant, label=f'Interpolant (p={p})')
ax.legend()
plt.show()
else:
fig = None
return interpolant, fig
def piecewise_interpolation(a, b, p, m, n, x, f, produce_fig):
fig = plt.figure()
subintervals = np.linspace(a, b, m+1)
# Arrays to store the piecewise interpolant values
p_u_interpolant = np.zeros(n)
p_nu_interpolant = np.zeros(n)
for i in range(m):
a_sub = subintervals[i]
b_sub = subintervals[i+1]
# Extract the points in the current subinterval
x_sub = x[(x >= a_sub) & (x <= b_sub)]
# Uniform interpolation
p_u_interp_sub, _= uniform_poly_interpolation(a_sub, b_sub, p, len(x_sub), x_sub, f, False)
p_u_interpolant[(x >= a_sub) & (x <= b_sub)]= p_u_interp_sub
# Nonuniform interpolation
p_nu_interp_sub, _= nonuniform_poly_interpolation(a_sub, b_sub, p, len(x_sub), x_sub, f, False)
p_nu_interpolant[(x >= a_sub) & (x <= b_sub)]= p_nu_interp_sub
# Plot if required
fig = None
if produce_fig:
fig, ax = plt.subplots()
ax.plot(x, f(x),'k-', label='Original function')
ax.plot(x, p_u_interpolant, 'r--', label='Uniform interpolant')
ax.plot(x, p_nu_interpolant, 'b-.', label='Non-uniform interpolant')
ax.legend()
plt.show()
return p_u_interpolant, p_nu_interpolant, fig
For p_nu_interpolant1 and p_nu_interpolant2 it should be [0.047103130.10.308394660.92677670.308394660.10.04710313][0.04705882010.307692311.0.307692310.10.04705882]
instead i am getting
[0.047105390.099859980.308470210.941176470.308470210.09874062
0.04710539]
[0.047058820.10.307692311.0.307692310.09999998
0.04705882]

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!