MATLAB MATLAB Plot function of two variables on 2D Grid

AI Thread Summary
A user seeks assistance in plotting a function with two independent variables in MATLAB, aiming to visualize outputs as colored dots on a 2D grid. They express confusion over whether such a plot can be achieved, given that many tutorials suggest 3D plotting for functions of two variables. Responses indicate that a scatter plot can be utilized, with suggestions to generate the necessary vectors for plotting. Techniques such as using the `kron` and `repmat` functions are recommended to create the required input vectors for the scatter function. The discussion concludes with a clear method for visualizing the data effectively in 2D.
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');
 

Similar threads

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