clear all
close all
n = 30;
disp ('1) equispaced X,Y, meshgridded XI,YI: should use bicubic')
x = linspace (0, 1, n);
y = linspace (0, 2, n)';
[X, Y] = meshgrid (x, y);
Z = sin (X) .* cos (Y);
XI = linspace(0, 1, 2 * n);
YI = linspace(0, 2, 3 * n)';
## ZI corresponds to meshgrid(XI,YI)
time = cputime;
ZI = newinterp2 (x, y, Z, XI, YI);
CPU_s = cputime - time
##mesh (XI, YI, ZI)
##xlabel('x')
##ylabel('y')
[XI, YI] = meshgrid (XI, YI);
error = norm (ZI(:) - sin (XI(:)) .* cos (YI(:)), Inf)
disp ('')
disp ('2) equispaced X,Y, scattered XI,YI: should use bicubic')
XI = rand (2, 10 * n);
YI = 2 * rand (2,10 * n);
time = cputime;
ZI = newinterp2 (x, y, Z, XI, YI);
CPU_s = cputime - time
##figure
##plot3(XI(:),YI(:),ZI(:),'*')
##xlabel('x')
##ylabel('y')
error = norm (ZI(:) - sin (XI(:)) .* cos (YI(:)), Inf)
disp ('')
disp ('3) not equispaced X,Y, meshgridded XI,YI: should use __splinen__')
x = -cos (linspace (0, pi, n));
y = 2 * sin (linspace (-pi / 2, pi / 2, 2 * n));
[X, Y] = meshgrid (x, y);
Z = sin (X) .* cos (Y);
XI = linspace (-1, 1, 2 * n);
YI = linspace(-1, 1,2 * n) .';
% ZI corresponds to meshgrid(XI,YI)
time = cputime;
ZI = newinterp2 (x, y, Z, XI, YI);
CPU_s = cputime - time
[XI, YI] = meshgrid (XI, YI);
##figure
##mesh(XI,YI,ZI)
##xlabel('x')
##ylabel('y')
error = norm (ZI(:) - sin (XI(:)) .* cos (YI(:)), Inf)
XI = 2 * rand(2, 10 * n) - 1;
YI = 4 * rand(2, 10 * n) - 2;
disp('')
disp('4) not equispaced X,Y, scattered XI,YI: should use spline')
time = cputime;
ZI = newinterp2 (x, y ,Z, XI, YI);
CPU_s = cputime - time
##figure
##plot3(XI(:),YI(:),ZI(:),'*')
##xlabel('x')
##ylabel('y')
error = norm (ZI(:) - sin(XI(:)) .* cos(YI(:)), Inf)
disp ('')
disp ('5) equispaced X,Y, meshgridded XI,YI, spline forced')
x = linspace (0, 1, n);
y = linspace (0, 2, n)';
[X, Y] = meshgrid (x, y);
Z = sin (X) .* cos (Y);
XI = linspace (0, 1, 2 * n);
YI = linspace (0, 2, 3 * n)';
## ZI corresponds to meshgrid(XI,YI)
time = cputime;
ZI = newinterp2 (x, y, Z, XI, YI, "spline");
CPU_s = cputime - time
##mesh (XI, YI, ZI)
##xlabel('x')
##ylabel('y')
[XI,YI] = meshgrid (XI, YI);
error = norm (ZI(:) - sin (XI(:)) .* cos (YI(:)), Inf)
disp('compare with 1)')
disp ('') 
disp ('6) equispaced X,Y, scattered XI,YI, spline forced')
XI = rand (2, 10 * n);
YI = 2 * rand (2, 10 * n);
time = cputime;
ZI = newinterp2 (x, y, Z, XI, YI, "spline");
CPU_s = cputime - time
##figure
##plot3(XI(:),YI(:),ZI(:),'*')
##xlabel('x')
##ylabel('y')
error = norm (ZI(:) - sin (XI(:)) .* cos (YI(:)), Inf)
disp ('compare with 2)')