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:
adding constraints to curve fitting / least squares code?

Subject: adding constraints to curve fitting / least squares code?

From: Andres

Date: 9 Mar, 2013 10:24:10

Message: 1 of 2

Hello.

I am using the curve fitting tool's generated code to obtain coefficient values for different functions that I'm adjusting to some data.

One problem I often encounter with the data sets, though, is that the best fit that the code finds for the coefficients leads to a negative slope at the very beginning of the data. Since I'm doing this to obtain a material model, this initial negative slope doesn't really make sense and will cause computing problems when I use the coefficient values in a model. So the ideal solution would be to make the code such that the slope is always positive in the range of the minimum and maximum x-values.

I can solve this by constraining the coefficient values either on CFTOOL or in the code itself. But I'd prefer to do it automatically, by a code, since this way I have to keep rewriting things. Is there an 'easy' way of doing this (i.e. adding a code to the current cftool-generated code that forces a condition), or would I have to start from scratch?

This is a sample of the code:

% --- Create fit "Mooney-Rivlin 2"
fo_ = fitoptions('method','NonlinearLeastSquares','Upper',[4.5 Inf]);
ok_ = isfinite(x) & isfinite(y);
if ~all( ok_ )
    warning( 'GenerateMFile:IgnoringNansAndInfs',...
        'Ignoring NaNs and Infs in data.' );
end
st_ = [0.78695148204232868 0.66701749178803094 ];
set(fo_,'Startpoint',st_);
ft_ = fittype('2*(x^2-x^-1)*(A+(B/x))',...
    'dependent',{'y'},'independent',{'x'},...
    'coefficients',{'A', 'B'});

% Fit this model using new data
[cf_,gof] = fit(x(ok_),y(ok_),ft_,fo_);
moon2 = coeffvalues(cf_);

Any help would be appreciated.

Subject: adding constraints to curve fitting / least squares code?

From: John D'Errico

Date: 9 Mar, 2013 21:53:08

Message: 2 of 2

"Andres " <adelahoz@gmail.com> wrote in message <khf2ka$mtq$1@newscl01ah.mathworks.com>...
> Hello.
>
> I am using the curve fitting tool's generated code to obtain coefficient values for different functions that I'm adjusting to some data.
>
> One problem I often encounter with the data sets, though, is that the best fit that the code finds for the coefficients leads to a negative slope at the very beginning of the data. Since I'm doing this to obtain a material model, this initial negative slope doesn't really make sense and will cause computing problems when I use the coefficient values in a model. So the ideal solution would be to make the code such that the slope is always positive in the range of the minimum and maximum x-values.
>
> I can solve this by constraining the coefficient values either on CFTOOL or in the code itself. But I'd prefer to do it automatically, by a code, since this way I have to keep rewriting things. Is there an 'easy' way of doing this (i.e. adding a code to the current cftool-generated code that forces a condition), or would I have to start from scratch?
>
> This is a sample of the code:
>

(snip)

>
> Any help would be appreciated.

Use a tool (SLM) that is designed from the start to help
you do this.

http://www.mathworks.com/matlabcentral/fileexchange/24443-slm-shape-language-modeling

If you want the slope to be nonnegative at the left end, there is a
simple way to specify that.

HTH,
John

Tags for this Thread

No tags are associated with 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