Question: In the python file Pmath 0 4 b . py , 2 D bicubic interpolation is done with the assumption that both x and y

In the python file
Pmath04b.py,2D bicubic interpolation is done with the assumption that both x and y are =
0,1,2,3. The code does not work if the unknown point is outside the 0,3 domain. Please write a bicubic
interpolation function (without using derivatives) similar to the function myinterp_bilin( x,y,z,p in this file.
That is, the user can specify the x and y vectors with the corresponding z=f(x,y). And the function value of the
new point p can be found if p is within the specified x,y domain.
*Importance* I need the complete code!!! And please check the code is correct : when print(myinterp_bilin(x,y,z,[1,2])), it should output "-0.93532843"
---------------------------------------
import numpy as np
from scipy import interpolate as interp #.interpolate import interp2d
x = np.arange(-5.01,5.01,0.25)
y = np.arange(-5.01,5.01,0.25)
xx, yy = np.meshgrid(x, y)
z = np.sin(xx**2+yy**2) #based on length of x, y, construct matrix z (size: len(x) by len(y))
# finterp = interp.interp2d(x, y, z, kind='cubic')
finterpL = interp.interp2d(x, y, z, kind='linear')
print('Use scipy interpolate interp2d function, linearly interpolated value =',finterpL(1,2))
def myinterp_bilin(x,y,z,p):#p is the new point; x and y are grid points; z=f(x,y)
xn=p[0]; yn=p[1]
if xnx[-1] or yny[-1]:
print('Out of range!')
return 0
else:
for i in range(1,len(x)):#find the x position of the new point
if x[i]>xn:
indx=i
break
for i in range(1,len(y)):#find the y position of the new point
if y[i]>yn:
indy=i
break
x1=x[indx-1]; x2=x[indx];
y1=y[indy-1]; y2=y[indy];
f11=z[indx-1][indy-1];
f12=z[indx-1][indy];
f21=z[indx][indy-1];
f22=z[indx][indy];
den=(x1-x2)*(y1-y2);
f=(xn-x2)*(yn-y2)*f11/den #((x1-x2)*(y1-y2))
f=f+(xn-x2)*(yn-y1)*f12/(-den)#((x1-x2)*(y2-y1))
f=f+(xn-x1)*(yn-y2)*f21/(-den)#((x2-x1)*(y1-y2))
f=f+(xn-x1)*(yn-y1)*f22/(den)#((x2-x1)*(y2-y1))
#print('x1y1x2y2',x1,y1,x2,y2)
return f
print('Use myinterp_bilin:',myinterp_bilin(x,y,z,[1,2]))
In the python file Pmath 0 4 b . py , 2 D bicubic

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 Accounting Questions!