MATLAB Plot function of two variables on 2D Grid

In summary: 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).
  • #1
etothex
2
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
  • #3
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:
  • #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:
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');
 
  • #5


Hello etothex,

Yes, it is possible to plot a function of two variables on a 2D grid using MATLAB's plot function. You can use the meshgrid function to create a grid of x and y values, and then use the plot function to plot the output of your stability function for each combination of x and y as a colored dot.

Here is an example code:

% Define x and y as vectors
x = 1:100;
y = 1:100;

% Create a grid of x and y values
[X,Y] = meshgrid(x,y);

% Call the stability function for each combination of x and y
Z = stability(X,Y);

% Plot the result as a colored dot for each combination of x and y
plot(X(:),Y(:),'o','MarkerFaceColor',Z)

This will create a 2D plot with x and y on the axes and the output of your stability function represented by colored dots. You can adjust the colors and markers as desired to make the plot easier to interpret.

I hope this helps. Happy plotting!
 

1. What is the purpose of the MATLAB Plot function of two variables on a 2D grid?

The purpose of the MATLAB Plot function of two variables on a 2D grid is to visualize the relationship between two variables on a two-dimensional grid. This can help to identify patterns, trends, and correlations in the data.

2. How do I plot two variables on a 2D grid using MATLAB?

To plot two variables on a 2D grid in MATLAB, you can use the "plot" function and specify the x and y variables as inputs. You can also use the "meshgrid" function to create a grid of evenly spaced points and then use the "surf" or "mesh" functions to plot the data on the grid.

3. Can I customize the appearance of the plot?

Yes, you can customize the appearance of the plot by using various options in the "plot" function, such as changing the line style, color, and marker type. You can also add labels, titles, and legends to the plot to make it more informative.

4. How can I add a third variable to the plot?

You can add a third variable to the plot by using color or size to represent the third variable. For example, you can use different colors to represent different categories or values of the third variable, or change the size of the markers based on the third variable.

5. Can I save the plot as an image or in a specific file format?

Yes, you can save the plot as an image or in a specific file format by using the "saveas" function in MATLAB. This will allow you to save the plot in a variety of formats, such as PNG, JPEG, PDF, or EPS, which can be useful for presentations or publications.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
995
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
763
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top