Hi,
I am trying to build a 2-D bilinear interpolation function as shown below. While using the profiler, I noticed that the maximum computation time is spent in finding upper and lower bound
temp = x(i,j) <= X;[idx1, ~] = find(temp, 1);
x , y are scalars
and X, Y, V are gridded data with equal size of (m, n).
My aim is to achieve better computational performance than using the native griddedinterpolant in Matlab
V_fit = griddedInterpolant(X, Y, V, 'linear' )v = V_fit (x, y)
At the moment, griddedinterpolant is 10 times faster than my user defined function.
Is there a better way to calculate the upper and lower bounds? Possibly, that works also when x , y are matrix of size (i,j).
function [v] = interp2D(X, Y, V, x, y)% Calculate lower bound in x direction
temp = x <= X;[idx1, ~] = find(temp, 1);% Calculate upper bound in x direction
temp = x > X;[idx2, ~] = find(temp, 1, 'last');% Calculate lower bound in y direction
temp = y <= Y;[~, idy1] = find(temp, 1);% Calculate upper bound in y direction
temp = y > Y;[~ , idy2] = find(temp, 1, 'last');% Evaluate the function at four points
V11 = V(idx1 , idy1);V12 = V(idx1 , idy2);V21 = V(idx2 , idy1);V22 = V(idx2 , idy2);% Interpolate in x-direction
Vx1 = (X(idx2 , 1) - x) * V11 / ( X(idx2 , 1) - X(idx1 , 1)) + ... (x - X(idx1 , 1)) * V21 / ( X(idx2, 1) - X(idx1, 1)); Vx2 = (X(idx2, 1) - x) * V12 / ( X(idx2, 1) - X(idx1, 1)) + ... (x - X(idx1, 1)) * V22 / ( X(idx2, 1) - X(idx1, 1)); % Interpolate in y-direction
v = (Y(1, idy2) - y) * Vx1 / ( Y(1 , idy2) - Y(1, idy1)) + (y - Y(1, idy1)) * Vx2 / ( Y(1, idy2) - Y(1, idy1));end
Edit: In my case, m = 181, n = 181. And, while comparing computational time, I assume that griddedInterpolant(X, Y, V, 'linear' ) is performed before the simulation is run i.e. I compare the time of v = V_fit (x, y) with the execution time of my code.
Best Answer