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