Nlinfit error with func2str

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Fit Postprocessing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by