1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Matlab - plotting a function

  1. Oct 28, 2012 #1
    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);

    It's giving me the error:

    Error using *
    Inner matrix dimensions must agree.
  2. jcsd
  3. Oct 28, 2012 #2

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper

    Code (Text):
    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 (Text):
    a=-6; # start
    b=6; # finish
    N=1024; # no. data points
    dx=(b-a)/(N-1); # step size
    x=a:dx:b; # x-axis
    Last edited: Oct 28, 2012
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook