• Support PF! Buy your school textbooks, materials and every day products Here!

Matlab - plotting a function

  • Thread starter eurekameh
  • Start date
  • #1
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.
 

Answers and Replies

  • #2
Simon Bridge
Science Advisor
Homework Helper
17,856
1,654
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:

Related Threads on Matlab - plotting a function

  • Last Post
Replies
2
Views
4K
Replies
10
Views
720
  • Last Post
Replies
5
Views
928
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
1
Views
824
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
10
Views
10K
  • Last Post
Replies
6
Views
609
  • Last Post
Replies
1
Views
8K
  • Last Post
Replies
2
Views
1K
Top