Question: def nonuniform _ poly _ interpolation ( a , b , p , n , x , f , produce _ fig ) : #Remove
def nonuniformpolyinterpolationabpnxfproducefig:
#Remove the following two lines when you have completed the code
#interpolant None
#fig None
xhat npcos nparangep p nppi
# Map nodal points to the interval a b
xhat b a xhat a b
# Call lagrangepoly with tol e
lagrangematrix, errorflag lagrangepolyp xhat, n xe
if errorflag :
printError: Nodal points are not distinct."
return None, None
# Calculate interpolant
interpolant npdotfxhat lagrangematrix
if producefig:
# Plot the function f and the interpolant
fig, ax pltsubplots
axplotx fx labelfx
axplotxhat fxhatro label'Nonuniform Nodes'
axplotx interpolant, labelf'Interpolant pp
axlegend
pltshow
else:
fig None
return interpolant, fig
def piecewiseinterpolationa b p m n x f producefig:
fig pltfigure
subintervals nplinspacea b m
# Arrays to store the piecewise interpolant values
puinterpolant npzerosn
pnuinterpolant npzerosn
for i in rangem:
asub subintervalsi
bsub subintervalsi
# Extract the points in the current subinterval
xsub xx asub & x bsub
# Uniform interpolation
puinterpsub, uniformpolyinterpolationasub, bsub, p lenxsub xsub, f False
puinterpolantx asub & x bsub puinterpsub
# Nonuniform interpolation
pnuinterpsub, nonuniformpolyinterpolationasub, bsub, p lenxsub xsub, f False
pnuinterpolantx asub & x bsub pnuinterpsub
# Plot if required
fig None
if producefig:
fig, ax pltsubplots
axplotx fxk label'Original function'
axplotx puinterpolant, r label'Uniform interpolant'
axplotx pnuinterpolant, b label'Nonuniform interpolant'
axlegend
pltshow
return puinterpolant, pnuinterpolant, fig
For pnuinterpolant and pnuinterpolant it should be
instead i am getting
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
