You are on page 1of 3

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/364957487

RBF Interpolation using Pyhton

Experiment Findings · November 2022

CITATIONS READS

0 40

1 author:

Hamaidi Mohammed
Faculté des Sciences et Techniques de Tanger
7 PUBLICATIONS 42 CITATIONS

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

RBF Localized method and Applications View project

Python RBF Sofware View project

All content following this page was uploaded by Hamaidi Mohammed on 09 December 2022.

The user has requested enhancement of the downloaded file.


# 2D Interpolation using RBFs

import numpy as np
import time
import random
import matplotlib.pyplot as plt

RBF = lambda rx, ry, ep : np.sqrt( 1 + ep**2*(rx**2 + ry**2)) #MQ-RBF

exact = lambda x, y : np.sin(np.pi*(x + y)) * np.exp(x + y)

NX = NY = 19
N = (NX+2)*(NY+2) # Number of nodes N = (NX+2)*(NY+2)
ep = 1 # Paramter Shape

ax, bx = 0, 1
ay, by = 0, 1

print('----- Creating vectors x & y -----')


k=0
tt=time.time()
x=np.zeros([N])
y=np.zeros([N])
for i in range(NX+2):
for j in range(NY+2):
x[k]=(bx-ax)*i/(NX+1) + ax
y[k]=(by-ay)*j/(NY+1) + ay
k+=1
print('Using for loop: ',time.time()-tt,'seconds')

tt=time.time()
x = np.array([(bx-ax)*i/(NX+1) + ax for i in range(NX+2) for j in range(NY+2)])
y = np.array([(by-ay)*j/(NY+1) + ay for i in range(NX+2) for j in range(NY+2)])
print('Using comprehensible lists:',time.time()-tt,'seconds')

tt=time.time()
xx = np.linspace(ax,bx,NX+2)
x = (xx*np.ones([NX+2,NY+2])).T.reshape(N)
yy = np.linspace(ay,by,NX+2)
y = (yy*np.ones([NX+2,NY+2])).reshape(N)
print('Using linspace & vectors: ',time.time()-tt,'seconds')

B = exact(x,y)
print('----- Distance Matrix -----')
tt=time.time()
rx = np.abs([[x[i]-x[j] for j in range(N)] for i in range(N)])
ry = np.abs([[y[i]-y[j] for j in range(N)] for i in range(N)])
print('Using comprehensible lists: ',time.time()-tt,'seconds')

tt=time.time()
rx = np.abs([x-x[j] for j in range(N)])
ry = np.abs([y-y[j] for j in range(N)])
print('Using reduced comprehensible lists:',time.time()-tt,'seconds')

tt=time.time()
o = np.ones([N,1])
rx = np.abs(x*o-(x*o).T)
ry = np.abs(y*o-(y*o).T)
print('Using vectorial multiplications: ',time.time()-tt,'seconds')

print('----- Determining coefficients -----')

1
View publication stats

tt = time.time()
P = RBF(rx, ry, ep)
A = np.linalg.solve(P, B)
print('Time for linear solver:',time.time()-tt,'seconds')
tt = time.time()
P = RBF(rx, ry, ep)
A = np.linalg.pinv(P).dot(B)
print('Time using pinv :',time.time()-tt,'seconds')
U_num = RBF(rx, ry, ep).dot(A)
print('-------- Errors --------------')
print('RMSE =', '%5.2e'%np.linalg.norm(B - U_num,2)) # RMS error
print('MAE =', '%5.2e'%np.max(abs(B - U_num))) # max erreor

# Test
print('----------Test----------')
NT = N//2 # Number of test nodes
x_test = np.array([(bx-ax)*random.random() + ax for i in range(NT)])
y_test = np.array([(by-ay)*random.random() + ay for i in range(NT)])
U_Exact = exact(x_test, y_test)

print('----- Distance Matrix for the test-----')


tt=time.time()
rx = np.abs([[x_test[i]-x[j] for j in range(N)] for i in range(NT)])
ry = np.abs([[y_test[i]-y[j] for j in range(N)] for i in range(NT)])
print('Using comprehensible lists: Test',time.time()-tt,'seconds')
tt=time.time()
rx = np.abs([x_test-x[j] for j in range(N)]).T
ry = np.abs([y_test-y[j] for j in range(N)]).T
print('Using comprehensible lists: Reduced',time.time()-tt,'seconds')

U_num = RBF(rx, ry, ep).dot(A)


print('-------- Errors --------------')
print('RMSE =', '%5.2e'%np.linalg.norm(U_Exact-U_num,2)) # RMS error
print('MAE =', '%5.2e'%np.max(abs(U_Exact - U_num))) # max erreor

plt.plot(x, y,'r*')
plt.plot(x_test, y_test,'+')
plt.show()

You might also like