Error in line 2... Won't plot my graph.

1 view (last 30 days)
T= 1:5:1000;
P=(10*(log(T/298))*T)+(12.41*T)+1550;
plot(T,P)
What's wrong with my code, why won't it run?

Accepted Answer

Walter Roberson
Walter Roberson on 19 Sep 2015
P = (10*(log(T/298)).*T) + (12.41*T) + 1550;
Note: the volunteers who answer questions are sensitive to spacing in code. As in when there isn't any, it is more difficult for the volunteers to understand the code and then they tend to wander off to answer questions that are easier to read.
  3 Comments
Walter Roberson
Walter Roberson on 19 Sep 2015
T is a row vector 1 x 200, so log(T/298) is a row vector 1 x 200, so 10*(log(T/298)) is a row vector 1 x 200. You then had that "*" T. That is one row vector 1 x 200 "*" a row vector 1 x 200. But "*" is algebraic matrix multiplication. For matrix multiplication, the "inner dimensions" must be the same -- the second dimension of the left matrix must be the same as the first dimension of the right matrix. 1 x 200 by 1 x 200 does not satisfy that condition -- 200 ~= 1. So you get an error.
You could transpose the first matrix to be 200 x 1 using the .' operator, so that you were working with 200 x 1 by 1 x 200, if you wanted to get out a 200 x 200 matrix. Or you could transpose the second matrix to be 200 x 1, so that you were working with 1 x 200 by 200 x 1, if you wanted to get out a 1 x 1 matrix. Or you can switch to the element-by-element multiplication operator, .* instead of the matrix multiplication operator * and that is what I changed your code to.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!