Matlab非线性数据拟合:nlinfit和lsqcurvefit
程序员文章站
2024-03-08 21:45:10
...
目的:通过一个实例了解Matlab的数据拟合函数nlinfit和lsqcurvefit的使用。
- 结果图
- 具体数值
p =
0.3000 50.0000 0.4000 200.0000 0.3000 800.0000
p1 =
0.3267 48.3589 0.4030 226.6525 0.2838 809.6680
p2 =
0.3267 48.3646 0.4031 226.7359 0.2837 809.8132
- 代码
clear;clc;
M = 1000;
Te = 20;
t = Te*(1:M)';
rng('default'); % 固定噪声
Et = 0.3*exp(-(t/50).^2)+0.4*exp(-t/200)+0.3*exp(-t/800) + 0.01 * randn(M, 1);
p0 = [0.5, 50, 0.5, 100, 0.5, 500];
f = @(p,t) p(1)*exp(-(t/p(2)).^2)+p(3)*exp(-t/p(4)) +p(5)*exp(-t/p(6));
p1 = nlinfit(t, Et, f, p0);
lb = [0, 1, 0, 100, 0, 500];
ub = [1, 100, 1, 500, 1, 1000];
p2 = lsqcurvefit(f, p0, t, Et, lb, ub);
plot(t, Et, 'k', t, f(p1, t), 'r', t, f(p2, t), 'b', 'linewidth', 2)
legend('Model', 'nlinfit', 'lsqcurvefit')