% --- build point cloud (same as above, in same script session) ---
t = 0.4*pi : 0.02 : 0.6*pi;
x1 = cos(t)'; y1 = sin(t)' - 1.02;
x2 = x1; y2 = -y1;
x3 = linspace(-0.3, 0.3, 16)'; y3 = zeros(16,1);
x = [x1; x2; x3];
y = [y1; y2; y3];
z = x.^2 + y.^2;
% --- scatteredInterpolant ---
F = scatteredInterpolant(x, y, z);
% --- regularizeNd ---
xGrid = {linspace(min(x)-eps(min(x)), max(x)+eps(max(x)), 20), ...
linspace(min(y)-eps(min(y)), max(y)+eps(max(y)), 21)};
zGrid = regularizeNd([x,y], z, xGrid, [0.001, 0.005], 'linear');
F2 = griddedInterpolant(xGrid, zGrid, 'linear');
% --- evaluation grid ---
[xi, yi] = ndgrid(-0.3:0.02:0.3, -0.0688:0.01:0.0688);
zi_scattered = F(xi, yi);
zi_regularize = F2(xi, yi);
zi_true = xi.^2 + yi.^2;
zmin = min(zi_true(:));
zmax = max(zi_true(:));
figure('Color', 'w', 'Position', [100, 100, 1280, 1250])
tl = tiledlayout(3, 1, 'TileSpacing', 'compact', 'Padding', 'compact');
colormap(turbo(256))
nexttile
s1 = surf(xi, yi, zi_scattered, 'EdgeColor', [0.35, 0.35, 0.35], 'LineWidth', 0.2);
title('scatteredInterpolant')
xlabel('x'); ylabel('y'); zlabel('z')
caxis([zmin, zmax])
xlim([-0.3, 0.3]); ylim([-0.08, 0.08]); zlim([zmin, zmax])
view(-36, 26)
daspect([1, 0.55, 0.35])
set(gca, 'FontSize', 10, 'LineWidth', 0.8)
grid on; box on
nexttile
s2 = surf(xi, yi, zi_regularize, 'EdgeColor', [0.35, 0.35, 0.35], 'LineWidth', 0.2);
title('regularizeNd')
xlabel('x'); ylabel('y'); zlabel('z')
caxis([zmin, zmax])
xlim([-0.3, 0.3]); ylim([-0.08, 0.08]); zlim([zmin, zmax])
view(-36, 26)
daspect([1, 0.55, 0.35])
set(gca, 'FontSize', 10, 'LineWidth', 0.8)
grid on; box on
nexttile
s3 = surf(xi, yi, zi_true, 'EdgeColor', [0.35, 0.35, 0.35], 'LineWidth', 0.2);
title('True surface z = x² + y²')
xlabel('x'); ylabel('y'); zlabel('z')
caxis([zmin, zmax])
xlim([-0.3, 0.3]); ylim([-0.08, 0.08]); zlim([zmin, zmax])
view(-36, 26)
daspect([1, 0.55, 0.35])
set(gca, 'FontSize', 10, 'LineWidth', 0.8)
grid on; box on
cb = colorbar;
cb.Layout.Tile = 'east';
cb.Label.String = 'z';