重み付き最小二乗法に LSQNONLIN 関数を使用するにはどうすればよいですか?

1 view (last 30 days)
MathWorks Support Team
MathWorks Support Team on 5 Sep 2011
最小二乗法には Optimization Toolbox による LSQNONLIN および LSQCURVEFIT 関数が使用できますが、重み付き最小二乗法の場合の方法を教えてください。

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Sep 2011
LSQNONLIN 関数を重み付き最小二乗法に使用するためには、データを Fit するための方程式が必要です。以下は LSQNONLIN を重みを使用するための簡単なサンプルです。まず、二つの微分方程式を使用してノイズを加えることによりデータセットを作成します。次にネストされた MYCURV 関数を使用して LSQNONLIN をコールします。最後にデータを表示します。このサンプルを実行するためには以下のコードを myFit.m ファイルとして保存し、MATLAB 上で
>> myFit
を実行します。
function myFit
%%create the first half of the data
xdata1 = 0:.01:1;
ydata1 = exp(-3*xdata1) + randn(size(xdata1))*.05;
weight1 = ones(size(xdata1))*1;
%%create the second half of the data
% use a different function and with higher weights
xdata2 = 1:.01:2;
ydata2 = (xdata2-1).^2 + randn(size(xdata2))*.05;
weight2 = ones(size(xdata2))*10;
%%combine the two data sets
xdata = [ xdata1 xdata2 ];
ydata = [ ydata1 ydata2 ];
weight = [ weight1 weight2 ];
%%call |LSQNONLIN|
parameter_hat = lsqnonlin(@mycurve,[.10 -1],[],[],[],xdata,ydata)
%%plot the original data and fitted function in a figure
plot(xdata,ydata,'b.')
hold on
fitted = exp(parameter_hat(1).*(parameter_hat(2) +xdata));
plot(xdata,fitted,'r')
xlabel('x'); ylabel('y')
legend('Data', 'Fit')
%%function that reports the error
function err = mycurve(parameter,real_x, real_y)
fit = exp(parameter(1).*(real_x + parameter(2)));
err = fit - real_y;
% weight the error according to the |WEIGHT| vector
err_weighted = err.*weight;
err = err_weighted;
end
end
元のデータと比較すると、前半のデータに対し、後半のデータのほうがFitされています。また、MYCURVE に使用されているモデルが元のデータを作成するための関数と同じではないことにご注意ください。しかし、指数では2次の多項式とほぼ同じです。後半の重みより前半のデータへ重みをつけますが、これは LSQNONLIN に指数部分のみ減衰させることになります。

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!