Least squares matrix?

%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.

Answers (1)

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

2 Comments

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.

Sign in to comment.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Asked:

on 8 Feb 2018

Commented:

on 8 Feb 2018

Community Treasure Hunt

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

Start Hunting!