MATLAB Plotting 3D Surface Graph: Issues & Solutions

AI Thread Summary
The discussion revolves around challenges in creating a 3D surface graph using MATLAB with limited data points for x, y, and z coordinates. The user encounters errors with the griddata function, which indicates insufficient points to generate a surface. Suggestions include using the meshgrid command to create a grid for x and y coordinates, which is essential for surface plotting. It is emphasized that with very few data points, a surface plot may not be feasible, and alternative plotting methods such as plot3 or using subplots to display x-y and x-z relationships might be more effective. The conversation also touches on the importance of element-wise operations in MATLAB when calculating z values. Overall, the key takeaway is that the limited data points significantly restrict the ability to create a meaningful surface plot, prompting the need for alternative visualization strategies.
morry
Messages
136
Reaction score
0
Im trying to plot a 3d surface graph, but I am having trouble.

I have 3 data inputs, x,y,z. I also had X and Y=Y' which are the min:int:max values. Because I only have a few points, I tried to do Z= griddata(x,y,z,X,Y) to interpolate the results. But it comes up with an error saying that not all points could converge.

When I type surf(X,Y,Z) a new window opens and I get a blank graph.

Any ideas why this may be happening?
Cheers
 
Physics news on Phys.org
3d grids are a little tricky the first time you try to create them.

The easiest way I've found to get them up and running is to use the meshgrid command to create your x and y coordinates, and then create your z values from those grids.

If this doesn't answer your question, let me know and I'll track down some sample code for you.
 
I actually just read the command description for the griddata command you used, and I think I misinterprited your problem.

Can you cut and paste the relevant code you used here, and say what form the input variables have? Either that or just attach .mat and .m files? (you will probably need to zip them to allow the forum software to accept them)
 
Thanks enigma, here is the file. I don't have winzip, so I just saved it in a txt file.

Im pretty certain that the problem is to do with griddata, but I have no idea why.

Thanks again.
 

Attachments

ok. the reason it's giving you the errors is because you don't have enough points for it to fill in a surface.

If you just plot(x,y), you'll see that your data falls into a roughly linear set running from ~(1500,130) to ~(4000,35). What you're asking it to do when you use the surf command is to plot a rectangular grid surface covering the entire 1500-4100 in the x direction and 30-130 in the y direction. Since MATLAB has no information about anything other than along that line, it can't possibly come up with a surface for you.

What you can do is do a plot3(x,y,z) command, but that doesn't look much easier to see. Do you _need_ to display both x and y as independant variables? If so, you might be able to do a bunch of lines from (xi,yi,0) to (xi,yi,zi) and then put a star or something on the z=0 axis and label the z value.

What does the data represent? I'm sure there is an easier way to display your results.
 
Last edited:
I'm thinking that if you do need to show both x and y, your best bet would be to use the subplot command to split the window into 2. Put X-Y on the top half, and X-Z on the bottom half.

Also, because you have so few points, I'd be sure to use stars instead of lines to display the data. You don't have enough points to justify using the lines.
 
Last edited:
Well I need to graph engine speed, torque and efficiency. Whats frustrating is that I have instructions here from the lecturer and following those leads me up the garden path. :)

The graph should look something like this:
http://www.mame.mu.oz.au/thermofluids/mesh3.gif

Ill try that plot3 command, I don't really know what that last post of yours meant.

Thanks!
 
Last edited by a moderator:
It's not going to look like that gif because you simply don't have enough points. Plot X vs. Y and you'll see what I mean.

Check out the subplot command. It let's you split up a plot window into multiple graphs. I think the format is subplot(2,1,1) and subplot (2,1,2) to split it into 2 vertically stacked plots and access the first (top) and second (bottom) plot respectively.
 
Hmm, might have to ask my lecturer about this one. The graph is going to look pretty dodgy if I only have a handful of points to work with.
Thanks.
 
  • #10
lets say i wanted to do a surface plot of z = x*y^2*e^sin(3*x*y);

well first define the range of your x vector let's say -5 to 5

so x = linspace(-5,5) (this by default will put 100 pts between the values)

lets let y go from -6 to 6 so one again

y = linspace(-6,6,101) (now were using 101 elements)

this next part is SUPER CRUCIAL as it makes the grid for the x and y axes

[x y] = meshgrid(x,y)

this will make a dim(Y) by dim(X) grid for each of your variables

so now X is no longer a vector it is a 101x100 matrix

same thing with Y

You can look at these matrices more in detail in the MATLAB workspace if you really want. or just simply by typing mesh(x) or mesh(y)

now to plot your function you must remember to include periods for element-wise multiplication so...

z = x.*y.^2.*exp(sin(3*x.*y))

(notice you don't need a period between the 3 and x in the e term because 3 is a scalar)

now we simply just type mesh(x,y,z) and we will get the surface plot

if you want things to look smoother then just increase the number of point in the linspace commands. If you use too many it will take a while to plot

if you want the figure to move around continuously when rotating it, try not to use too many points. Also after you have computed z, all the commands

mesh(x,y,z) surf(x,y,z) contour(x,y,z) willl all work

try running them and compare the differences. Good Luck!
 

Similar threads

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