Plotting 3D Surface Graph: Issues & Solutions

Click For Summary

Discussion Overview

The discussion revolves around issues encountered while attempting to plot a 3D surface graph using MATLAB, specifically focusing on the use of the griddata function for interpolation with limited data points. Participants explore various methods for visualizing data in three dimensions, including alternative plotting commands and techniques for handling sparse datasets.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports difficulties with the griddata function, receiving an error about convergence and ending up with a blank graph when using the surf command.
  • Another participant suggests using the meshgrid command to create x and y coordinates, which may help in generating z values from those grids.
  • A different participant requests clarification on the input variables and code used, indicating that understanding the data structure is crucial for troubleshooting.
  • One participant notes that the error arises from insufficient data points to create a surface, suggesting that plotting the data directly may reveal its linear nature.
  • Another participant proposes using the plot3 command as an alternative but questions the necessity of displaying both x and y as independent variables.
  • Suggestions are made to use the subplot command to create multiple plots in one window, which may help in visualizing the data more effectively given the limited points.
  • One participant expresses frustration with the instructions provided by their lecturer, indicating a disconnect between expectations and the practical limitations of their data.
  • A participant provides a hypothetical example of creating a surface plot, detailing the steps to define ranges for x and y, create a grid, and compute z values, emphasizing the importance of element-wise operations in MATLAB.

Areas of Agreement / Disagreement

Participants generally agree that the limited number of data points is a significant issue for creating a meaningful surface plot. However, there are multiple competing views on the best approach to visualize the data, and the discussion remains unresolved regarding the optimal method to achieve the desired graph.

Contextual Notes

Limitations include the potential misunderstanding of the griddata function and the challenges of visualizing sparse datasets. There is also uncertainty about the specific requirements for the graph as per the lecturer's instructions.

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 independent 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 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K