Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Start Curve Fitting Tool GUI with initial guesses and bounds already entered

Subject: Start Curve Fitting Tool GUI with initial guesses and bounds already entered

From: Eli

Date: 21 Jun, 2012 17:01:08

Message: 1 of 4

Hi,
I am having some trouble with autoloading the Curve Fitting Toolbox with initial guesses and upper/lower bounds.

WHAT I HAVE DONE:
I performed a fit and generated the code. Part of it is shown below:

function [fitresult, gof] = createFit(x, y)
% Comments Removed for brevity
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( x, y );
 
% Set up fittype and options.
ft = fittype( 'equation removed for brevity', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( ft );
opts.Display = 'Off';
opts.Lower = [0 0 0 -Inf -Inf 11.45 11.9 9.5 0 0 0];
opts.StartPoint = [0.0758542895630636 0.0539501186666072 0.530797553008973 0.779167230102011 0.934010684229183 11.59 12 10.4 0.2 0.2 0.455];
opts.Upper = [Inf Inf Inf Inf Inf 11.7 12.1 11.5 0.3 0.3 Inf];
ex = excludedata( xData, yData, 'Indices', [1 2 3 4 5......numbers reduced for brevity.....200] );
opts.Exclude = ex;
 
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

--------------------X---------------------

REASON FOR AUTOSTARTING:
I had to repeat this fit for other data with the same initial guesses, fitting function and upper/lower bounds. As you can see from the last 2 lines, I had to manually select the points that I wanted to exclude. I wanted to avoid this time-consuming procedure and instead open up the cftool with the exclusion already applied (i.e. with the excluded points already removed). To do this I used:
outliers = excludedata(xdata,ydata,'domain',[16 199]); % exclusion range may not make much sense.....just ignore it
cftool( xdata(~outliers),ydata(~outliers))

and it produced the desired result. The cftool opened up and the excluded points were removed from the graph.

PROBLEM:
I then wanted to take this one step further. I had to repeat this fit for other data with the same fitting function (it is a custom equation that is quite long, which is why I removed it from the text above), initial guesses, lower and upper bounds. So I decided to enter the custom equation (into ft), initial guesses, lower bounds and upper bounds as follows (from the command line, by copying and pasting the generated code from above):

ft = fittype( 'equation removed for brevity', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( ft );
opts.Display = 'Off';
opts.Lower = [0 0 0 -Inf -Inf 11.45 11.9 9.5 0 0 0];
opts.StartPoint = [0.0758542895630636 0.0539501186666072 0.530797553008973 0.779167230102011 0.934010684229183 11.59 12 10.4 0.2 0.2 0.455];
opts.Upper = [Inf Inf Inf Inf Inf 11.7 12.1 11.5 0.3 0.3 Inf];

I then saved the script file that I was using and entered this:
cftool( xdata(~outliers),ydata(~outliers), ft, opts)

but it did not produce the desired result. The cftool opened up as before and the excluded points were again excluded as I indicated, but this time a message came up saying the following:

Invalid inputs to CFTOOL

Input must be numeric with two or more elements.
   Ignoring Z-data because itis not numeric
   Ignoring Weights because it is not numeric

Only the exclusion statement was executed. I know this because, like before, the excluded data was not plotted.

I also tried:
cftool( xdata(~outliers, ft, opts),ydata(~outliers, ft, opts))

But it did not work either.

QUESTION:
Is there some way that I can specify the custom fitting equation, initial guesses, lower and upper bounds from the command line similar to how I specified the exclusion range? How should I modify the cftool command to automatically load up cftool with the custom fitting equation, initial guesses, lower and upper bounds entered?

NOTE: I do not want to perform command line curve fitting. I want to use the cftool GUI (to visualize the fits, as I change the parameters in realtime), so I need to start it up, but I need to start it up with the custom fitting equation, initial guesses, lower and upper bounds entered.

Subject: Start Curve Fitting Tool GUI with initial guesses and bounds already entered

From: Eli

Date: 25 Jun, 2012 18:29:07

Message: 2 of 4

I'm still interested in getting help for this. I have tried but can't get this to work.

If you could offer some help or re-direct me to another sub-forum/newsgroup that would be really helpful.

Subject: Start Curve Fitting Tool GUI with initial guesses and bounds already entered

From: Star Strider

Date: 25 Jun, 2012 20:24:07

Message: 3 of 4

"Eli" wrote in message <jsaalj$cb2$1@newscl01ah.mathworks.com>...
> I'm still interested in getting help for this. I have tried but can't get this to work.
>
> If you could offer some help or re-direct me to another sub-forum/newsgroup that would be really helpful.

---------------------------------------------------------------

It might be easier if, instead of the GUI, you used programmatic curve-fitting such as 'lsqnonlin', 'lsqcurvefit' or one of the others that allow parameter bounds. (Unless I overlooked something in the documentation, 'NonLinearModel.fit' and 'nlinfit' don't allow parameter bounds.)

The problem I (and perhaps others) have in responding to your question is that you stated:

'QUESTION:
Is there some way that I can specify the custom fitting equation, initial guesses, lower and upper bounds from the command line similar to how I specified the exclusion range?'

and then:

'NOTE: I do not want to perform command line curve fitting.'

So I'm not sure what you actually want to do. Programmatic curve-fitting (from a script, not the command line) is easy enough and will do what you want, even if it's not quite as straightforward as using the GUI. I've only done programatic curve-fitting and haven't had any problems with it.

Your use of 'Inf' and '-Inf' in your bounds also makes me a bit queasy. Why not '1E+100' and '-1E+100' or some such instead? It would achieve the same essential effect and wouldn't produce an unbounded or undefined result if your objective function wandered off into the wilderness.

Subject: Start Curve Fitting Tool GUI with initial guesses and bounds already entered

From: Eli

Date: 26 Jun, 2012 15:47:07

Message: 4 of 4

"Star Strider" wrote in message <jsahd7$eib$1@newscl01ah.mathworks.com>...
> The problem I (and perhaps others) have in responding to your question is that you stated:
>So I'm not sure what you actually want to do. Programmatic curve-fitting (from a script, not the command line) is easy enough and will do what you want, even if it's not quite as straightforward as using the GUI. I've only done programatic curve-fitting and haven't had any problems with it.
--------------------x---------------------------------

You've got it. That's exactly what I do not want to do.....programmatic curve-fitting. I want the GUI approach.

I guess I am kind of pushing it........I got the exclusion rule to be incorporated into the script I posted above so I was hoping to take it one step further.

Despite not being my preference, I am aware of the programmatic approach. Having spent a lot of time trying, I'm actually leaning quite strongly towards just going with that approach and getting it over with. My preferred approach is proving to be far too difficult.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us