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

  • Context: MATLAB 
  • Thread starter Thread starter naomineu
  • Start date Start date
  • Tags Tags
    2d Error Plotting
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting an error encountered while plotting functions in MatLab as part of a homework assignment. Participants explore the specifics of matrix dimensions and element-wise operations in the context of plotting multiple functions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes their struggle with an error message when attempting to plot functions, specifically asking for help with the code they have written.
  • Another participant suggests checking the error message and emphasizes the importance of distinguishing between element-by-element multiplication and matrix multiplication.
  • It is noted that the error message indicates a problem with inner matrix dimensions not agreeing.
  • Some participants recommend using the element-wise multiplication operator (.*) to resolve the issue.
  • One participant points out that the vectors for x and r have different lengths, which contributes to the error.
  • A participant mentions that they attempted to use the transpose of x but ended up with a graph that does not match the expected curve.
  • Another participant provides a detailed breakdown of the matrix dimensions involved in the calculations, highlighting where the errors occur.
  • Some participants suggest a one-line solution for calculating y2 using the correct operator.
  • One participant expresses frustration and mentions dropping the course due to feeling overwhelmed, while others provide encouragement and advice on learning programming in smaller steps.

Areas of Agreement / Disagreement

Participants generally agree on the need to use element-wise operations and the importance of matching vector dimensions. However, there is no consensus on the best approach to resolve the plotting issue, and some participants express differing views on learning strategies.

Contextual Notes

Participants discuss the limitations of their current understanding of MatLab and the challenges of debugging code, which may depend on individual experience levels and familiarity with programming concepts.

Who May Find This Useful

Individuals learning MatLab, particularly beginners facing similar issues with matrix operations and plotting functions, may find this discussion helpful.

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   Reactions: 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   Reactions: 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   Reactions: FactChecker

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K