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
 fitting to recursive functions
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

jupp

Germany
6 Posts

Posted - 07/14/2005 :  08:56:17 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.0/7.5
Operating System: Win2000

Hi everybody,

I have a question concerning fitting data to the following equation in Origin (this is a simplified example):

Dependent variable: y
Independent variable: x
Parameter: a

y(x)= a*x + y(x-1);

I think this is called a recursive function, but I may be wrong
Anyway, in order to calculate the current value of y(x), I need access to the previous value of y, i.e. y(x-1). I tried to solve the problem using the index variable [i-1], but this does not seem to work in the NLS fitter. Is there a way to temporarily store the value of y and retrieve it in the next calculation step? Any help is appreciated.

Best regards,

Jupp

Hideo Fujii

USA
1582 Posts

Posted - 07/14/2005 :  10:45:37 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Jupp,

If I understand your problem correctly, your function could be written with no recursion. You see that:
y(x)=a*x+y(x-1)
y(x-1)=a*(x-1)+y(x-2)
...
y(2)=a*2+y(1)
Therefore,
y(x)=a*x+a*(x-1)+...+a*2+y(1)
= a*(x+(x-1)+...+2)+y(1)
= a*(x^2-4)+y(1)
Say, y(1)=b
y(x)=a*(x^2-4)/2+b

Hope I didn't miss your point...

OriginLab
Hideo Fujii

Edited by - Hideo Fujii on 07/14/2005 10:54:40 AM
Go to Top of Page

jupp

Germany
6 Posts

Posted - 07/14/2005 :  11:38:54 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Hideo,

although your solution may solve the problem I posted, my "real" equation is more complicated. That's why I wrote "simplified"
in the introductory sentence. I fear there is no workaround for this one:

dependent variable: Q
independent variables: x1, x2, x3
Parameters: K, n, H
Constant: V

double A, B, C; // Temporary variables
A = (-x2 + x1 + K);
B = (-A + sqrt(A^2 + 4*K*x2))/2;
C = (x2 - B) * H * V;

Q = C + x3/V * ((C + P)/2) - P;

The P in the last equation is defined as follows:
P(i) = C(i-1), i.e. the value of C from the previous "loop"
This is the recursive part of the fit function which causes me so much headache.

My plan was to store the value of C in some 'global' variable every time it is calculated, and then assign P the value of this variable before Q is calculated. But I don't know how to achieve this.

In C code, it would look like this

double temp=0;
for {....
C=....
P= temp;
Q = ....;
temp = C;
}
I hope you get my idea.

Best regards,

Jupp
Go to Top of Page

Hideo Fujii

USA
1582 Posts

Posted - 07/14/2005 :  3:42:15 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Jupp,

Your x1, x2, and x3 probably cannot be solely independent, because, for example, even exactly the same value set of x1,...xn, it might give different Q value depending on the previous state (i (=time?)). Your function seems dynamic.

Someone who has strong mathematical background can give you better answer. Sorry, but this is just my general feeling...

Hideo



Edited by - Hideo Fujii on 07/14/2005 3:45:00 PM
Go to Top of Page

jupp

Germany
6 Posts

Posted - 07/15/2005 :  04:53:52 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Hideo,

I may have found what I need. When I open the fit function in the code builder window, there is an editable section in the very beginning for placing C code and other functions used for fitting (right after the #include statements). When I define a variable there, it appears to me that the fitting process regards this variable as global. In other words, it is initialized only once at the beginning of the fitting process. Is this correct?
Go to Top of Page

easwar

USA
1964 Posts

Posted - 07/15/2005 :  09:09:37 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:

...When I open the fit function in the code builder window, there is an editable section in the very beginning for placing C code and other functions used for fitting (right after the #include statements). When I define a variable there, it appears to me that the fitting process regards this variable as global. In other words, it is initialized only once at the beginning of the fitting process. Is this correct?



Hi Jupp,

Yes, that is correct. You can define global variables as you described.

However please note that your fitting function code is called by the minimization routine not only for each data point but also when a fitting parameter is changed. The minimization routine could be calling the function for change in x and change in parameter alternately, meaning that every consecutive call to your function may not be for the next data point, but could be for the same data point with a different set of parameters etc.

So currently the fitter is not set up to perform recursive fitting.

Easwar
OriginLab

Go to Top of Page

jupp

Germany
6 Posts

Posted - 07/15/2005 :  11:01:18 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:

Hi Jupp,

Yes, that is correct. You can define global variables as you described.

However please note that your fitting function code is called by the minimization routine not only for each data point but also when a fitting parameter is changed. The minimization routine could be calling the function for change in x and change in parameter alternately, meaning that every consecutive call to your function may not be for the next data point, but could be for the same data point with a different set of parameters etc.

So currently the fitter is not set up to perform recursive fitting.

Easwar
OriginLab




I see. It's a pity that the trick with the global variable won't work. Thanks for your help.

Jupp

Go to Top of Page

Hideo Fujii

USA
1582 Posts

Posted - 07/18/2005 :  10:44:39 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Jupp,

The following possible method has come up to my mind,
though I'm not sure how theoretically okay, or not.
I wait for someone's further comments.

I assume that your x1, x2, and x3 are in the columns
as given in your worksheet, and your recursive function
can be written in the following way:
A(i)=f(x1(i),x2(i))
B(i)=g(x1(i),x2(i))
C(i)=h(x1(i),x2(i))
Thus, Q(i)=q(x1(i),x2(i),x3(i),x1(i-1),x2(i-1))
Here, q has parameters, K, n, and H, and you get its
exact formula.
You can organize your worksheet like:
i:= 1, 2, 3, .....
x1(i):=x1(1), x1(2), x1(3),...
x2(i):=x2(1), x2(2), x2(3),...
x3(i):=x3(1), x3(2), x3(3),...
x1(i-1):=--, x1(1), x1(2), x1(3),...
x2(i-1):=--, x2(1), x2(2), x2(3),...
Q(i):=q(1), q(2), q(3),...
That is, you make two extra columns, which represent
the previous states of x1 and x2.
Then, you can use the fitter in regular way on these
five independent variables and a dependent variable
at i>=2.

Hope it helps even a little bit in your challenge...

--Hideo



Edited by - Hideo Fujii on 07/18/2005 10:49:35 AM
Go to Top of Page

jupp

Germany
6 Posts

Posted - 07/18/2005 :  3:54:13 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Hideo,

your method works great! I just had to modify both spreadsheet and fit function according to your suggestions, and the fit converged! Thanks a lot.

Over the week-end, I came up with an alternative solution that also seems to work. As Easwar pointed out, the problem with my previous approach was that I didn't know which line of the dataset the NLSF fitter is currently processing.
The trick in my new appraoch is to use the 'nlsf.currow' variable to retrieve this information. It corresponds to the index [i] used for spreasheet operations. I also defined an additional parameter, Q0, to determine the value of Q(x1=0, x2=0).

double b[size_of_dataset]; // global array to hold the values of Q(x1, x2)

// here starts the code for the fit function
using nlsf = LabTalk.NLSF;
double Q;
int row = nlsf.currow; // retrieve current row number

b[0] = Q0; // initialize first element of array, which is of unknown value
Q = f(x1, x2);
b[row] = Q; // store current value of Q in array element corresponding to row number

dQ = Q + g(x3) * ((Q + b[row-1])/2) - b[row-1];

It looks like both procedures, yours and mine, are working, although the values of the parameters after fitting deviate slightly. I am not sure whether I should be worried about this, though.

Stefan



Edited by - jupp on 07/18/2005 3:57:51 PM
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