%Constants NumRocks=[1608 1277 1025 851 726 438 102 53 9 5 1]; %Number of rockfalls vector Vol=[0.01 0.02 0.03 0.04 0.05 0.1 0.5 1 5 10 100]; %Volume in meters cubed
%Least Squares Method A=[NumRocks',ones(11,1)]; y=Vol'; b=[inv(A.*A').*A'.*y]
Getting error: Matrix dimensions must agree.

回答 (1 件)

Star Strider
Star Strider 2018 年 2 月 8 日

0 投票

Use the mldivide,\ (link) operator instead:
b = A\y;
b =
-19.1869e-003
21.2449e+000

2 件のコメント

Michelle Babak
Michelle Babak 2018 年 2 月 8 日
Thank you! Is it possible to use the least-squares formula though?
Star Strider
Star Strider 2018 年 2 月 8 日
My pleasure!
The mldivide function solves the equation in the least-squares sense.
You have the correct idea, however the derivation requires matrix operations, not element-wise operations. (You also have the order of the matrix and its transpose reversed.)
This works:
b = (A'*A)\A'*y
giving the same result as posted earlier.
It is more efficient and accurate to use this version than to use:
b = inv(A'*A)*A'*y
that again gives the same result.

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

カテゴリ

ヘルプ センター および File ExchangeLinear Algebra についてさらに検索

タグ

質問済み:

2018 年 2 月 8 日

コメント済み:

2018 年 2 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by