%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?
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 Exchange에서 Linear 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