Nobuhito Mori
Navigation :
開始行:
*カーブフィッテング [#l4bb7932]
* 多項式 [#n4e71567]
関数 polyfit は、最小二乗的にデータ群に適合する多項式の係...
p = polyfit(x,y,n)
x と y は、適合されるデータ群のxデータ、yデータで、n は、...
細かい刻みで、polyfit 推定値を計算し、実データ値と比較す...
x2 = 1:.1:5;
y2 = polyval(p,x2);
plot(x,y,'o',x2,y2)
grid on
* 一般形 (R14以降) [#a130cf75]
** Help通り [#d77fc3b7]
最初に以下のような M-ファイルを作成します。
データの x座標 と y座標に対応するベクトルを受け取る。
データを最も良く近似する指数関数のパラメータを返す。
そのためには、つぎのコードを M-ファイルにコピー、貼り付け...
function [estimates, model] = fitcurvedemo(xdata, ydata)
% ランダムな出発点を用いて fminsearch を呼び出します
start_point = rand(1, 2);
model = @expfun;
estimates = fminsearch(model, start_point);
% expfun は入力として曲線のパラメータを受け取り、
% A * exp(-B * xdata) - ydataの二乗誤差の和sseと
% FittedCurveを出力します。 FMINSEARCH は、sseのみを必要...
% 最後にFittedCurve のプロットも行います。
function [sse, FittedCurve] = expfun(params)
A = params(1);
B = params(2);
FittedCurve = A .* exp(-B * xdata);
ErrorVector = FittedCurve - ydata;
sse = sum(ErrorVector .^ 2);
end
end
この M-ファイルは、 fminsearch 関数を呼び出します。この関...
- 例題の実行
-- この例題を実行するために、最初に近似するためのランダム...
-- つぎのコマンドは、パラメータ A = 40 と B=0.5をもつ指数...
xdata = (0:.1:10)';
ydata = 40 * exp(-.5 * xdata) + randn(size(xdata));
- データを指数関数で近似するためには、つぎのように入力し...
[estimates, model] = fitcurvedemo(xdata,ydata)
これは、パラメータ A と Bに対する推定値を出力します。
estimates = 40.1334 0.5025
-結果のプロット
-- 近似曲線とデータをプロットするためには、つぎのコマンド...
plot(xdata, ydata, '*')
hold on
[sse, FittedCurve] = model(estimates);
plot(xdata, FittedCurve, 'r')
xlabel('xdata')
ylabel('f(estimates,xdata)')
title(['Fitting to function ', func2str(model)]);
legend('data', ['fit using ', func2str(model)])
hold off
** ちょっと変えてみた. [#e5741f55]
fminsearchを使う.
以下の関数を用意.ここではy=exp(-x^a)という関数の最適なa...
function [estimates, model] = fitcurve(xdata, ydata, sta...
model = @expfun;
estimates = fminsearch(model, start_point);
function [sse, FittedCurve] = expfun(params)
A = params(1);
FittedCurve = exp(-xdata.^A);
ErrorVector = FittedCurve - ydata;
sse = sum(ErrorVector .^ 2);
end
end
これを使い,以下のように実行すると最適値fが出力される.
[f] = fitcurve(x,y,start_point)
実際
[f] = fitcurve(x,y,[1])
関数の与え方によっては,初期値(start_point)に鋭敏に反応...
---------------------------------------------------------...
* 一般形 (R13まで) [#j9932ee5]
If you know the form of the required function you can use...
function out=fun(coeff,X,Y)
% fun.m
a = coeff(1);
b = coeff(2);
c = coeff(3);
Y_fun = a .* X + b .* sin(X)+c;
DIFF = Y_fun - Y;
SQ_DIFF = DIFF.^2;
out = sum(SQ_DIFF);
If you call this file fun.m, then
x=1:10;
y=sin(x);
bestcoeffs=fmins('fun',[1 1 1],[],[],x,y);
yfit=bestcoeffs(1)*x +bestcoeffs(2)*sin(x) + bestcoeffs(...
%Now compare y with yfit
plot(x,y,x,yfit);
will display the results. The [1 1 1] argument to fmins p...
fmins becomes fminsearch since R13.
*** fmins から fminsearchへの変換 バージョン4では、次の...
[X,OPTIONS] = fmins('FUN',x0,OPTIONS,[],P1,P2,...);
バージョン5では、つぎのように fminsearch を呼び出します。
[X,FVAL,EXITFLAG,OUTPUT] = fminsearch(@FUN,xs0,...
OPTIONS,P1,P2,...);
*** Referenece [#i3ca5a5e]
http://www-h.eng.cam.ac.uk/help/tpl/programs/Matlab/curve...
終了行:
*カーブフィッテング [#l4bb7932]
* 多項式 [#n4e71567]
関数 polyfit は、最小二乗的にデータ群に適合する多項式の係...
p = polyfit(x,y,n)
x と y は、適合されるデータ群のxデータ、yデータで、n は、...
細かい刻みで、polyfit 推定値を計算し、実データ値と比較す...
x2 = 1:.1:5;
y2 = polyval(p,x2);
plot(x,y,'o',x2,y2)
grid on
* 一般形 (R14以降) [#a130cf75]
** Help通り [#d77fc3b7]
最初に以下のような M-ファイルを作成します。
データの x座標 と y座標に対応するベクトルを受け取る。
データを最も良く近似する指数関数のパラメータを返す。
そのためには、つぎのコードを M-ファイルにコピー、貼り付け...
function [estimates, model] = fitcurvedemo(xdata, ydata)
% ランダムな出発点を用いて fminsearch を呼び出します
start_point = rand(1, 2);
model = @expfun;
estimates = fminsearch(model, start_point);
% expfun は入力として曲線のパラメータを受け取り、
% A * exp(-B * xdata) - ydataの二乗誤差の和sseと
% FittedCurveを出力します。 FMINSEARCH は、sseのみを必要...
% 最後にFittedCurve のプロットも行います。
function [sse, FittedCurve] = expfun(params)
A = params(1);
B = params(2);
FittedCurve = A .* exp(-B * xdata);
ErrorVector = FittedCurve - ydata;
sse = sum(ErrorVector .^ 2);
end
end
この M-ファイルは、 fminsearch 関数を呼び出します。この関...
- 例題の実行
-- この例題を実行するために、最初に近似するためのランダム...
-- つぎのコマンドは、パラメータ A = 40 と B=0.5をもつ指数...
xdata = (0:.1:10)';
ydata = 40 * exp(-.5 * xdata) + randn(size(xdata));
- データを指数関数で近似するためには、つぎのように入力し...
[estimates, model] = fitcurvedemo(xdata,ydata)
これは、パラメータ A と Bに対する推定値を出力します。
estimates = 40.1334 0.5025
-結果のプロット
-- 近似曲線とデータをプロットするためには、つぎのコマンド...
plot(xdata, ydata, '*')
hold on
[sse, FittedCurve] = model(estimates);
plot(xdata, FittedCurve, 'r')
xlabel('xdata')
ylabel('f(estimates,xdata)')
title(['Fitting to function ', func2str(model)]);
legend('data', ['fit using ', func2str(model)])
hold off
** ちょっと変えてみた. [#e5741f55]
fminsearchを使う.
以下の関数を用意.ここではy=exp(-x^a)という関数の最適なa...
function [estimates, model] = fitcurve(xdata, ydata, sta...
model = @expfun;
estimates = fminsearch(model, start_point);
function [sse, FittedCurve] = expfun(params)
A = params(1);
FittedCurve = exp(-xdata.^A);
ErrorVector = FittedCurve - ydata;
sse = sum(ErrorVector .^ 2);
end
end
これを使い,以下のように実行すると最適値fが出力される.
[f] = fitcurve(x,y,start_point)
実際
[f] = fitcurve(x,y,[1])
関数の与え方によっては,初期値(start_point)に鋭敏に反応...
---------------------------------------------------------...
* 一般形 (R13まで) [#j9932ee5]
If you know the form of the required function you can use...
function out=fun(coeff,X,Y)
% fun.m
a = coeff(1);
b = coeff(2);
c = coeff(3);
Y_fun = a .* X + b .* sin(X)+c;
DIFF = Y_fun - Y;
SQ_DIFF = DIFF.^2;
out = sum(SQ_DIFF);
If you call this file fun.m, then
x=1:10;
y=sin(x);
bestcoeffs=fmins('fun',[1 1 1],[],[],x,y);
yfit=bestcoeffs(1)*x +bestcoeffs(2)*sin(x) + bestcoeffs(...
%Now compare y with yfit
plot(x,y,x,yfit);
will display the results. The [1 1 1] argument to fmins p...
fmins becomes fminsearch since R13.
*** fmins から fminsearchへの変換 バージョン4では、次の...
[X,OPTIONS] = fmins('FUN',x0,OPTIONS,[],P1,P2,...);
バージョン5では、つぎのように fminsearch を呼び出します。
[X,FVAL,EXITFLAG,OUTPUT] = fminsearch(@FUN,xs0,...
OPTIONS,P1,P2,...);
*** Referenece [#i3ca5a5e]
http://www-h.eng.cam.ac.uk/help/tpl/programs/Matlab/curve...
ページ名:
MENU
INTRODUCTION
RESEARCH
EDUCATION
MEMO
COMPUTER
RESEARCH
ETC
LINK
最新の5件
2024-04-20
Introduction/CV in English
Introduction/CV in Japanese
2024-04-17
Publications/講演・セミナー等
Publications/Seminars and Presentations
2024-04-06
Publications/Conference Proceedings
カスタム検索
Counter
Total : 0
Today : 0
Yesterday : 0
Edit Menu
[
新規
] [
ヘルプ
]
Site Search
AND
OR