Total Least squares error help. I tried a couple different things here, but i'm not exactly sure how to do a total least squares error. The numbers aren't making sense to me.
1 view (last 30 days)
Show older comments
clear all
% A represents the sample measured in days
A = [1, 0; 1, 1; 1, 2; 1, 3; 1, 4; 1, 5; 1, 6; 1, 7]
% b represents the amount of radium measured
b = [100; 82.7; 68.3; 56.5; 46.7; 38.6; 31.9; 26.4]
% Least Squares
c = lsqr(A,log(b))
g = A.'*A
rhs = A.'*log(b);
x = g\rhs
thalf = -x(2)^-1*log(2)
m0 = exp(x(1))
m = @(t) m0*exp(x(2)*t);
predictionvalues = m([0, 1, 2, 3, 4, 5, 6, 7]);
squareddifferences = (predictionvalues - b).^2;
totalerror = sum(squareddifferences)
t = [0, 1, 2, 3, 4, 5, 6, 7];
plot(t, x(1) + x(2)*t, t, log(b),'o')
%%plot(t, abs(exp(y_approx(t)-b)))
xlabel("t")
ylabel("error")
2 Comments
Answers (1)
David Goodmanson
on 17 Feb 2024
Edited: David Goodmanson
on 17 Feb 2024
Hi Rian,
the problem is that b is a column vector, but predictionvalues is a row vector. Using
predictionvalues = m([0, 1, 2, 3, 4, 5, 6, 7])'; % column vector
squareddifferences = (predictionvalues - b).^2;
totalerror = sum(squareddifferences)
totalerror =
0.0033
although depending on circumstances, you may want to use sqrt(totalerror/8), the rms value, as the final result
See Also
Categories
Find more on PDE Solvers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!