How can I change a spherical 1D into cartesian 3D?

Hello I have a matlab problem I need some guidance on. I solved the diffusion equation with a reaction term using the so called finite difference method. If you're not familiar with this, don't worry. It basically gives me an answer (concentration, C) which varies in space and time. My solution right now is a 1D in radius because I am assuming a symmetric profile around a center. Long story short, I have C in a 101X10000 array. The 101 is for the incremental radius (dr) and 10000 for incremental time (dt). I want to now change this into a 3D cartesian space such that each point in the space has a value of C corresponding to its equivalent radial location. I also want to capture the time domain somehow, and visualize the whole thing in an animation. I don't know how to do this and was hoping for some guidance on how to proceed. Just a sentence or a line of code to guide me may suffice.
Thanks in advance!

Réponses (1)

Star Strider
Star Strider le 18 Mai 2016

0 votes

You emailed me about this, so I will at least follow up. The problem with your data is that you have incremental radius data and time data, but it’s not obvious to me (and apparently to everyone else) how you want this represented in polar or spherical coordinates.
See the documentation for the cart2pol and cart2sph functions, and experiment with them to see how you want to implement them with your data. You know your data better than we do, and you know how best you want them transformed.

8 commentaires

Thanks Star, appreciate the fast response. I will take a look at these functions. What I want to do is the reverse, perhaps I can somehow make it work. One way I have attempted to do this was to create an ndgrid, and calculate radius at each grid using r=sqrt(X^2+y^2+z^2), and then assign the corresponding concentration value from my 101X10000 array to that grid point. It is easier said than done (at least for me)
The reverse is (probably) as easy as what I described. See the pol2cart and sph2cart functions.
I don’t understand your array. That’s the reason I find it difficult to provide anything specific.
Star, let's call my matrix C. It is a 101X10000 matrix, whose elements represent concentration. The rows represent radial distance from a center and the columns represent time evolution. For example, the first row is for r=0 (center), the second row is for r=dr, 3rd row for r=2*dr etc...where dr=0.2mm. The first column is for t (time)=0, the second column is for t=dt, 3rd column for t=2*dt etc where dt = 0.001seconds (1ms). Each element of the matrix is the concentration at that radial location and time. Now I want to change this to cartesian x,y,z coordinates. Each coordinate will have a concentration value and the value changes with time. Does that mean I need an ND array, where N=5? (x,y,z,c,t)?...and more importantly, how do I do this? Disclosure: I have not looked at the sph2cart function yet but I just wanted to clarify my question. Sorry for the long post.
To use sph2cart and the others, you have to have your data in terms of the azimuth and elevation angles, and the radial distance. My guess is that you want to plot your data as distance and time in 3D space. It doesn’t seem to me that your data are in that form at present. You will likely need to transform them into a form the functions can work with.
I would first just plot your data using the mesh function:
figure(1)
meshc(C)
grid on
to see what it looks like. That will give you some idea of what you’re working with. If you want to save your ‘C’ matrix as a .mat file and upload it as an attachment to your original post (click on ‘Edit’ and then click on the ‘paperclip’ icon and be sure to do both the ‘Choose file’ and ‘Attach file’ steps).
That will give me the opportunity to experiment with it. I can’t promise results (what you want to do may not be possible), but I will see if I can transform it.
Hi Star, I tried your suggestion and it does help visualize the array for sure. I have also made a few 2D plots (for example, plot of concentration as a function of time for a radial location). I have attached the .mat file of the array. Take a look and let me know what you think.
You can get a better idea of your data by taking the log of it:
D = load('Katie Ciston Concentration.mat');
Mtx = D.uu1;
Range = [min(Mtx(:)) max(Mtx(:))];
figure(1)
meshc(Mtx+1)
grid on
set(gca, 'ZScale','log')
The minimum of your data are 0, so it’s necessary to add 1 before taking the log. I’m still not quite certain how you want to transform it, since I don’t have a good idea of what you want to do. You also have two ‘peaks’ in your data, and I’m not certain how to interpret them.
Hi Star, the peaks you observed come from the underlying physical problem. It just means concentration is high at that radial distance and point in time.
I was hoping to get the following lines to work to get the 1D radial profile into an ND array (x,y,z,concentration, time). I keep running into dimension errors.
[X,Y,Z]=ndgrid(1:57,1:57,1:57); uuu=reshape(uu1(floor(sqrt(X.^2+Y.^2+Z.^2))),58,58,58);
The ultimate objective is to simulate diffusion out of multiple point sources located inside a cubic simulation volume. The point sources will be located in specific grid points in the simulation volume and the concentration of a grid point in the volume will be the sum of the concentrations from these sources.
Thanks for the discussion! And I will let you know if/when I get this thing to work.
My pleasure.
I’m still not certain of what you want to do. For a spherical transformation, you need to define your array in terms of two angles and a radius, and I don’t understand how to do that with your data.
Your data are a radial distance and time, but to work with the 2D pol2cart function, time then becomes the angle. That’s easily enough done (scale your time dimension by 2*pi/max(time)), but is that what you want to do?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by