The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Nonlinear constraints for custom fitt funct
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

millot.marius

USA
38 Posts

Posted - 09/06/2012 :  6:50:09 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin 8.5.1 win vista

Hi

I d like to fit a dataset with a piecewise function being quadratic up to threshold_1, cubic between threshold1 and threshold2 and again quadratic above threshold2

However the piecewise fitting function must be continuous as well as its first derivative

I tried ti define the function like this


// 
void _nlsfTestFitPiecewise(
// Fit Parameter(s):
double a0, double a1, double a2, double b0, double b1, double b2, double b3, double c0,
double c1, double c2, double d0, double d1,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
	// Beginning of editable part
	if (x<d0)
	y = a0+a1*x+a2*x^2;
	else 
		if (x<d1)
	y = b0+b1*x+b2*x^2+b3*x^3;
	else 
		y=c0+c1*x+c2*x^2;
		
		
	// End of editable part
}


This works fine but i don't know how to implement the continuity of the function and its derivative that is to say the following constraints


a0+a1*d0+a2*d0^2=b0+b1*d0+b2*d0^2+b3*d0^3;
a1+2*a2*d0=b1+2*b1*d0+3*b3*d0^2;
c0+c1*d1+c2*d1^2=b0+b1*d1+b2*d1^2+b3*d1^3;
c1+2*c2*d1=b1+2*b1*d1+3*b3*d1^2;

Sam Fang

292 Posts

Posted - 09/07/2012 :  05:10:50 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You can use meaningful parameters, and in this way nonlinear constraints are not necessary. e.g.


threshold_1: d0
y value at threshold_1: b0
derivative at threshold_1: k0
threshold_2: d1
y value at threshold_2: b1
derivative at threshold_2: k1

two extra parameters:
parameter for the first quadratic: a
parameter for the second quadratic: c


Use parameters a, d0, k0 to define the first quadratic.
Use parameters c, d1, k1 to define the second quadratic.
Use parameters d0, k0, d1, k1 to define the cubic.

Fitting Function can be defined as follows:
-------------------------------------

  if( x<d0 )
    y = b0 + k0*(x-d0) + a*(x-d0)^2;
  else if( x <d1 )
    y = b0*(x-d1)/(d0-d1) + b1*(x-d0)/(d1-d0) + (k0-(b1-b0)/(d1-d0))*(x-d0)*(x-d1)^2/(d0-d1)^2
      + (k1-(b1-b0)/(d1-d0))*(x-d0)^2*(x-d1)/(d1-d0)^2;
  else
    y = b1 + k1*(x-d1) + c*(x-d1)^2;

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

You can apply a constraint:
d0 < d1

Sam
OriginLab Technical Services

Edited by - Sam Fang on 09/07/2012 10:26:47 AM
Go to Top of Page

millot.marius

USA
38 Posts

Posted - 09/07/2012 :  12:37:54 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thank you Sam, this works!

Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000