From: Oznur Mete on
Dear All,

I am trying to define a custom fit model and fit to the data.

Here is the piece of my code:
======================================
s = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0.9,0.9],...
'Upper',[1.1,1.1], ...
'Startpoint',[1 1]);
f = fittype('a*exp(b*sqrt(sind(x)))','coefficients',{'a','b'},'independent','x','options', s);
ph1= ph1(:);
ch1 = ch1(:);
[c2,gof2] = fit(ph1,ch1,f);
=====================================


I received the following error message:
===================================
??? Complex value computed by model function, fitting cannot continue.
Try using or tightening upper and lower bounds on coefficients.

Error in ==> fit at 448
errstr = handleerr('curvefit:fit:complexValueComputed',errmsg,suppresserr);
===========================


I do not prefer to use curve fitting toolbox, although it is a nice tool, I need to include the fitting as a part of a long and iterating code. Therefore I prefer to use custom model fitting.

Concerning the error message I have played with the upper and lower bounds. The error message was still there...

Ideas will be highly appreciated.

Cheers,
Oznur
From: Oznur Mete on
"Oznur Mete" <oznur.mete(a)cern.ch> wrote in message <hmqqmt$4v8$1(a)fred.mathworks.com>...
> Dear All,
>
> I am trying to define a custom fit model and fit to the data.
>
> Here is the piece of my code:
> ======================================
> s = fitoptions('Method','NonlinearLeastSquares',...
> 'Lower',[0.9,0.9],...
> 'Upper',[1.1,1.1], ...
> 'Startpoint',[1 1]);
> f = fittype('a*exp(b*sqrt(sind(x)))','coefficients',{'a','b'},'independent','x','options', s);
> ph1= ph1(:);
> ch1 = ch1(:);
> [c2,gof2] = fit(ph1,ch1,f);
> =====================================
>
>
> I received the following error message:
> ===================================
> ??? Complex value computed by model function, fitting cannot continue.
> Try using or tightening upper and lower bounds on coefficients.
>
> Error in ==> fit at 448
> errstr = handleerr('curvefit:fit:complexValueComputed',errmsg,suppresserr);
> ===========================
>
>
> I do not prefer to use curve fitting toolbox, although it is a nice tool, I need to include the fitting as a part of a long and iterating code. Therefore I prefer to use custom model fitting.
>
> Concerning the error message I have played with the upper and lower bounds. The error message was still there...
>
> Ideas will be highly appreciated.
>
> Cheers,
> Oznur




Typo:
The code will be like this surely (with 'options'):

======================================
> s = fitoptions('Method','NonlinearLeastSquares',...
> 'Lower',[0.9,0.9],...
> 'Upper',[1.1,1.1], ...
> 'Startpoint',[1 1]);
> f = fittype('a*exp(b*sqrt(sind(x)))','coefficients',{'a','b'},'independent','x','options', s);
> ph1= ph1(:);
> ch1 = ch1(:);
> [c2,gof2] = fit(ph1,ch1,f,'options',s);
> =====================================



Oznur
From: Steven Lord on

"Oznur Mete" <oznur.mete(a)cern.ch> wrote in message
news:hmqqmt$4v8$1(a)fred.mathworks.com...
> Dear All,
>
> I am trying to define a custom fit model and fit to the data.
>
> Here is the piece of my code:
> ======================================
> s = fitoptions('Method','NonlinearLeastSquares',...
> 'Lower',[0.9,0.9],...
> 'Upper',[1.1,1.1], ...
> 'Startpoint',[1 1]);
> f =
> fittype('a*exp(b*sqrt(sind(x)))','coefficients',{'a','b'},'independent','x','options',
> s);

Note that if x contains a value whose sine (when it's treated as an angle in
degrees) is negative, your function will return a complex value. That's
what the error message complained about.

> ph1= ph1(:);
> ch1 = ch1(:);
> [c2,gof2] = fit(ph1,ch1,f);
> =====================================
>
>
> I received the following error message:
> ===================================
> ??? Complex value computed by model function, fitting cannot continue.
> Try using or tightening upper and lower bounds on coefficients.
>
> Error in ==> fit at 448
> errstr =
> handleerr('curvefit:fit:complexValueComputed',errmsg,suppresserr);
> ===========================
>
>
> I do not prefer to use curve fitting toolbox, although it is a nice tool,
> I need to include the fitting as a part of a long and iterating code.
> Therefore I prefer to use custom model fitting.

Well, the functions FITOPTIONS, FITTYPE, and FIT are all part of Curve
Fitting Toolbox ... what I think you mean is that you don't want to use the
Curve Fitting Toolbox GUI CFTOOL, right?

If you're not aware of this feature, I'd like to let you know you could use
the CFTOOL GUI to experiment with getting the fit to look and act the way
you wanted it, then generate code from the GUI for use in your "long and
iterating code".

http://www.mathworks.com/access/helpdesk/help/toolbox/curvefit/bqxoya6.html

> Concerning the error message I have played with the upper and lower
> bounds. The error message was still there...

In some cases, adjusting the bounds on the parameters could avoid the
function returning a complex value.

f = fittype('sqrt(a*x)', 'coefficients', 'a', 'independent', 'x')

Assuming all your x values are nonnegative, if your original lower bound on
a included negative numbers, changing the lower bound to be at least 0 would
resolve the "Complex value computed by model function" issue. But in this
case, it won't.

> Ideas will be highly appreciated.

Change the form of your function so that it doesn't try to compute the
square root of a negative value or change your phi data so it doesn't
contain any values whose sind is negative.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Oznur Mete on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hmr3rs$ctv$1(a)fred.mathworks.com>...
>
> "Oznur Mete" <oznur.mete(a)cern.ch> wrote in message
> news:hmqqmt$4v8$1(a)fred.mathworks.com...
> > Dear All,
> >
> > I am trying to define a custom fit model and fit to the data.
> >
> > Here is the piece of my code:
> > ======================================
> > s = fitoptions('Method','NonlinearLeastSquares',...
> > 'Lower',[0.9,0.9],...
> > 'Upper',[1.1,1.1], ...
> > 'Startpoint',[1 1]);
> > f =
> > fittype('a*exp(b*sqrt(sind(x)))','coefficients',{'a','b'},'independent','x','options',
> > s);
>
> Note that if x contains a value whose sine (when it's treated as an angle in
> degrees) is negative, your function will return a complex value. That's
> what the error message complained about.
>
> > ph1= ph1(:);
> > ch1 = ch1(:);
> > [c2,gof2] = fit(ph1,ch1,f);
> > =====================================
> >
> >
> > I received the following error message:
> > ===================================
> > ??? Complex value computed by model function, fitting cannot continue.
> > Try using or tightening upper and lower bounds on coefficients.
> >
> > Error in ==> fit at 448
> > errstr =
> > handleerr('curvefit:fit:complexValueComputed',errmsg,suppresserr);
> > ===========================
> >
> >
> > I do not prefer to use curve fitting toolbox, although it is a nice tool,
> > I need to include the fitting as a part of a long and iterating code.
> > Therefore I prefer to use custom model fitting.
>
> Well, the functions FITOPTIONS, FITTYPE, and FIT are all part of Curve
> Fitting Toolbox ... what I think you mean is that you don't want to use the
> Curve Fitting Toolbox GUI CFTOOL, right?
>
> If you're not aware of this feature, I'd like to let you know you could use
> the CFTOOL GUI to experiment with getting the fit to look and act the way
> you wanted it, then generate code from the GUI for use in your "long and
> iterating code".
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/curvefit/bqxoya6.html
>
> > Concerning the error message I have played with the upper and lower
> > bounds. The error message was still there...
>
> In some cases, adjusting the bounds on the parameters could avoid the
> function returning a complex value.
>
> f = fittype('sqrt(a*x)', 'coefficients', 'a', 'independent', 'x')
>
> Assuming all your x values are nonnegative, if your original lower bound on
> a included negative numbers, changing the lower bound to be at least 0 would
> resolve the "Complex value computed by model function" issue. But in this
> case, it won't.
>
> > Ideas will be highly appreciated.
>
> Change the form of your function so that it doesn't try to compute the
> square root of a negative value or change your phi data so it doesn't
> contain any values whose sind is negative.
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>



Thanks Steve!

Yes, I have just realized that the error's gone when I changed my x-values, due to the reason you explained above.
Yes I did not want to use the GUI but seems I can go to this way and extract the code.

Cheers,
Oznur