Confusing about applying weighted least square for constant fitting

I'm now fitting a line with noise. My equation is to minimize corresponding to equation , then I have and with data. I want to caculate the best y. The WSL gives for the answer. But now my confusing is what is Y? Is this , which means my code is
(1) is the matrix with number 1. Is this right for me? or I should use other function such as fminsearch(I saw in the community, maybe it's still my missunderstanding)...Thanks

 採用された回答

Matt J
Matt J 2021 年 7 月 19 日
編集済み: Matt J 2021 年 7 月 19 日

0 投票

I would recommend lscov
p=lscov(x(:).^[1,0],y,w/N);
yfit=polyval(p,x)

6 件のコメント

WeiHao Xu
WeiHao Xu 2021 年 7 月 19 日
Thanks sir! I use this get the result with dimension , so this right! And could you please point out my misstake above question? Thanks so much!
Matt J
Matt J 2021 年 7 月 19 日
編集済み: Matt J 2021 年 7 月 19 日
@Torsten has corrected your formulation of the normal equations, but it would be inadvisable to use the normal equations directly. lscov handles the numerical issues involved in the inversion of (X^T*W*X) more reliably.
WeiHao Xu
WeiHao Xu 2021 年 7 月 19 日
Maybe I can get it, I will test it later . To prevent my missunderstanding, the code above x(:).^[1,0] is matrix with one? The standard solution of WSL confused me with vector X, for my case, y=y+noise, has no X. In this case I think it's ones(N,1), same as x above. Does my comprehension correct?
Matt J
Matt J 2021 年 7 月 19 日
編集済み: Matt J 2021 年 7 月 19 日
You can execute some example code to see what x(:).^[1,0] gives you
x=1:5;
x(:).^[1,0]
ans = 5×2
1 1 2 1 3 1 4 1 5 1
This is appropriate if you are fitting a line with potentially non-zero slope. If you are fitting a line with zero slope, then you could substitute just x(:).^0
WeiHao Xu
WeiHao Xu 2021 年 7 月 19 日
Thanks for your helping, I will try it later!
Matt J
Matt J 2021 年 7 月 19 日
You're welcome, but if you find that one of the answers does what you want, please do Accept-click it.

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2021 年 7 月 19 日
編集済み: Torsten 2021 年 7 月 19 日

0 投票

X = ones(N,1)
W = diag(w)
Y = y
where y is the (Nx1) column vector of the measurements and w is the (Nx1) column vector of weights.
The result of your formula is the coefficient a of the line y=a that best approximates the measurements.

5 件のコメント

WeiHao Xu
WeiHao Xu 2021 年 7 月 19 日
I have tried with ones(N,1)...According to my comprehension, my code is y = pinv( ones(N,1) * w * ones(N,1) )' *ones(N,1)' * w * y, which w and y is . Then matlab gives a dimension error. Then I change w to diag(w) which is , the result is a number. But I think the true result is , Could you please tech me some about it? Thanks so much!
Torsten
Torsten 2021 年 7 月 19 日
The result is a number since you do constant fitting (you want to fit your vector y to a horizontal line y = a).
It is this number a that you get from the formula.
WeiHao Xu
WeiHao Xu 2021 年 7 月 19 日
I may get it. Acording to y=a, I thought the result is [a a a a a ]. But the true answer is that I use N point to fit a data a, so it gives a. So even I use a sufficiently large number N, then I solve this equation, the result is a number, a, right? Thanks so much.
Torsten
Torsten 2021 年 7 月 19 日
Correct.
WeiHao Xu
WeiHao Xu 2021 年 7 月 19 日
Thanks so much!

サインインしてコメントする。

カテゴリ

質問済み:

2021 年 7 月 19 日

コメント済み:

2021 年 7 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by