# Fit surface to position and time data, convert to vectors?

1 view (last 30 days)
walkswithlooselaces on 19 Oct 2021
Commented: Image Analyst on 20 Oct 2021
I have an array of x,y,t data.
I would like to fit a surface then pull X and Y points from the fitted surface separately. I will then calculate velocity from these X and Y points.
I was planning to use either griddata or scatteredinterpolant.
I think the first step is to convert my data (below) into vectors.
x=[278.504000000000;259.694000000000;255.515000000000;245.587000000000;236.183000000000;191.772000000000]
y=[391.359000000000;356.875000000000;324.482000000000;275.891000000000;232.003000000000;134.300000000000]
t=[149;181;251;288;326;422]

Image Analyst on 19 Oct 2021
Try this:
% Demo to do surface estimation using a scattered interpolant.
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
x=[278.504000000000;259.694000000000;255.515000000000;245.587000000000;236.183000000000;191.772000000000]
y=[391.359000000000;356.875000000000;324.482000000000;275.891000000000;232.003000000000;134.300000000000]
t=[149;181;251;288;326;422]
subplot(1, 2, 1);
plot3(x, y, t, 'b.', 'MarkerSize', 20);
grid on;
title('Original Data', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('t', 'FontSize', fontSize);
%================================ MAIN PART RIGHT HERE ==============================================
% Create the scattered interpolant. x, y, and gray levels must be column vectors so use (:) to make them column vectors.
% And grayLevels must be double or else an error gets thrown.
F = scatteredInterpolant(x(:), y(:), t(:))
% The above line creates an interpolant that fits a surface of the form v = F(x,y).
% Vectors x and y specify the (x,y) coordinates of the sample points.
% v is a vector that contains the sample values associated with the points (x,y).
% Get a grid of points at every data location.
rows = numel(y)
columns = numel(x)
[xSorted, sortOrder] = sort(x, 'ascend')
ySorted = y(sortOrder)
[xGrid, yGrid] = meshgrid(xSorted, ySorted);
xq = xGrid(:);
yq = yGrid(:);
% Evaluate the interpolant at query locations (xq,yq).
vq = F(xq, yq);
interpolatedSurface = reshape(vq, rows, columns)
%================================ END OF MAIN PART ==============================================
% Show the estimated surface rendering.
subplot(1, 2, 2);
surf(xGrid, yGrid, interpolatedSurface);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('t', 'FontSize', fontSize);
caption = sprintf('scatteredInterpolant surface');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
uiwait(helpdlg('Done!'));
Of course you could interpolate in many more points to make the function smoother.
Image Analyst on 20 Oct 2021
Not analytically as far as I know. But you could numerically if you had a complete array. You could fill out the elements everywhere to get a 2-D image and then use imgradientxy().

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!