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
 Origin Forum
 Fit parameter initialization problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

a.abc.b35

175 Posts

Posted - 05/19/2011 :  6:31:34 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
I have a strange problem. I have a fitting function, say ,
y = A*cos(x-x0)^2, where x, x0 are in radians.
.....
Now I generated a dataset, using the same function,in origin, with x from 0 to 2*PI, A =10, x0 = PI/2 and plotted it. So if I fit the above function to the graph generated from this graph, I should get A = 10 and x0 = PI/2. If I manually put any parameter and fit until converge, I do get this result !
..
Now what I did is, I set a auto initialization code as below :

#include <origin.h>
void _nlsfParamSD_cosSq(
// Fit Parameter(s):
double& A, double& x0,
// Independent Dataset(s):
vector& x_data,
// Dependent Dataset(s):
vector& y_data,
// Curve(s):
Curve x_y_curve,
// Auxilary error code: 
int& nErr)
{
	// Beginning of editable part
	//Code to be executed to initialize parameters
	vector& xcos;
	x0 = PI/2.0;
	int maxindex = x_data.GetSize();
	
	for (int i =0; i < maxindex; i++)
	{
	xcos[i] = cos(x_data[i] - x0)^2;
	}
	
	int nOrder =1;
	vector coeff;
	coeff.SetSize(nOrder+1);
	
	fitpoly(xcos,y_data,nOrder,coeff);
	
	A = coeff[1];

}


I used the fitpoly function to initialize the fit.
....
If I compile it, it compiles ok.
Now when I go the the fitting window by ctrl +Y, I would expect the fit to be almost there along with the data. But what I get is no fitted curve. And if i hit the parameter initialization button , I get the following error :
Error during compilation for parameters init. at the middle of NLFit window, just above the buttons. My initialization code will never run !! Again, if I manually put the values of A and x0 (say I put 9, 1), the code can be iterated and finally get the desired value of 10, PI/2. Can anyone tell me what's happening ? Does it have to do something with linear and non linear fit ?

AB

a.abc.b35

175 Posts

Posted - 05/19/2011 :  8:06:41 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
By the way, I get the same error in Origin 8 SR6 also !

AB
Go to Top of Page

minimax

348 Posts

Posted - 05/20/2011 :  01:51:17 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi AB,

The init code needs a bit modification.


{
	// Beginning of editable part
	//Code to be executed to initialize parameters
	vector xcos;
	x0 = PI/2.0;
	int maxindex = x_data.GetSize();
	xcos.SetSize(maxindex);
	for (int i =0; i < maxindex; i++)
	{
	xcos[i] = cos(x_data[i] - x0)^2;
	}
	
	int nOrder =1;
	vector coeff;
	coeff.SetSize(nOrder+1);
	
	fitpoly(xcos,y_data,nOrder,coeff);
	
	A = coeff[1];

}


Max
OriginLab Technical Support
Go to Top of Page

a.abc.b35

175 Posts

Posted - 05/20/2011 :  02:06:56 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks a lot Max. So the problem was not setting the size of the vector ? Or there is more to it ? Please enlighten me. Thanks again.

AB
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