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 linear fit parameters of separable least squares fit

Subject: Adding constraints to linear fit parameters of separable least squares fit

From: Tazmusica

Date: 19 Jul, 2009 17:37:02

Message: 1 of 3

I was wondering if anyone can help me to figure out how to add equality and inequality constraints to the linear parts of a separable least squares fit. I saw fminspleas is able to do it for the nonlinear fit parameters (using transformations from a constrained problem to an unconstrained one), but not for the linear fit parameters. Here is my code for the separable least squares fit. Thanks.


function[NLP,LP,res]=seplstsq(funlist,start_point,xdata,ydata,options)


function [sse,LP,res]=seplstsq_sse(NLP)
    DM=zeros(length(ydata),length(funlist));
    
    for ii=1:length(funlist);
        findx=funlist{ii};
        term=findx(NLP,xdata);
        DM(:,ii)=term(:);
    end
    
    LP=DM\ydata;
    res=DM*LP-ydata;
    sse=sum(res.^2);
    
end

NLP=fminsearch(@seplstsq_sse,start_point,options);
[crap,LP,res]=seplstsq_sse(NLP);

end

Subject: Adding constraints to linear fit parameters of separable least squares fit

From: John D'Errico

Date: 19 Jul, 2009 21:21:01

Message: 2 of 3

"Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <h3vlju$s5f$1@fred.mathworks.com>...
> I was wondering if anyone can help me to figure out how to add equality and inequality constraints to the linear parts of a separable least squares fit. I saw fminspleas is able to do it for the nonlinear fit parameters (using transformations from a constrained problem to an unconstrained one), but not for the linear fit parameters. Here is my code for the separable least squares fit. Thanks.
>
>
> function[NLP,LP,res]=seplstsq(funlist,start_point,xdata,ydata,options)
>
>
> function [sse,LP,res]=seplstsq_sse(NLP)
> DM=zeros(length(ydata),length(funlist));
>
> for ii=1:length(funlist);
> findx=funlist{ii};
> term=findx(NLP,xdata);
> DM(:,ii)=term(:);
> end
>
> LP=DM\ydata;
> res=DM*LP-ydata;
> sse=sum(res.^2);
>
> end
>
> NLP=fminsearch(@seplstsq_sse,start_point,options);
> [crap,LP,res]=seplstsq_sse(NLP);
>
> end

Use lsqlin to solve the internal constrained linear least
squares problem. It can use constraints. In fact if you
have mixed LINEAR constraints that employ one or more
of both classes of parameter, then you can define linear
inequality or equality constraints for the internal lsqlin
problem. The constant term in these internal linear
constraints will be defined by the current value of the
nonlinear parameters.

The only thing you cannot do is to implement mixed
nonlinear constraints that employ some of each class
of parameter.

John

Subject: Adding constraints to linear fit parameters of separable least squares fit

From: Tazmusica

Date: 21 Jul, 2009 15:45:03

Message: 3 of 3

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <h402nt$man$1@fred.mathworks.com>...
> "Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <h3vlju$s5f$1@fred.mathworks.com>...
> > I was wondering if anyone can help me to figure out how to add equality and inequality constraints to the linear parts of a separable least squares fit. I saw fminspleas is able to do it for the nonlinear fit parameters (using transformations from a constrained problem to an unconstrained one), but not for the linear fit parameters. Here is my code for the separable least squares fit. Thanks.
> >
> >
> > function[NLP,LP,res]=seplstsq(funlist,start_point,xdata,ydata,options)
> >
> >
> > function [sse,LP,res]=seplstsq_sse(NLP)
> > DM=zeros(length(ydata),length(funlist));
> >
> > for ii=1:length(funlist);
> > findx=funlist{ii};
> > term=findx(NLP,xdata);
> > DM(:,ii)=term(:);
> > end
> >
> > LP=DM\ydata;
> > res=DM*LP-ydata;
> > sse=sum(res.^2);
> >
> > end
> >
> > NLP=fminsearch(@seplstsq_sse,start_point,options);
> > [crap,LP,res]=seplstsq_sse(NLP);
> >
> > end
>
> Use lsqlin to solve the internal constrained linear least
> squares problem. It can use constraints. In fact if you
> have mixed LINEAR constraints that employ one or more
> of both classes of parameter, then you can define linear
> inequality or equality constraints for the internal lsqlin
> problem. The constant term in these internal linear
> constraints will be defined by the current value of the
> nonlinear parameters.
>
> The only thing you cannot do is to implement mixed
> nonlinear constraints that employ some of each class
> of parameter.
>
> John

John,
Thank you for your help. I am running into an issue where if I include bounds for my fits, Matlab is using 98% of my CPU and shows the "Busy" message indefinitely (for example when I give lower bounds as zero and no upper bounds). Yet when lb and ub are set to [], the fit on my test data is fine. For the test data, the linear parameter is >0, so I am not sure why it would behave this way. I am not sure what I am doing wrong, if my syntax is ok, and would appreciate any help. Here is the code:

function[NLP,LP,LPresnorm,LPres,lambda]=seplstsqcon(funlist,start_point,xdata,ydata,lb,ub,options)


function [LPresnorm,LPres,lambda]=seplstsqcon_sse(NLP)
    DM=zeros(length(ydata),length(funlist));
    
    for ii=1:length(funlist);
        findx=funlist{ii};
        term=findx(NLP,xdata);
        DM(:,ii)=term(:);
    end
    
    [LP,LPresnorm,LPres,exitflag,output,lambda]=lsqlin(DM,ydata,[],[],[],[],lb,ub,lb,options);
    
end

NLP=fminsearch(@seplstsqcon_sse,start_point,options);
[crap,LP,LPresnorm,LPres,lambda]=seplstsqcon_sse(NLP);

end

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