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

MATLAB Plot function of two variables on 2D Grid

  1. Jun 17, 2011 #1
    Hey all - this is my first post on the forum but I've browsed for help for a really long time on other topics. Any help would be greatly appreciated!

    So I have a function, called stability.m that takes in two independent variables, say x and y, as input and outputs either a 1 or a 0 for each pairing of x and y. I would like to set up a plot as x versus y that plots each pairing as a colored dot (simply all 1's = black and all 0's = red, for instance). I would more or less rather like a grid/matrix so I can easily see what each possible pairing would represent for broad ranges of x and y (IE, x and y = 1x100 vectors).

    I was wondering if this is even possible. I have read through tons of tutorials regarding mesh plots, scatter plots, etc and have found most to say that functions of two variables can only be plotted on a 3d graph. Is this true?

    Thanks for the help
    - etothex
     
    Last edited: Jun 17, 2011
  2. jcsd
  3. Jun 17, 2011 #2
    Last edited by a moderator: Apr 26, 2017
  4. Jun 17, 2011 #3
    Okay got a little further with the image function. however, with scatter, the constraint that C needs to be of same length as X, Y won't be good here because if X and Y are length 4, then there will be 16 points that need to be plotted on the graph. (X=1, Y=1:4 then X=2, Y=1:4 and so on). Is there a way around this?
     
    Last edited by a moderator: Apr 26, 2017
  5. Jun 17, 2011 #4
    The length of X and the length of Y and the length of C should all be equal to the number of points (since you want a different color for each point). By X, Y, and C, I mean the parameters to the scatter function.

    So, using the example you gave, one might think of the probem as, given the two length 4 vectors, how does one use MATLAB to generate the two 16 length vectors. Here is one way to do it:

    Code (Text):

    x = 1:4;
    y = 1:4;

    X = kron(x,ones(size(y))); %repeats each element (a bit like interpolation)
    Y = repmat(y, size(x)); %repeats the whole vector

    colormap(hsv(length(X)));

    C = (0:(length(X) - 1))/length(X);
    scatter(X, Y, 4, C, 'filled');
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MATLAB Plot function of two variables on 2D Grid
Loading...