Created
January 14, 2020 07:36
-
-
Save monhime/bb3a218e74e0b38b51b3272752787852 to your computer and use it in GitHub Desktop.
減衰振動のデータを非線形曲線近似する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% tdata: 0から20まで100等分した値 | |
tdata = linspace(0, 20, 100); | |
% ydata: 計測して得られた物体の位置(を想定) | |
% ここでは理想のモデル式に乱数のノイズを足しています | |
ydata = 12.5*exp(-0.2*tdata) .* sin(tdata) + 0.3*randn(size(tdata)); | |
% 係数ベクトルtと、tdataを入力とする関数を作成 | |
% 考えているモデル式の係数の部分を係数で置き換えます | |
fun = @(t, tdata) t(1)*exp(t(2)*tdata) .* sin(t(3)*tdata); | |
% 係数ベクトルxの各要素の初期値(なんとなく予想できる値) | |
% 近ければOKです | |
t0 = [12, -0.1, 1.1]; | |
% 各係数が出力されます | |
t = lsqcurvefit(fun, t0, tdata, ydata); | |
% t= [12.4747 -0.1979 0.9979]だったとします | |
% 計測値を黒の丸で、近似曲線を青の実線でプロットします | |
% plot(tdata, ydata, 'ko' ,tdata, fun(t, tdata), 'b-'); | |
% なら計測値と近似曲線を一気にプロット出来ますが、近似曲線がtdataの値ごとの点を結んでいるためカクカクになります | |
% そこで、近似曲線の式を別に新しく定義し、重ねます | |
plot(tdata, ydata, 'ko'); | |
hold on; | |
f = @(t) 12.4747* exp(-0.1979* t) .* sin(0.9979* t); | |
fplot(f, [0 20], 'b-'); | |
% 以下、包絡線のプロットを付け加えています | |
f1 =@(t) 12.4747 * exp(-0.1979*t); | |
f2 =@(t) -12.4747 * exp(-0.1979*t); | |
fplot(f1, [0 20]); | |
fplot(f2, [0 20]); | |
hold off; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment