|
沙发
楼主 |
发表于 2015-3-13 23:38:08
|
只看该作者
今天正好遇到一个拟合的问题,用到了匿名函数
clear all
close all
clc
format long
t = (1:25)';
y = [ 5.0291; 6.5099; 5.3666; 4.1272; 4.2948; ...
6.1261; 12.5140; 10.0502; 9.1614; 7.5677; ...
7.2920; 10.0357; 11.0708; 13.4045; 12.8415; ...
11.9666; 11.0765; 11.7774; 14.5701; 17.0440; ...
17.0398; 15.9069; 15.4850; 15.5112; 17.6572];
p = polyfit(t,y,1);%这里做一次多项式拟合
coff_a = [p(2);p(1)];
save 'coef_ex2a.dat' 'coff_a' -ascii
mse_ex2a = mean((polyval(p,t) - y).^2);
save 'mse_ex2a.dat' 'mse_ex2a' -ascii
opts=optimset('MaxIter',600,'MaxFunEvals',600,'TolFun',1e-12,'TolX',1e-12);
coef_ex2b = lsqcurvefit(@(p,tdata)p(1)+p(2)*tdata +p(3)*sin(tdata),zeros(3,1),t,y,[],[],opts);
%这里做非线性曲线拟合
save 'coef_ex2b.dat' 'coef_ex2b' -ascii
b1 = coef_ex2b(1);
b2 = coef_ex2b(2);
b3 = coef_ex2b(3);
mse_ex2b = mean((feval(@(t) b1+b2*t+b3*sin(t),t) - y).^2);
save 'mse_ex2b.dat' 'mse_ex2b' -ascii |
|