# Matlab Plotting a 3d function

Tags:
1. Apr 1, 2016

### Ben Wilson

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?

2. Apr 1, 2016

### Simon Bridge

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.

3. Apr 2, 2016

### Ben Wilson

a polyhedron sorry.

what i want to be able to do is see the polyhedron.

4. Apr 2, 2016

### kreil

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.

5. Apr 2, 2016

### Ben Wilson

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.

6. Apr 2, 2016

### Ben Wilson

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);

7. Apr 2, 2016

### kreil

Hmm. It sounds like you want to generate a 3-D grid of sample points over some range -L/2 to L/2:

Code (Text):

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 (Text):

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?

8. Apr 4, 2016

### Ben Wilson

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?

9. Apr 4, 2016

### kreil

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.

10. Apr 4, 2016

### Ben Wilson

thank you so much, you have been an amazing help