How to convert X, Y, Z cartesian coordinates into a surface.
15 views (last 30 days)
Show older comments
Hello,
I have three vectors representing x, y, and z cartesian coordinates of half of a nosecone shape. I need to convert the z data into a "matrix of z heights", as as described in the documentation for Grid Surfaces used in Simscape Multibody (https://www.mathworks.com/help/sm/ref/gridsurface.html#mw_e7b9ffe5-592d-4971-9715-092dba7fd93a).
I tried to use the diag() command and surf() to check things, but it isn't quite right; there are valleys between rows of points and some of the negative space outside the nosecone is also included. Additionally, Simscape grid surfaces need x and y vectors that are monotonic and increasing. I can sort these vectors in Matlab easily enough, I can't seem to get my z matrix quite right when I sort x and y.
Sample code below. The x_coords, y_coords, and z_coords are pulled in from a .mat file.
Thank you!
load('cone_data - Copy.mat');
x = x_coords(:);
y = y_coords(:);
z = z_coords(:);
z = diag(z);
fullset = [x y z];
surf(x, y, z);
4 Comments
Cris LaPierre
on 11 Apr 2024
Please upload the same data you are working with.
The mat file you uploaded does not appear to have the same variables your code uses.
load nosecone.mat
whos
Accepted Answer
Voss
on 12 Apr 2024
load('cone_data - Copy.mat');
Here's how the surface looks. This is using the matrices x_coords, y_coords, and z_coords.
% a surface based on x, y, and z matrices
figure
surf(x_coords,y_coords,z_coords)
I understand that for Simscape Multibody you need to specify the surface as a matrix z based on x and y being monotonically increasing vectors, instead of matrices. That constraint is the same as requiring the surface to have a footprint (i.e., its projection on the x-y plane) that is a rectangle. Obviously the surface above does not have a rectangular footprint; it's more like a filled-in parabola kind of shape. In order to define your surface on a rectangular footprint, you'll need to fill in some z values outside the parabola. Using zeros is a natural choice for those values, which can be done something like this:
% need to interpolate x/y/z_coords over some rectangular
% grid, so use a scatteredInterpolant
SI = scatteredInterpolant(x_coords(:),y_coords(:),z_coords(:));
% define the grid with some equally-spaced x values
x = linspace(min(x_coords(:)),max(x_coords(:)),25);
% and non-equally-spaced y values (because y_coords is already grid-like)
y = y_coords(:,1);
% interpolate to get z on that grid
[X,Y] = meshgrid(x,y);
Z = max(0,SI(X,Y));
% surface based on x, y vectors, and matrix z
figure
surf(x,y,Z);
Another choice might be NaNs (I have no idea what Simscape Multibody will do with NaN z values, but it's worth a shot, I guess), which can be done like this:
% using the same x-y grid as before, but
Z = SI(X,Y);
Z(Z < 0) = NaN;
% surface based on x, y vectors, and matrix z
figure
surf(x,y,Z);
I guess I would try the one with the zeros first.
4 Comments
More Answers (0)
See Also
Categories
Find more on Bodies in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!