1. Not finding help here? Sign up for a free 30min 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!

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);
    plot(x,y)

    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
    Gold Member
    2016 Award

    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
    y=(x+1).*x;
     
    Last edited: Oct 28, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab - plotting a function
  1. MATLAB Function (Replies: 0)

Loading...