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

3 views (last 30 days)
Rian Sullivan on 17 Feb 2024
Commented: Rian Sullivan on 17 Feb 2024
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]
A = 8×2
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]
b = 8×1
100.0000 82.7000 68.3000 56.5000 46.7000 38.6000 31.9000 26.4000
% Least Squares
c = lsqr(A,log(b))
lsqr converged at iteration 2 to a solution with relative residual 9.5e-05.
c = 2×1
4.6051 -0.1903
g = A.'*A
g = 2×2
8 28 28 140
rhs = A.'*log(b);
x = g\rhs
x = 2×1
4.6051 -0.1903
thalf = -x(2)^-1*log(2)
thalf = 3.6417
m0 = exp(x(1))
m0 = 99.9942
m = @(t) m0*exp(x(2)*t);
predictionvalues = m([0, 1, 2, 3, 4, 5, 6, 7]);
squareddifferences = (predictionvalues - b).^2;
totalerror = sum(squareddifferences)
totalerror = 1×8
1.0e+04 * 1.9858 1.0169 0.5788 0.4646 0.5396 0.7175 0.9436 1.1847
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")
Dyuman Joshi on 17 Feb 2024
What seems to be the problem/issue?
Rian Sullivan on 17 Feb 2024
Is the total least squares function okay? I was just making sure I even did the total least squares error code right. The total error seemed really small, but maybe im misunderstanding what it actually means. totalerror =
1.0e+04 *
Is what pops up along with all the other columns and their errors. It makes sense I believe.. but I'm just struggling with thinking my number of error doesn't look right.

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
Rian Sullivan on 17 Feb 2024
Oh wow! That makes complete sense. Can't believe I missed that. I think that total error makes a lot more sense to me now. Thank you so much!