當前位置:編程學習大全網 - 編程軟體 - matlab 最小二乘法擬合

matlab 最小二乘法擬合

主要的問題是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('圓',?'原始數據',?'擬合數據');

結果如圖:

  • 上一篇:30分信譽求解決壹個c的問題,通過編譯但是在輸入數據的時候CMD會掛掉,求解決
  • 下一篇:遼寧科技大學有哪些學部學院
  • copyright 2024編程學習大全網