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:
fitting a monotonic polynomial using fmincon

Subject: fitting a monotonic polynomial using fmincon

From: Anand Anand

Date: 3 Oct, 2010 06:00:22

Message: 1 of 6

I am trying to fit a cubic monotonic polymonial between say certain data values of variable G and corresponding variables for B. My objective function was
f=sum(G-a(1)+a(2)*B+a(3)*B.^2+a(4)*B.^3;);
and the non linear inequality constraints for monotonicity were like
a(3).^2-3*a(2)*a(4)<0;
and a(4)>0 which is -a(4)<0;
Is that all?
Nevertheless, I get a monotonic function but it is so different from the one obtained in the technical paper I am working on.any suggestions?

Subject: fitting a monotonic polynomial using fmincon

From: Matt J

Date: 3 Oct, 2010 13:24:04

Message: 2 of 6

"Anand Anand" <tranand3@gmail.com> wrote in message <i8965m$3rb$1@fred.mathworks.com>...


> My objective function was
> f=sum(G-a(1)+a(2)*B+a(3)*B.^2+a(4)*B.^3;);
===============

If this is actually your function, it would explain why you have problems. If you are attempting a least squares solution, the sum should be squared.

Failing that, email the author and tell him his paper is bogus!


> and the non linear inequality constraints for monotonicity were like
> a(3).^2-3*a(2)*a(4)<0;
> and a(4)>0 which is -a(4)<0;
=======

you might also have a look at this tool to see if it's relevant to you

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

Subject: fitting a monotonic polynomial using fmincon

From: Roger Stafford

Date: 3 Oct, 2010 18:18:04

Message: 3 of 6

"Anand Anand" <tranand3@gmail.com> wrote in message <i8965m$3rb$1@fred.mathworks.com>...
> I am trying to fit a cubic monotonic polymonial between say certain data values of variable G and corresponding variables for B. My objective function was
> f=sum(G-a(1)+a(2)*B+a(3)*B.^2+a(4)*B.^3;);
> and the non linear inequality constraints for monotonicity were like
> a(3).^2-3*a(2)*a(4)<0;
> and a(4)>0 which is -a(4)<0;
> Is that all?
> Nevertheless, I get a monotonic function but it is so different from the one obtained in the technical paper I am working on.any suggestions?
- - - - - - -
  The constraints a(3).^2-3*a(2)*a(4)<0 and a(4)>0 are requirements that your polynomial be strictly monotone increasing over its enter range from minus infinity to plus infinity. That is a much stricter constraint than merely being monotone increasing over the actual range spanned by the B variable. Are you sure you need such a tight constraint on your polynomial?

  There are at most three values of B at which you need to check for the minimum value of the derivative, a(2)+2*a(3)*B+3*a(4)*B^2, of your polynomial: 1) at the minimum value of B, 2) at the maximum value of B, and 3) at the value B = -a(3)/(3*a(4)) if that is within the B range. If each of these three derivative values is positive, then the polynomial is strictly monotone increasing over the entire B range.

Roger Stafford

Subject: fitting a monotonic polynomial using fmincon

From: siva

Date: 25 Nov, 2013 09:08:06

Message: 4 of 6

"Anand Anand" wrote in message <i8965m$3rb$1@fred.mathworks.com>...
> I am trying to fit a cubic monotonic polymonial between say certain data values of variable G and corresponding variables for B. My objective function was
> f=sum(G-a(1)+a(2)*B+a(3)*B.^2+a(4)*B.^3;);
> and the non linear inequality constraints for monotonicity were like
> a(3).^2-3*a(2)*a(4)<0;
> and a(4)>0 which is -a(4)<0;
> Is that all?
> Nevertheless, I get a monotonic function but it is so different from the one obtained in the technical paper I am working on.any suggestions?

Hi

I am trying to fit a monotonic cubic curve as well.

I hav used t same constraints but my code s giving wrong curve...

main function

n=10;
%figure;
xdata=[1 2 3 4 5 6 7 8 9 10];
ydata=[1 4 9 16 25 36 49 64 81 100];
x0=[1 1 1 1 ] %inital guess
x(1)=2;x(2)=1;x(3)=1; x(4)=1;
[x,fval]=fmincon(@myfunc,x0,[],[],[],[],[],[],@cons);
curve_handle=x;
ans=polyval(curve_handle,xdata);
plot(xdata,ans);

constraint function

function [c,ceq]= cons(x)
% Nonlinear inequality constraints
c=[ -x(3)^2+3*x(2)*x(4); x(4) ];
% Nonlinear equality constraints
ceq=[];

objective function

function y= myfunc(x)
global xdata ydata
y=sum(((x(1)+ (x(2)*xdata) + (x(3)*xdata.^2) + (x(4)*xdata.^3) )-ydata).^2 );

please let me know if i had made any mistake

Subject: fitting a monotonic polynomial using fmincon

From: Roger Stafford

Date: 27 Nov, 2013 19:11:06

Message: 5 of 6

"siva" wrote in message <l6v41m$mm5$1@newscl01ah.mathworks.com>...
> I am trying to fit a monotonic cubic curve as well.
> ......
> function [c,ceq]= cons(x)
> % Nonlinear inequality constraints
> c=[ -x(3)^2+3*x(2)*x(4); x(4) ];
> ......
- - - - - - - -
  I think you have your constraint backwards. You want to have

 x(3)^2-3*x(2)*x(4) <= 0

whereas you actually have

 -x(3)^+3*x(2)*x(4) <= 0.

Roger Stafford

Subject: fitting a monotonic polynomial using fmincon

From: John D'Errico

Date: 27 Nov, 2013 23:30:13

Message: 6 of 6

"siva" wrote in message <l6v41m$mm5$1@newscl01ah.mathworks.com>...
> "Anand Anand" wrote in message <i8965m$3rb$1@fred.mathworks.com>...
> > I am trying to fit a cubic monotonic polymonial between say certain data values of variable G and corresponding variables for B. My objective function was
> > f=sum(G-a(1)+a(2)*B+a(3)*B.^2+a(4)*B.^3;);
> > and the non linear inequality constraints for monotonicity were like
> > a(3).^2-3*a(2)*a(4)<0;
> > and a(4)>0 which is -a(4)<0;
> > Is that all?
> > Nevertheless, I get a monotonic function but it is so different from the one obtained in the technical paper I am working on.any suggestions?
>
> Hi
>
> I am trying to fit a monotonic cubic curve as well.
>
> I hav used t same constraints but my code s giving wrong curve...
>
> main function
>
> n=10;
> %figure;
> xdata=[1 2 3 4 5 6 7 8 9 10];
> ydata=[1 4 9 16 25 36 49 64 81 100];
> x0=[1 1 1 1 ] %inital guess
> x(1)=2;x(2)=1;x(3)=1; x(4)=1;
> [x,fval]=fmincon(@myfunc,x0,[],[],[],[],[],[],@cons);
> curve_handle=x;
> ans=polyval(curve_handle,xdata);
> plot(xdata,ans);
>
> constraint function
>
> function [c,ceq]= cons(x)
> % Nonlinear inequality constraints
> c=[ -x(3)^2+3*x(2)*x(4); x(4) ];
> % Nonlinear equality constraints
> ceq=[];
>
> objective function
>
> function y= myfunc(x)
> global xdata ydata
> y=sum(((x(1)+ (x(2)*xdata) + (x(3)*xdata.^2) + (x(4)*xdata.^3) )-ydata).^2 );
>
> please let me know if i had made any mistake

Why not use a tool that does exactly what you want, as
a cubic spline? It is available on the file exchange.

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

fmincon is not needed, as there is a very nice trick employed
to avoid the need for nonlinear constraints.

If you REALLY desperately want a purely cubic curve and not
a spline, then just use only 2 knots, one at each endpoint of
the interval.

x = rand(1,100);
y = -cos(5*x) + randn(size(x))/3;
pp = slmengine(x,y,'knots',[0 1],'increasing','on','result','pp');

pp.coefs
ans =
       2.1239 -6.3718 6.3718 -1.4214

Or, using a new feature of the SLM tools, we can extract the
actual polynomial as a symbolic toolbox object.

slmpar(pp,'symabs')
ans =
    [1x2 double]
    [1x1 sym ]

ans{2}
ans =
2.0207656227748422850254428340122*x^3 -
6.0622968683245268550763285020366*x^2 +
6.0622968683245259668979088019114*x -
1.2987278885064830191709006612655

I've got to upload the new version of slmpar for this
last to work, so it may not appear until after this
weekend.

John

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