MATLAB Plot function of two variables on 2D Grid

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 15K views
etothex
Messages
2
Reaction score
0
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:
Physics news on Phys.org
Last edited by a moderator:
MisterX said:
You can do that using http://www.mathworks.com/help/techdoc/ref/scatter.html", with C being vector with the same size as X and Y.


Code:
X = 0:7;
Y = 0:7;

colormap(hsv(256));
C = (0:7)/8;

scatter(X, Y, 4, C, 'filled');

You can also display 2D matrices using http://www.mathworks.com/help/techdoc/ref/image.html".

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