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 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
David Goodmanson
2024 年 2 月 17 日
編集済み: David Goodmanson
2024 年 2 月 17 日
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
参考
カテゴリ
Help Center および File Exchange で PDE Solvers についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!