How to extract a 2D surface from a 3D data set?

11 visualizaciones (últimos 30 días)
Donovan Allum
Donovan Allum el 12 de Jul. de 2023
Editada: Donovan Allum el 30 de Jul. de 2023
I have a 3D array, T, which has (Nx,Ny,Nz) elements. It contains the temperature of some fluid dynamical system.
I know ahead of time that there is some cylindrical symmetry in the data. As a result, there is some utility in extracting a cylindrical shell surface from the data, which could be contained in a single 2D array (only variables are theta - azimuth - and z).
Currently, I do this using a large loop (code at the bottom). This is obviously quite slow since I am interpolating at every z slice. Is there a way to do this without several loops? Perhaps using interp3?
One idea I had was to try
xcoord = rnow.*sin(theta);
ycoord = rnow.*sin(theta);
z1d = Z(1,1,:);
tprof = interp3(X,Y,Z,tnow,xcoord,ycoord,z1d,'spline');
The problem would be that if this worked, then tprof is just a different 3D array on the new grid. Essentially a cylindrical shell in cartesian coordinates. If this works, how can I convert it to cylindrical coordinates and then remove the radial component (since it would be contant with r)?
num_points = 1000;
%Dimensions
Lx = 1;
Ly = 2;
Lz = 1.2;
%Number of grid points
Nx = 50;
Ny = 100;
Nz = 20;
%Calculate x,y,z grid
x1d = linspace(0,Lx,Nx);
y1d = linspace(0,Ly,Ny);
z1d = linspace(0,Lz,Nz);
[X,Y,Z] = meshgrid(x1d,y1d,z1d);
%Example field
Data = X.^3+2.*Y-Z;
%Azimuthal angle in polar coordinates
%Origin is at Data(1,1,1)
theta = linspace(0,pi/2,num_points);
rnow = 1;
for AzimuthalPoint = 2:num_points
% Calculate the angle along the arc of r = rnow
% Calculate the corresponding y-coordinate
ycoord = rnow * sin(theta(AzimuthalPoint));
xcoord = rnow * cos(theta(AzimuthalPoint));
for VertPoint = 1:Nz
% Find the temperature at the radius r = rnow by interpolating tnow1d
% using spline interpolation
tprof(AzimuthalPoint,VertPoint) = interp2(x1d, y1d, Data(:,:,VertPoint), xcoord, ycoord, 'spline');
end
end

Respuestas (1)

Supraja
Supraja el 27 de Jul. de 2023
I understand that you want to convert your coordinates to cylindrical then remove the radial component from it.
To convert the coordinates from cylindrical to 2 D matrix by removing the radial component, you can use the “squeeze” function in MATLAB. The detailed documentation link for the function is attached is here: https://www.mathworks.com/help/matlab/ref/squeeze.html
For the initial part of conversion of coordinates to cylindrical system, you can mathematical equations.
Hope this works!
  1 comentario
Donovan Allum
Donovan Allum el 27 de Jul. de 2023
Editada: Donovan Allum el 30 de Jul. de 2023
Unfortunately this does not work. But I did manage to figure out it out, I think. I used griddedInterpolant and it appeared to work, at least to two dimensions. I was able to interpolate from a 2D set of points to a 1D vector, which is essentially what I was trying to do in my question. Thanks for the help anyway!
This is just from memory as I am adding this a few days later but it was something like this.
load('Data.mat','Data','x','y','z') %The 3D data I started with, with grid vectors
[X,Y,Z] = meshgrid(x,y,z);
r = 1; %radius of cylindrical shell
theta = linspace(0,pi/2,1000); %azimuthal angle passing from theta = 0 (where y=0) to theta = pi/2 (where x=0)
F = griddedInterpolant(X,Y,Z,Data);
for ii=1:length(z)
NewData(:,ii) = F(r.*cos(theta),r.*sin(theta),z(ii));
end
%New data is a profile along the arc at radius r from the origin (x,y,z) = (0,0,0)

Iniciar sesión para comentar.

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by