是否有可能使用點的大小'重量'最適合的線?使用散佈點的大小重量線最適合matlab
x = [1 2 3 4 5];
y = [2 4 5 3 4];
s = [10 15 20 2 5];
scatter(x,y,s)
hold on
weight = s;
p = polyfit(x,y,1); %how do I take into account the size of the points?
f = polyval(p,x);
plot(x,f,'-r')
是否有可能使用點的大小'重量'最適合的線?使用散佈點的大小重量線最適合matlab
x = [1 2 3 4 5];
y = [2 4 5 3 4];
s = [10 15 20 2 5];
scatter(x,y,s)
hold on
weight = s;
p = polyfit(x,y,1); %how do I take into account the size of the points?
f = polyval(p,x);
plot(x,f,'-r')
使用馬辛的建議,您可以將lscov
到polyfit
。正如documentation所解釋的那樣,通過計算x
的Vandermonde matrixV
,然後執行p = V\y
來完成多項式擬合。這是任何最小二乘解的標準形式,並通過lscov
將它自己應用於MATLAB中的加權最小二乘。
以你x
,y
和weight
載體,而不是調用polyfit(x,y,n)
你可以做到以下幾點:
% Construct Vandermonde matrix. This code is taken from polyfit.m
V(:,n+1) = ones(length(x),1,class(x));
for j = n:-1:1
V(:,j) = x.*V(:,j+1);
end
% Solve using weighted-least-squares
p = lscov(V,y,weight);
你甚至可以更進一步,並修改polyfit.m本身包含此功能,或如果您不想修改原始MATLAB函數,請添加另一個函數polyfitw.m。但請注意,polyfit
具有更多可選的結構輸出,使用QR分解計算,詳見文檔。將這些產出推廣到加權案例需要更多的工作。
x = (1:10)';
y = (3 * x + 5) + rand(length(x),1)*5;
w = ones(1,length(y));
A = [x ones(length(x),1)];
p = lscov(A,y,w);
plot(x,y,'.');
hold on
plot(x,p(1)*x + p(2),'-r');
您可以使用wieghted-least-square fit。有關matlab的示例的更多信息是[here](http://www.mathworks.com/matlabcentral/answers/98657)。 – Marcin
感謝@Marcin - 查看了這個文檔,我弄不清楚如何使用這個函數。我會假設像'a = lscov(x,y,s)'來獲得座標?任何幫助將不勝感激。 – user2861089
@ user2861089我擴展了Marcin的建議。這對你有幫助嗎? – buzjwa