Nlinfit error with func2str

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 11 Aug 2015
You need to call fit() on fit objects returned from fittype(), not nlinfit()
  2 Comments
Michael Lherbette
Michael Lherbette on 12 Aug 2015
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 on 12 Aug 2015
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

Sign in to comment.

More Answers (0)

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!