MATLAB Plot 3D Function: Point Where f=1, No Point Where f=0

AI Thread Summary
The discussion centers on plotting a 3D function of three variables, where the function takes values of 1 within a small polygon and 0 outside. Participants suggest using scatter graphs, but emphasize that a more intuitive approach might involve plotting slices through the data. The concept of a polygon is clarified as a 2D object, leading to the recommendation of using alphaShape to generate a polyhedron for visualization. To effectively plot points where the function equals 1, it is advised to filter the data using the FIND function after rounding the function values to avoid issues with floating-point comparisons. Overall, the conversation provides guidance on visualizing and filtering 3D data based on function values.
Ben Wilson
Messages
90
Reaction score
16
I had a function of 3 variables f'(kx,ky,kz)

and did an ifft

f = ifft(f')

and now i have a function where inside a small polygon the function f = 1, and outside f=0, within a cube space of length L.

how do i plot a scatter graph for this function where there is a point when f =1, and no point when f=0?
 
Physics news on Phys.org
You would have to consider if a scatter graph is the best way to represent this function.
How you plot the graph depends on what you want to be able to do with it.
You could see what is possible by plotting slices through the data by more intuitive approaches.
Usually you would color-code the dots for magnitude and the graph would look like an array.

Note: you cannot have 3D polygon... by definition a polygon is a 2D object.
So I think you would benefit from a more careful description.
 
Simon Bridge said:
You would have to consider if a scatter graph is the best way to represent this function.
How you plot the graph depends on what you want to be able to do with it.
You could see what is possible by plotting slices through the data by more intuitive approaches.
Usually you would color-code the dots for magnitude and the graph would look like an array.

Note: you cannot have 3D polygon... by definition a polygon is a 2D object.
So I think you would benefit from a more careful description.
a polyhedron sorry.

what i want to be able to do is see the polyhedron.
 
Can you show some more code?

I want to recommend that you use alphaShape to generate the polyhedron, but it sounds like you have 4-D data since the function values depend on x,y,z.

http://www.mathworks.com/help/matlab/ref/alphashape.html

If your data is really 4-D, then you could use delaunayn to generate an N-D triangulation. But you still wouldn't be able to plot the output unless you take 3-D slices.
 
kreil said:
Can you show some more code?

I want to recommend that you use alphaShape to generate the polyhedron, but it sounds like you have 4-D data since the function values depend on x,y,z.

http://www.mathworks.com/help/matlab/ref/alphashape.html

If your data is really 4-D, then you could use delaunayn to generate an N-D triangulation. But you still wouldn't be able to plot the output unless you take 3-D slices.
i'm not trying to "generate" a polyhedron as such, I have a function F of 3d space (x,y,z). At each point the function is valued as either zero or one. I want to plot a marker on a 3d graph wherever F=1 in the x-y-z space.
 
This is part of my code, in the code there is a function in inverse space (chi_wedge), and I want plot its IFT in real space by plotting a data point wherever the function takes the value 1.No_x = 100;
i_x = 1:No_x;
dx = L/(No_x - 1);
No_k0 = 100;

%real space
r_x = linspace(-L/2, L/2, No_x);
r_y = linspace(-L/2, L/2, No_x);
r_z = linspace(-L/2, L/2, No_x);
[r_x,r_y,r_z] = meshgrid(r_x,r_y,r_z);

%inverse space
k_x = linspace(-No_k0*2*pi/L,No_k0*2*pi/L,(2*No_k0)+1);
k_y = linspace(-No_k0*2*pi/L,No_k0*2*pi/L,(2*No_k0)+1);
k_z = linspace(-No_k0*2*pi/L,No_k0*2*pi/L,(2*No_k0)+1);
[k_x,k_y,k_z] = meshgrid(k_x,k_y,k_z);

%%%%

f = ifft(chi_wedge);
 
Hmm. It sounds like you want to generate a 3-D grid of sample points over some range -L/2 to L/2:

Code:
L= 10;
[x,y,z] = meshgrid(-L/2:L/100:L/2);
plot3(x(:), y(:), z(:), '.') %plot the entire grid as blue points

Then you want to filter these points according to whether f=1:
Code:
idx = find(f) % finds linear indices for all places where f is nonzero
x_new = x(idx); %filter x,y,z points based on where f is nonzero
y_new = y(idx);
z_new = z(idx);

% Plot only the points from the grid where f=1
plot3(x_new(:), y_new(:), z_new(:), 'r*')

Is this in the ballpark of what you're looking for?
 
  • Like
Likes Ben Wilson
kreil said:
Hmm. It sounds like you want to generate a 3-D grid of sample points over some range -L/2 to L/2:

Code:
L= 10;
[x,y,z] = meshgrid(-L/2:L/100:L/2);
plot3(x(:), y(:), z(:), '.') %plot the entire grid as blue points

Then you want to filter these points according to whether f=1:
Code:
idx = find(f) % finds linear indices for all places where f is nonzero
x_new = x(idx); %filter x,y,z points based on where f is nonzero
y_new = y(idx);
z_new = z(idx);

% Plot only the points from the grid where f=1
plot3(x_new(:), y_new(:), z_new(:), 'r*')

Is this in the ballpark of what you're looking for?

yes that is what I'm looking for thank you.

If my function has values of approximately 0 or 1 ( i.e. f= -0.1 to 0.1, and f=0.9 to 1.1) how would I achieve this separation?
 
Ben Wilson said:
If my function has values of approximately 0 or 1 ( i.e. f= -0.1 to 0.1, and f=0.9 to 1.1) how would I achieve this separation?

Since the FIND function only locates nonzeros, and you don't want to do exact comparisons with floating-point numbers, you should just use a rounding function on the function values before you pass them to find. This ensures that the values close to zero are exactly zero for the purposes of FIND.

It looks like you just need to use round(f) to round the values of f to the nearest integer. But other options are:

  • ceil rounds to the nearest integer towards +Inf.
  • floor rounds to the nearest integer towards -Inf.
  • fix rounds to the nearest integer towards zero.
 
  • Like
Likes Ben Wilson
  • #10
kreil said:
Since the FIND function only locates nonzeros, and you don't want to do exact comparisons with floating-point numbers, you should just use a rounding function on the function values before you pass them to find. This ensures that the values close to zero are exactly zero for the purposes of FIND.

It looks like you just need to use round(f) to round the values of f to the nearest integer. But other options are:

  • ceil rounds to the nearest integer towards +Inf.
  • floor rounds to the nearest integer towards -Inf.
  • fix rounds to the nearest integer towards zero.
thank you so much, you have been an amazing help
 

Similar threads

Replies
8
Views
2K
Replies
3
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
4K
Replies
1
Views
2K
Back
Top