Need help plotting simple graph in matlab

In summary, to plot a simple graph in Matlab, you can use the plot() function with two arrays of x and y values. To customize the appearance of your graph, you can use additional arguments with plot(), the legend() function, and functions such as xlim(), ylim(), and grid(). To save your graph as an image, you can use the saveas() function. Adding multiple lines to a graph can be done using the hold on and hold off commands, along with multiple calls to plot(). Lastly, to plot a function in Matlab, you can use the fplot() function with the desired function and range of values, and add a title and labels using title(), xlabel(), and ylabel().
  • #1
Fairy111
73
0

Homework Statement


I need to plot a graph in matlab,
i typed in the following,
V=[0:.2:20];
I=(V^3)-(1.4*V)+0.9;
plot(V,I)

It comes up with the error,
error using mpower, matrix must be square.

Any help into how to fix this problem would be great.

Thankyou



Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
A^2 = A*A, i.e. a matrix multiplication. Use A.^2 for element-wise operations:

V=[0:.2:20];
I=(V.^3)-(1.4*V)+0.9;
plot(V,I)

Ought to work.
 
  • #3


It seems like you are trying to raise a vector to a power in your code, which is why you are getting the error message. The "mpower" function in Matlab is used for matrix exponentiation, not for raising a vector to a power. To plot a graph in Matlab, you need to have two vectors of the same length - one for the x-axis values and one for the y-axis values. In your code, you have defined the vector V for the x-axis values, but you need to define a vector for the y-axis values as well. You can do this by using the element-wise power operator (".^") instead of the matrix power operator ("^") in your code. So your code should be:

V = [0:.2:20];
I = (V.^3) - (1.4*V) + 0.9;
plot(V,I)

This should plot the graph for you without any errors. I hope this helps. If you have any further questions, feel free to ask.
 

1. How do I plot a simple graph in Matlab?

To plot a simple graph in Matlab, you can use the plot() function. This function takes in two arrays as arguments, one for the x-values and one for the y-values. For example, if you want to plot the points (1,2), (3,4), and (5,6), you can use the following code: plot([1,3,5],[2,4,6]). You can also add a title and labels for the x and y axes using the title(), xlabel(), and ylabel() functions.

2. How do I customize the appearance of my graph in Matlab?

There are several ways to customize the appearance of your graph in Matlab. You can use the plot() function with additional arguments to change the line style, color, and markers. You can also use the legend() function to add a legend to your graph. Additionally, you can use the xlim() and ylim() functions to set the limits for the x and y axes, and the grid() function to add a grid to your graph.

3. How do I save my graph as an image in Matlab?

To save your graph as an image in Matlab, you can use the saveas() function. This function takes in two arguments - the handle of the figure you want to save and the file name you want to save it as. For example, if your figure handle is fig and you want to save it as "my_graph.png", you can use the following code: saveas(fig, "my_graph.png"). You can also specify the file type by including the file extension in the file name.

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

To add multiple lines to a graph in Matlab, you can use the hold on and hold off commands. These commands allow you to plot multiple lines on the same graph. First, use the plot() function to plot your first line. Then, use the hold on command to hold the current plot and add more lines using the plot() function. Finally, use the hold off command to turn off the hold and display all lines on the graph.

5. How do I plot a function in Matlab?

To plot a function in Matlab, you can use the fplot() function. This function takes in two arguments - the function you want to plot and the range of values for the independent variable. For example, if you want to plot the function f(x) = x^2 for x values between -5 and 5, you can use the following code: fplot(@(x) x^2, [-5,5]). You can also add a title and labels for the x and y axes using the title(), xlabel(), and ylabel() functions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
881
  • Engineering and Comp Sci Homework Help
Replies
2
Views
825
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top