Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!


  1. Oct 17, 2005 #1
    Im trying to plot a 3d surface graph, but Im 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?
  2. jcsd
  3. Oct 17, 2005 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
  4. Oct 17, 2005 #3


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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 relevent 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)
  5. Oct 17, 2005 #4
    Thanks enigma, here is the file. I dont 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.

    Attached Files:

  6. Oct 18, 2005 #5


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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: Oct 18, 2005
  7. Oct 18, 2005 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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: Oct 18, 2005
  8. Oct 18, 2005 #7
    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 [Broken]

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

    Last edited by a moderator: May 2, 2017
  9. Oct 18, 2005 #8


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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 lets 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.
  10. Oct 18, 2005 #9
    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.
  11. Nov 9, 2005 #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 lets 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 dont 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!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook