I have numerical data that corresponds to a particle moving in three dimensions, sampled at n equally spaced times t = [1,2,3,4,5,...], resulting in three coordinate arrays, e.g., for n=5:
x = [1,2,3,4,5]
y = [1,5,6,7,10]
z = [2,3,8,9,10]
I have been able to find a decent interpolation using cscvn, but I am now at a loss as to how I would find the x and y coordinates that correspond to a fixed value of z, i.e., a constant z slice.
CodePudding user response:
You can extract the individual cubics within the curve output by cscvn, then examine each one for if and where it crosses your chosen Z value within that piece's boundary.
x = randi(10,[1 5]);
y = randi(10,[1 5]);
z = randi(10,[1 5]);
zChosen = 5;
curve = cscvn([x;y;z]);
parametricCrossingPoints = struct('crossingPoints',[],'Xs',[],'Ys',[],'Zs',[]);
parametricCrossingPoints = repmat(parametricCrossingPoints,curve.pieces,1);
% for each peice of the parametric curve, find the points where it crosses
% your desired z
for i = 1:size(curve.coefs,1)/3
% Find where parametric piece crosses zChosen
pPoints = roots(curve.coefs(i*3,:)'-[0;0;0;zChosen]);
% Filter out imaginary roots
pPoints = real(pPoints(imag(pPoints) == 0));
% Filter for points within the piece's boundary
pPoints(pPoints < 0 | pPoints > curve.breaks(i 1)-curve.breaks(i)) = [];
% Append points to list
parametricCrossingPoints(i).crossingPoints = pPoints curve.breaks(i);
end
% Get x, y, and z of each crossing point
for i = 1:numel(parametricCrossingPoints)
xyzOfCrossingPoints = fnval(curve,parametricCrossingPoints(i).crossingPoints);
parametricCrossingPoints(i).Xs = xyzOfCrossingPoints(1,:);
parametricCrossingPoints(i).Ys = xyzOfCrossingPoints(2,:);
parametricCrossingPoints(i).Zs = xyzOfCrossingPoints(3,:); % Should all equal zChosen
end
% Collate points
Xs = [parametricCrossingPoints.Xs];
Ys = [parametricCrossingPoints.Ys];
Zs = [parametricCrossingPoints.Zs];
% Sometimes breakpoints aren't chosen, add them manually if necessary
xsToAdd = x(z == zChosen);
ysToAdd = y(z == zChosen);
for i = 1:numel(xsToAdd)
if ~any(Ys(Xs == xsToAdd(i)) == ysToAdd(i))
Xs(end 1) = xsToAdd(i);
Ys(end 1) = ysToAdd(i);
Zs(end 1) = zChosen;
end
end
% Plot results
fnplt(curve);
hold on
plot3(x,y,z,'o')
patch([min(x)-1,max(x) 1,max(x) 1,min(x)-1],...
[min(y)-1,min(y)-1,max(y) 1,max(y) 1],...
[zChosen,zChosen,zChosen,zChosen], ...
'green',...
'FaceAlpha',0.5)
plot3(Xs,Ys,Zs,'*')
hold off
There's some funky calculation to do around the parametric independent parameter (which doesn't match your time = t) for each piece of the curve and the specified breakpoints, but other than that it's pretty straightforward.
CodePudding user response:
Have you tried analyzing it as a contour? I do not know how the interpolation is handled internal to MATLAB but if you take a look at the contour functions in MATLAB you can extract slices of a contour at a given Z coordinate. Take a look at contourf or contourc, contourslice, and related functions. Without knowing what physical process your data is representing I do not know if this representation would be meaningful, but that is where I would start.