Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab 2D plotting error help please

  1. Aug 28, 2016 #1
    Hey everyone,
    I just started, what is supposed to be a beginners course in MatLab, but I am the only one who has never used MatLab before! I'm stuck on the very first HW assignment (although I did manage to get to problem 6 out of 7) and was hoping someone could explain why I keep getting an error message.
    The question reads:

    Make one 2D plot for 0≤x≤1 containing the following 6 functions
    f(x) = x
    g(x) = r x (1-x)
    with r={0.5, 1.5, 2.5, 3.5, and 4}. Label the axes, choose very different line styles for each curve, and add a legend for each curve. Please insert the resulting graph here. For what values of r do f(x) and g(x) intersect?

    Here is what I was trying to do:
    >> x=0:0.1:1
    >> r=[0.5 1.5 2.5 3.5 4]
    >>y1=x
    >>y2=r*x*(1-x)
    --> This is where I get an error! What am I doing wrong? How do I get around this?

    Thank you!!!
     
  2. jcsd
  3. Aug 28, 2016 #2

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    What does the error say? You should look at the individual dimensions and be careful to distinguish between element-by-element multiplication versus matrix multiplication. When debugging, it helps to do the individual multiplications one at a time into intermediate variables and verify that you get what you expected.
     
  4. Aug 28, 2016 #3
    It says Inner matrix dimensions must agree.
     
  5. Aug 28, 2016 #4

    jedishrfu

    Staff: Mentor

    Try using the *. operator as in the example y = x *. x

    Also r and x need to have the same number of elements.
     
    Last edited: Aug 29, 2016
  6. Aug 29, 2016 #5

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    x has 10 elements, r has 5..... there is your problem.
     
  7. Aug 29, 2016 #6
    I've tried again with equal number elements and I had the same problem/error. I then tried y2=r*x'*(1-x) (so to do the transpose of x), and that worked, but the graph I get is 2 lines, not a curve like the question talks about
     
  8. Aug 29, 2016 #7

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    Try some of the suggestions above. @jedishrfu and @Dr Transport have given you corrections that you really need. I gave you a couple of debugging hints. It looks like you haven't tried any of those.
     
  9. Aug 29, 2016 #8

    jedishrfu

    Staff: Mentor

  10. Aug 31, 2016 #9

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    x is a 1x10 vector. r is a 1x5 vector. r*x*(1-x) is (1x5)*(1x10)*(1x10), which is invalid because the inner dimensions do not match.
    r*x'*(1-x) is (1x5)*(10x1)*(1x10) = (1x5)*(10x10), which is also invalid for the same reason.

    With the .* operator that @jedishrfu recommended, you have x .* (1-x) is a 1x10 vector. So r * x .* (1-x) is (1x5) * (1x10), which is invalid, but now at least x .* (1-x) should be the 1x10 vector of values that you want.

    Try this:
    % Calculate y2 = r' * ( x .* (1-x) )
    temp1 = (1-x) % 1x10
    temp2 = x .* temp1 % 1x10
    y2 = r' * temp2 % (5x1) * (1x10) = (5x10)

    y2 should be a valid 5x10 matrix. Check that temp1, temp2, y2 have the results that you expect. Each row of y2 should be the values of one function.
     
    Last edited: Aug 31, 2016
  11. Aug 31, 2016 #10

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    in one line

    y2 = r.*(x.*(1-x));
     
  12. Aug 31, 2016 #11

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    Yes, but a person who doesn't know how to write or debug MATLAB programs should learn to program in baby steps first and consolidate later.
     
  13. Sep 1, 2016 #12
    I really appreciate the help thank you! I've ended up dropping the course today though because I felt way too far behind the rest of the class. I am going to try and get some books, learn on my own first and try again next year.
     
  14. Sep 1, 2016 #13

    Mark44

    Staff: Mentor

    This is good advice for beginning students of just about any programming language. It's much easier to debug the code when intermediate calculations are stored in separate variables.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted