Nlinfit error with func2str

1 回表示 (過去 30 日間)
Michael Lherbette
Michael Lherbette 2015 年 8 月 11 日
コメント済み: Michael Lherbette 2015 年 8 月 12 日
Hello,
I am trying to fit a curve using this function: t is the time vector, x the force measurement, d0 and nu are constants. I want to extract Er, tau1 and tau2.
function [Er,tau1,tau2] = viscoelasticcone(t,x,d0,nu)
fitfunctionconical=sprintf('Er*2*tan(35*pi/180)/(pi*(1-%d^2))*%d^2*(1+(tau1-tau2)/tau2*exp(-t/tau2))',nu,d0);
ftconical=fittype( fitfunctionconical, 'independent', 't', 'dependent', 'F');
[fitresult,R,~,CovB,MSE] = nlinfit(t,x, ftconical,[1e3 0.001 0.001]);
fit=ftconical(fitresult,t);
[Ypred,delta] = nlpredci(ftconical,t,fitresult,R,'Covar',CovB,'MSE',MSE,'SimOpt','on');
confidence{k}=delta;
lower = Ypred - delta;
upper = Ypred + delta;
end
If I use this function however, i get the following error:
Undefined function 'func2str' for input arguments of type 'fittype'.
Error in nlinfit (line 204)
m = message('stats:nlinfit:ModelFunctionError',func2str(model));
Any help ?
Michael

採用された回答

Walter Roberson
Walter Roberson 2015 年 8 月 11 日
You need to call fit() on fit objects returned from fittype(), not nlinfit()
  2 件のコメント
Michael Lherbette
Michael Lherbette 2015 年 8 月 12 日
Hello Walter,
What about the nlinfit ?
I use fit with option equal to NonLinearLeastSquares but I have a real trouble with the fitting.
s = fitoptions('Method','NonlinearLeastSquares');
fitfunctionconical=sprintf('Er*2*tan(35*pi/180)/(pi*(1-%d))*%d^2*(1+(tau1-tau2)/tau2*exp(-t/tau2))',nu,d0);
ftconical=fittype( fitfunctionconical, 'independent', 't', 'dependent', 'F','coefficients',{'Er','tau1','tau2'},'option',s);
fitresult = fit(t',x, ftconical);
coeffvals = coeffvalues(fitresult);
I get wrong values for the coefficients and the curve is not at all fitted.
Michael Lherbette
Michael Lherbette 2015 年 8 月 12 日
All right,
Here my final code:
ftVisco= @(b, t) b(1)*2*tan(35*pi/180)/(pi*(1-nu))*d0^2*(1+(b(2)-b(3))/b(3).*exp(-t./b(3)));
[fitresultnlinfit, R,~,CovB,MSE,Errorinfo] = nlinfit(t',x, ftVisco, [1e3 0.001 0.001]);
fitresultnlinfit
[Ypred,delta] = nlpredci(ftVisco,t',fitresultnlinfit,R,'Covar',CovB,'MSE',MSE);
confidence=delta;
lower = Ypred - delta;
upper = Ypred + delta;
It seems to work well.
Thank you Walter for pointing me out that we can't use fittype for nlinfit.
Cheers

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by