1. PF Contest - Win "Conquering the Physics GRE" book! Click Here to Enter
    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);

    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
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Matlab plotting function Date
Plotting with Matlab Feb 18, 2017
Find Fourier transform and plot spectrum by hand & MATLAB Dec 1, 2016
Plotting in Matlab Nov 12, 2016
MATLAB: Plot vectors from a function Mar 17, 2015
MATLAB function plotting Nov 15, 2011