What am I doing wrong? Matrix operators

16 ビュー (過去 30 日間)
Kevin Carty
Kevin Carty 2020 年 2 月 22 日
コメント済み: Preet Talati 2021 年 2 月 3 日
function surfaceArea = surfaceBalloon(Volume,M)
% Your code goes here %
radius = ((3*Volume)./((2+M)*pi))^1/3;
surfaceArea = pi*radius^2.*(2+sqrt(1+M^2));
end
What is wrong with my code? It says:
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the
matrix is square and the power is a scalar. To perform elementwise
matrix powers, use '.^'.
Error in surfaceBalloon (line 5)
surfaceArea = pi*radius^2.*(2+sqrt(1+M^2));
I thought you didn't have to use dot operators unless you are multiplying, dividing, or raising two matricies. I am raising the radius variable to 1/3 which is a scalar value. Am i missing something?
  2 件のコメント
Stephen23
Stephen23 2020 年 2 月 22 日
編集済み: Stephen23 2020 年 2 月 22 日
"Am i missing something?"
The difference between matrix and array operations.
"I thought you didn't have to use dot operators unless you are multiplying, dividing, or raising two matricies. I am raising the radius variable to 1/3 which is a scalar value."
I don't think that rule-of-thumb is correct. Instead:
  • use matrix operations when doing linear algebra,
  • otherwise use array operations.
Because, as the documentation makes clear, matrix operations apply the rules of linear algebra.
Preet Talati
Preet Talati 2021 年 2 月 3 日
You need to add a dot operator to all the exponent calculations and your 1/3 needs to be (1/3).

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

回答 (1 件)

Star Strider
Star Strider 2020 年 2 月 22 日
If you want to raaise the elements of a matrix to a power, use element-wise exponentiation with the dot operator: .^ and see: Array vs. Matrix Operations for details.
Matrix exponentiation (and several other operattions) are only defined for square matrices. See the Wikipedia article on the Cayley-Hamilton theorem for that discussion.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by