主要的問題是inline函數寫法不對,matlab不能識別下面的寫法:
f?=?inline('R?*?exp(-a?*?x)','[R?a]','x');像這種有多個待辨識參數的情況,應該寫成壹個向量,如
f?=?inline('c(1)?*?exp(-c(2)?*?x)','c','x');參考代碼:
%?生成測試數據t=linspace(0,2*pi,50);
x=1.5*cos(t);
y=1.5*sin(t);
plot(x,y)
hold?on
t=linspace(0,pi,30);
r=1.5?*?exp(-1?*?t)?+?0.02?*?randn(size(t));?%?數據中加入噪聲
x=r.*cos(t);
y=r.*sin(t);
plot(x,y,'r')
axis?equal
%?curve?fit
[theta,?rho]?=?cart2pol(x,?y);?%?transform?into?polar?coord
idx?=?(x<=eps)?&?(y<=eps);
theta(idx)?=?[];
rho(idx)?=?[];
f?=?inline('c(1)?*?exp(-c(2)?*?x)','c','x');
[BestPara,resnorm,residul]?=?lsqcurvefit(f,?[1.5?1],?theta,?rho);
R?=?f(BestPara,?theta);
X=R.*cos(theta);
Y=R.*sin(theta);
plot(X,Y,':g')
legend('圓',?'原始數據',?'擬合數據');
結果如圖: