Matlab plotting and defining range

In summary, to define x from minus pi to plus pi in matlab, you can use the code "x = -pi : pi" and then plot it using the function "plot(x,y)." However, if you receive an error message regarding y, make sure to use a dot before each operator and check if the result for y is a vector.
  • #1
ketanco
15
0
how do you define x to be from minus pi to plus pi in matlab?

i tried:
x = -pi : piBut when i said plot (x,y)

Matlab says:

"Error using plot
Data must be numeric, datetime, duration or an array convertible to double"

(i already defined y interms of x, that function is ok)

so i culd not define x probably... how to do it?
 
Physics news on Phys.org
  • #2
ketanco said:
how do you define x to be from minus pi to plus pi in matlab?

i tried:
x = -pi : piBut when i said plot (x,y)

Matlab says:

"Error using plot
Data must be numeric, datetime, duration or an array convertible to double"

(i already defined y interms of x, that function is ok)

so i culd not define x probably... how to do it?

Hi ketanco, welcome to MHB! ;)

Your x is fine, so I believe there is something wrong with your y.
Did you put a dot before each operator to allow for vector evaluation?
Did MatLab respond with a vector result for y?
 

FAQ: Matlab plotting and defining range

1. How do I plot a graph in Matlab?

To plot a graph in Matlab, you can use the plot() function. First, define the x and y values as vectors, then pass them as arguments to the plot() function. You can also add a title, labels for the x and y axes, and customize the appearance of the graph using additional arguments.

2. How do I define the range of values for my graph in Matlab?

To define the range of values for a graph in Matlab, you can use the xlim() and ylim() functions. Pass the desired minimum and maximum values as arguments to these functions, and they will set the range for the x and y axes, respectively.

3. Can I plot multiple graphs on the same figure in Matlab?

Yes, you can plot multiple graphs on the same figure in Matlab by using the hold on command. This will allow you to plot multiple graphs without clearing the previous ones. You can also use the subplot() function to create multiple plots within the same figure.

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

To change the color or style of a graph in Matlab, you can use additional arguments in the plot() function. For example, plot(x, y, 'r--') will plot the graph with a red dashed line. You can also use the color and linestyle functions to change the color and style of the graph after it has been plotted.

5. How can I save my plotted graph as an image file in Matlab?

To save a plotted graph as an image file in Matlab, you can use the saveas() function. Pass the figure handle and the desired file name and format as arguments to this function, and it will save the graph as an image file in your current working directory.

Similar threads

Replies
8
Views
895
Replies
5
Views
1K
Replies
11
Views
3K
Replies
4
Views
1K
Replies
11
Views
3K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
2
Views
2K
Back
Top