Error using "*" in my equation, assumes I am trying to do matrix multiplication.

18 ビュー (過去 30 日間)
Kyle
Kyle 2023 年 5 月 1 日
コメント済み: Kyle 2023 年 5 月 1 日
I have a simple plot that I am trying to create with xticks and a trig function. For some reason, when I try to plot my graph, its saying the equation for my y is using "*" multiplication wrong and gives me the "incorrect dimensions for matrix multiplication error. Obviously I am not using a matrix and I don't seem to understand how I have wrote my y equation incorrectly, and ideas? Code below:
x = linspace(-0.23,5);
y = .6*x + (x*cos(x/3)) + ((x*30)/sin(x));
plot(x,y);
xticks(-0.23:.1:5);
title('Problem 4: x vs F');
xlabel('Value of x');
ylabel('Value of F');
  1 件のコメント
Stephen23
Stephen23 2023 年 5 月 1 日
"Obviously I am not using a matrix and I don't seem to understand how I have wrote my y equation incorrectly, and ideas? "
If you are not doing linear algabra then you probably should be using array operations:

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2023 年 5 月 1 日
Even though you are not using matrices, does not mean that linear algebra could not be intended. MATLAB does not know what you are trying to do. It only tries to execute what you tell it to do.
And you may THINK you are not using matrices, but you are, when you use vectors. Matrix algebra is well defined for vectors too. And vectors are just matrices, with only one row or column. For example, the dot product between two vectors can be represented as:
X = 1:5;
Y = [2 3 5 7 11];
X*Y'
ans = 106
Here, MATLAB has formed an inner product beteween two vectors, so MATRIX algebra. And next, we see:
X'*Y
ans = 5×5
2 3 5 7 11 4 6 10 14 22 6 9 15 21 33 8 12 20 28 44 10 15 25 35 55
This is sometimes called the outer product. Again, matrix algebra.
So when you use * where vectors are involved, MATLAB still assumes you know what you are doing. Abd it tries to execute that, as you wrote it.
When MATLAB has a problem with what you wrote, it tells you. And that means you need to learn to use the dotted operators in MATLAB, when you want to perform element-wise operations. So if you want to perform the pairwise multiplications between elements, then do this:
X.*Y
ans = 1×5
2 6 15 28 55
Remember that MATLAB is at heart, a MATRIX language. If you don't want it to think in terms of matrices, you need to tell it exctly what you do want it to do, instead of assuming it can read your mind. Computers are notoriously bad at reading minds. (And if the day comes that computers can read minds, I'm not sure that would be a good thing anyway.)
  1 件のコメント
Kyle
Kyle 2023 年 5 月 1 日
Apologies I am still new to matlab, that makes much more sense now thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by