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:
Contraint in curve fitting optimization

Subject: Contraint in curve fitting optimization

From: Kapila Bandara

Date: 1 Sep, 2013 07:16:32

Message: 1 of 3

i want to know how can i put constraint in curve fitting optimization function.

For example i want to give constraint as 0<beta <1 in following function.

.......................................................
function [estimates, model] = fitcurvedemo(xdata, ydata)
% Call fminsearch with a random starting point.
start_point = rand(1,4 );
model = @expfun;
estimates = fminsearch(model, start_point);
% expfun accepts curve parameters as inputs, and outputs sse,
% the sum of squares error for A*exp(-lambda*xdata)-ydata,
% and the FittedCurve. FMINSEARCH only needs sse, but we want
% to plot the FittedCurve at the end.
    function [sse, FittedCurve] = expfun(params)
        A = params(1);
        lambda = params(2);
        B=params(3);
        beta=params(4);
        FittedCurve = (A .* exp(-lambda * xdata) +B.*xdata.^beta);
        ErrorVector = FittedCurve - ydata;
        sse = sum(ErrorVector .^ 2);
    end
end

Subject: Contraint in curve fitting optimization

From: Alan_Weiss

Date: 1 Sep, 2013 15:41:34

Message: 2 of 3

On 9/1/2013 3:16 AM, Kapila Bandara wrote:
> i want to know how can i put constraint in curve fitting optimization function.
>
> For example i want to give constraint as 0<beta <1 in following function.
>
> .......................................................
> function [estimates, model] = fitcurvedemo(xdata, ydata)
> % Call fminsearch with a random starting point.
> start_point = rand(1,4 );
> model = @expfun;
> estimates = fminsearch(model, start_point);
> % expfun accepts curve parameters as inputs, and outputs sse,
> % the sum of squares error for A*exp(-lambda*xdata)-ydata,
> % and the FittedCurve. FMINSEARCH only needs sse, but we want
> % to plot the FittedCurve at the end.
> function [sse, FittedCurve] = expfun(params)
> A = params(1);
> lambda = params(2);
> B=params(3);
> beta=params(4);
> FittedCurve = (A .* exp(-lambda * xdata) +B.*xdata.^beta);
> ErrorVector = FittedCurve - ydata;
> sse = sum(ErrorVector .^ 2);
> end
> end

You can use fmincon instead of fminsearch if you have Optimization Toolbox.

Or, for this simple constraint, use exp(-beta^2) where you currently
have beta in FittedCurve. exp(-beta^2) is always between 0 and 1. I
recommend you start beta out with a value of 1, not something close to 0
or higher than 2.

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: Contraint in curve fitting optimization

From: Kapila Bandara

Date: 2 Sep, 2013 09:28:55

Message: 3 of 3

On Monday, September 2, 2013 1:41:34 AM UTC+10, Alan_Weiss wrote:
> On 9/1/2013 3:16 AM, Kapila Bandara wrote:
>
> > i want to know how can i put constraint in curve fitting optimization function.
>
> >
>
> > For example i want to give constraint as 0<beta <1 in following function.
>
> >
>
> > .......................................................
>
> > function [estimates, model] = fitcurvedemo(xdata, ydata)
>
> > % Call fminsearch with a random starting point.
>
> > start_point = rand(1,4 );
>
> > model = @expfun;
>
> > estimates = fminsearch(model, start_point);
>
> > % expfun accepts curve parameters as inputs, and outputs sse,
>
> > % the sum of squares error for A*exp(-lambda*xdata)-ydata,
>
> > % and the FittedCurve. FMINSEARCH only needs sse, but we want
>
> > % to plot the FittedCurve at the end.
>
> > function [sse, FittedCurve] = expfun(params)
>
> > A = params(1);
>
> > lambda = params(2);
>
> > B=params(3);
>
> > beta=params(4);
>
> > FittedCurve = (A .* exp(-lambda * xdata) +B.*xdata.^beta);
>
> > ErrorVector = FittedCurve - ydata;
>
> > sse = sum(ErrorVector .^ 2);
>
> > end
>
> > end
>
>
>
> You can use fmincon instead of fminsearch if you have Optimization Toolbox.
>
>
>
> Or, for this simple constraint, use exp(-beta^2) where you currently
>
> have beta in FittedCurve. exp(-beta^2) is always between 0 and 1. I
>
> recommend you start beta out with a value of 1, not something close to 0
>
> or higher than 2.
>
>
>
> Alan Weiss
>
> MATLAB mathematical toolbox documentation

Thank you very much it is working

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