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일
I would recommend lscov
p=lscov(x(:).^[1,0],y,w/N);
yfit=polyval(p,x)

댓글 수: 6

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.
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?
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
Thanks for your helping, I will try it later!
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

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