Matlab - plotting a function

In summary, to plot the equation (x-4)(x-5)(x-6), you can use the following code in MATLAB: x = -6:0.01:6; y = (x-4).*(x-5).*(x-6); plot(x,y). However, be careful to use the dot operator when multiplying vectors element-wise, as the "*" symbol represents matrix multiplication in MATLAB. It is also recommended to choose step sizes that are not multiples of 0.1, as this can lead to errors in computer simulations. Instead, use numbers such as inverse powers of 2. Additionally, it is a good practice to define all parameters (such as start and finish values, number of data points
  • #1
eurekameh
210
0
How do I plot something like (x-4)(x-5)(x-6)?
I've tried:

x = -6:0.01:6;
y = (x-4)*(x-5)*(x-6);
plot(x,y)

It's giving me the error:

Error using *
Inner matrix dimensions must agree.
 
Physics news on Phys.org
  • #2
Code:
y = (x-4).*(x-5).*(x-6);
The MATLAB "*" symbol is matrix multiplication.
If you want each element of a vector to be multiplied by the corresponding element of another vector, you have to "dot the star". Same if you want powers... v^2 is different from v.^2.

Aside: it is good form to choose step sizes that are not multiples of 0.1 ... try inverse powers of 2 instead.
The reason is that 0.1 is an irrational number in binary, so your computer has to approximate it ... when you, later, write simulations involving many iterations the errors can mount up.

Best practice: pick the number of data points you want first... in fact, put all your parameters in as variables: makes for easy adjustment later:
Code:
a=-6; # start
b=6; # finish
N=1024; # no. data points
dx=(b-a)/(N-1); # step size
x=a:dx:b; # x-axis
y=(x+1).*x;
 
Last edited:

1. How do I plot a function in Matlab?

To plot a function in Matlab, you can use the built-in "plot" function. First, define the x and y values of your function using the "linspace" and "function" commands. Then, use the "plot" command to plot the function with the specified x and y values. Finally, use the "xlabel", "ylabel", and "title" commands to add labels and a title to your plot.

2. Can I plot multiple functions on the same graph in Matlab?

Yes, you can plot multiple functions on the same graph in Matlab by using the "hold on" command after each "plot" command. This will allow you to plot multiple functions without clearing the previous plot. You can also use the "legend" command to label each function on the plot.

3. How do I change the color or style of a plotted function in Matlab?

To change the color or style of a plotted function in Matlab, you can use the "color" and "linestyle" parameters in the "plot" command. For example, to plot a red dashed line, you would use the command "plot(x, y, 'r--')" where 'r' represents the color red and '--' represents the dashed line style.

4. Can I save my plotted function as an image in Matlab?

Yes, you can save your plotted function as an image in Matlab by using the "saveas" command. This will allow you to save your plot as a .png, .jpg, or .eps file. You can also specify the resolution and size of the saved image.

5. How do I add a grid to my plotted function in Matlab?

To add a grid to your plotted function in Matlab, you can use the "grid on" command after your "plot" command. This will add a grid to your plot, which can make it easier to read and interpret the data. You can also customize the grid style and color using the "grid" command.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
883
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
875
  • Engineering and Comp Sci Homework Help
Replies
1
Views
952
  • Engineering and Comp Sci Homework Help
Replies
3
Views
805
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
819
Back
Top