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!

MATLAB function plotting

  1. Nov 15, 2011 #1
    QUESTION:

    Plot the function f(x) = cosx sin(2x) and its derivative, both on the same
    plot, for π ≤ x ≤ π . Plot the function with a solid line, and the derivative with
    a dashed line. Add a legend and label the axes.


    Okay, so this is what I have so far in a script file...


    x = [-pi: 0.01: pi];

    y = cos(x)*sin(2*x);

    yd = 2*cos(x)*cos(2*x)-sin(x)*sin(2*x);

    plot (x,y,x,yd)


    But then I get this error:

    ??? Error using ==> mtimes
    Inner matrix dimensions must agree.

    Error in ==> p11 at 3
    y = cos(x)*sin(2*x);


    I'm not worried about the details, that should be easy enough. (dotted/solid lines and legends and axes labeling.) I just want to make sure I can get it to plot. Also it asks to plot for π ≤ x ≤ π, but i have a feeling it means -π ≤ x ≤ π.

    It looks pretty simple, but I'm still learning this program and it's proving to be more difficult than it should be. Anyone have any ideas what's wrong with my script?

    Thanks!
     
  2. jcsd
  3. Nov 16, 2011 #2
    Try taking the brackets off of x

    x = -pi : 0.01 : pi;
     
  4. Nov 16, 2011 #3
    That didn't work, but searched the error and I found out that there is an operator "." that you're supposed to use for multiplication.


    x = [-pi: 0.01: pi];

    y = (cos(x)).*(sin(2.*x));

    yd = 2.*cos(x).*cos(2.*x)-sin(x).*sin(2.*x);

    plot (x,y)

    hold on

    plot (x,yd, '--k')

    legend('y = (cos(x)).*(sin(2.*x))', 'yd = 2.*cos(x).*cos(2.*x)-sin(x).*sin(2.*x)')

    xlabel ('x-axis')

    ylabel ('y-axis')

    hold off


    thank you though
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MATLAB function plotting
  1. Plot in matlab (Replies: 1)

  2. Matlab Plots (Replies: 2)

  3. Matlab, plot (Replies: 5)

  4. Matlab plots (Replies: 1)

Loading...