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

In summary: 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.
  • #1
naomineu
6
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
  • #2
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
It says Inner matrix dimensions must agree.
 
  • #4
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
  • #5
x has 10 elements, r has 5... there is your problem.
 
  • #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
 
  • #7
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.
 
  • #9
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

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

1. How do I fix an error in my 2D plot?

To fix an error in your 2D plot, you can try adjusting the data points, changing the plot type, or checking for any missing or incorrect values. It may also be helpful to consult the documentation or seek help from a colleague or online forum.

2. Why does my 2D plot have gaps or missing data points?

This could be due to incomplete or incorrect data, or issues with the way the data is being plotted. Check for any missing or incorrect values, and try adjusting the plot settings or data points to see if it resolves the issue.

3. How do I change the color or style of my 2D plot?

To change the color or style of your 2D plot, you can use the color and style parameters in your plotting function or adjust the settings in the graphing software you are using. You can also consult the documentation for specific instructions.

4. What is the best way to label my 2D plot?

The best way to label your 2D plot will depend on the type of data and the purpose of the plot. Generally, it is helpful to include a title, axis labels, and a legend to explain the data being plotted. You can also use annotations or captions to provide additional information.

5. How do I save or export my 2D plot for use in a presentation or report?

To save or export your 2D plot, you can use the save or export function in your plotting software or use a screenshot tool to capture the image. Make sure to choose a high-quality image format and check the resolution to ensure the plot appears clear and legible in your presentation or report.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
880
Back
Top