Matlab graphing/plotting problem

  • MATLAB
  • Thread starter sara_87
  • Start date
  • Tags
    Matlab
In summary, To graph the coordinates (8,0), (4,-4), (-4,4), (-8,0) on MATLAB and join the first two points and the last two points on the same graph, you will need to create arrays for the X and Y data and use the plot command to plot them. The code to do this is shown above.
  • #1
sara_87
763
0

Homework Statement



i have 4 points,
(8,0)
(4,-4)
(-4,4)
(-8,0)
i want to draw a graph of this on MATLAB so that the first to points are joined, the last two points are joined, but i want it on the same graph.
how do i do this? what's the command?
thank you
 
Physics news on Phys.org
  • #2
Alright, so you're learning to use Matlab... :)
Your first two coordinates are: (8,0) and (4,-4)
These coordinates are in the form (X,Y).
so make an array with your X data and an array with your Y data like this:
X1=[8 4]; Y1=[0 -4];
Then get Matlab to plot it by using the plot command:
plot(X1,Y1)

Do the same for the other pair of coordinates...

Altogether, it will look like this: (just cut and paste.. and it should work)

%start
X1=[8 4]; Y1=[0 -4];
X2=[-4 -8]; Y2=[4 0];

plot(X1,Y1,...
X2,Y2);
%end
 
  • #3


To plot these points on the same graph in MATLAB, you can use the "plot" function. For example, if you have stored the x-values in a vector named "x" and the y-values in a vector named "y", you can use the command "plot(x,y)" to plot the points. To join the first two points, you can use the "line" function with the coordinates (8,0) and (4,-4). Similarly, you can use the "line" function to join the last two points with the coordinates (-4,4) and (-8,0). You can add these lines to your plot by using the "hold on" command before plotting the lines. This will allow you to plot multiple lines on the same graph. I hope this helps.
 

1. How do I plot a graph in Matlab?

To plot a graph in Matlab, you can use the plot function. This function takes in two arrays, one for the x-values and one for the y-values, and creates a plot with those points connected by lines. You can also add labels, titles, and customize the appearance of your graph using additional parameters in the plot function.

2. How do I save a graph in Matlab?

To save a graph in Matlab, you can use the saveas function. This function takes in the handle of the figure you want to save and the file name you want to save it as. You can also specify the file type (such as PNG, JPEG, or EPS) using additional parameters in the saveas function.

3. How do I add multiple lines to a single graph in Matlab?

To add multiple lines to a single graph in Matlab, you can use the hold function. This function allows you to plot multiple lines on the same axes without overwriting the previous plot. You can use it in conjunction with the plot function to add multiple lines, or use it with other plotting functions like scatter or bar.

4. How do I change the color or style of a graph in Matlab?

To change the color or style of a graph in Matlab, you can use the color and linestyle parameters in the plot function. These parameters allow you to specify the color and line style for your plot, such as 'r' for red or '--' for a dashed line. You can also use the color and linestyle functions to change the color and style of existing plots on your graph.

5. How do I add a legend to a graph in Matlab?

To add a legend to a graph in Matlab, you can use the legend function. This function takes in a cell array of strings containing the labels for each line on your graph. You can also specify the location of the legend and customize its appearance using additional parameters in the legend function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
932
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top