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.
--------------------------------
Pmath04b.py :
# -*- coding: utf-8-*-
"""
2-dimensional interpolation. Based on the book:Numerical methods for engineers, 7th, by Chapra
(page 522-523)
"""
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 f(X):
x=X[0]; y=X[1];
#return np.sin(x)*np.sin(x)+ np.sin(x)*np.cos(y)+1#x*x + x*y +2*y*y +2
return x**2+y+1 #np.sin(np.sqrt(x**2+ y**2))
# xpoint=[1,2]
# print('f=',f(xpoint))
def myinterp_bilin_4ptval(V,p,X):#p is the new point; X contains the coord of [x1,y1] and [x2,y2]
#V contains f values at [x1,y1],[x1,y2],[x2,y1],[x2,y2](the 4 corners)
x=p[0]; y=p[1]; # x and y coordinate of the new point
p1=X[0]; p2=X[1];
x1=p1[0]; y1=p1[1];
x2=p2[0]; y2=p2[1];
f11=V[0]; f12=V[1];
f21=V[2]; f22=V[3];
den=(x1-x2)*(y1-y2);
f=(x-x2)*(y-y2)*f11/den #((x1-x2)*(y1-y2))
f=f+(x-x2)*(y-y1)*f12/(-den)#((x1-x2)*(y2-y1))
f=f+(x-x1)*(y-y2)*f21/(-den)#((x2-x1)*(y1-y2))
f=f+(x-x1)*(y-y1)*f22/(den)#((x2-x1)*(y2-y1))
#print('x1y1x2y2',x1,y1,x2,y2)
return f
print('Example from the book by Chapra. Function values at 4 points are known:')
print('f(2,1)=69; f(2,6)=55; f(9,1)=57.5; f(9,6)=70;')
newp=[5.25,4.8]
V=[60,55,57.5,70]
Xcoord=[[2,1],[9,6]]
print('At the point',newp,', f=',myinterp_bilin_4ptval(V,newp,Xcoord))
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]))
# xmat=[[1,3],[5,4]]#the 1st row is [x1,y1]. The 2nd row is [x2,y2]
# xnew=[3,3.5]
# print('ff=',myinterp_bilin(f,xnew, xmat))
def mydot(a,b):
na=len(a)
nb=len(b)
f=0
if na!=nb:
print('Error! The two input vectors should have the same length.')
else:
for i in range(0,na):
f=f+a[i]*b[i]
return f
def p(x,y): # p(x,y)= sigma_i sigma_j x**i y**j
n=4
c=np.zeros([n,n])
for i in range(n):
for j in range(n):
c[i][j]= c[i][j]+ y**j
c[i]=c[i]*x**i
return c

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!