MATLAB 2D plotting error help please

1. Aug 28, 2016

naomineu

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.

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. Aug 28, 2016

FactChecker

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.

3. Aug 28, 2016

naomineu

It says Inner matrix dimensions must agree.

4. Aug 28, 2016

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
5. Aug 29, 2016

Dr Transport

x has 10 elements, r has 5..... there is your problem.

6. Aug 29, 2016

naomineu

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

7. Aug 29, 2016

FactChecker

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.

8. Aug 29, 2016

Staff: Mentor

9. Aug 31, 2016

FactChecker

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
10. Aug 31, 2016

Dr Transport

in one line

y2 = r.*(x.*(1-x));

11. Aug 31, 2016

FactChecker

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.

12. Sep 1, 2016

naomineu

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.

13. Sep 1, 2016

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.