MATLAB Why am I Getting an Error Message When Plotting Functions in MatLab?

AI Thread Summary
The user is struggling with an error in MATLAB while attempting to plot functions due to mismatched dimensions between the vectors for r and x. The error message indicates that inner matrix dimensions must agree, which is a common issue when performing element-wise operations. Suggestions include using the element-wise multiplication operator (.*) and ensuring that r and x have compatible dimensions. The user attempted to resolve the issue by transposing x, but this led to incorrect graph outputs. Ultimately, the user decided to drop the course, feeling too far behind, and plans to self-study MATLAB before retrying the course next year.
naomineu
Messages
6
Reaction score
0
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!
 
Physics news on Phys.org
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.
 
It says Inner matrix dimensions must agree.
 
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:
  • Like
Likes naomineu
x has 10 elements, r has 5... there is your problem.
 
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
 
naomineu said:
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
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.
 
naomineu said:
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)
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:
  • Like
Likes naomineu
  • #11
Dr Transport said:
in one line

y2 = r.*(x.*(1-x));
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
FactChecker said:
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.

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
Dr Transport said:
in one line

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

FactChecker said:
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.
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.
 
  • Like
Likes FactChecker
Back
Top