Author |
Topic  |
|
cellbiophysics
S. Korea
Posts |
Posted - 12/10/2007 : 8:16:50 PM
|
Origin Version (Select Help-->About Origin): 7.5 SR4 Operating System: Windows XP
I'm trying to fit the data with a function, which includes summation and integral. The fitting function is
y(x) = P1*sum(n=0 to k){F(n,P2,P3,x)+G(P4,x)*integral[H(n,P2,P3,P4,s),s=0 to x]}.
where, P1..P4 are the fitting parameters. I'm not good at Origin C. After reading several topics in the forum, I wrote some codes for fitting. FOurtunately, it runs. But, it took half a day to fit the data, which is too time-consuming. I want to reduce the fitting time. But, I don't know what to do. My code is as follows.
double dSum = 0; int ser=nsum; double dU = x; double dL = 0.0; double b = dL; int nSteps = nint; double db = (dU-dL)/nSteps; double dF; double dFF; double dInteg = 0.0;
for(int n = 0; n < ser; n++) { for (int ii = 0; ii < nSteps; ii++) { if (ii == 0) dF = H(n,P2,P3,P4,s); else dF = dFF; b += db; dFF = H(n,P2,P3,P4,s); dInteg += 0.5*(dF + dFF)*db; } dSum += F(n,P2,P3,x)+G(P4,x)*dInteg; } y = P1*dSum;
Here I set the values of nsum and nint to fixed ones. For nsum=60 and nint=500, it took more than 8 hours. The number of data was ~ 500. I believe there is someone who help me solve this problem.
Thanks, Kang-Bin.
|
|
Mike Buess
USA
3037 Posts |
Posted - 12/10/2007 : 8:37:53 PM
|
I've never encountered or heard of a fit that takes 8 hours. 500 points and 4 parameters are not a lot. Three of your functions... F(), G() and H() ...are certainly not standard Origin C functions and must have been defined by you. What are they? Perhaps the problem lies with them?
...Also, where are the variables nsum, nint and s declared?
Mike Buess Origin WebRing Member
Edited by - Mike Buess on 12/10/2007 8:45:57 PM |
 |
|
cellbiophysics
S. Korea
Posts |
Posted - 12/10/2007 : 9:10:11 PM
|
Thanks, Mike. Yes, you're right. F, G, and H() are as follows.
F(x) = exp(-P2*x)/(2+n*x/P3)*sqrt(1/(2+n*k*x/P3)), G(x) = exp(-P4*x), and H(s) = exp(-(P2-P4)*s)/(2+n*s/P3)*sqrt(1/(2+n*k*s/P3)).
And nusm and nint is defined as fit parameters. But, they are set to fixed values in the NLSF window (nonlinear curve fitting window). Regarding s, the integral is calculated from 0 to x. As the value of s is varied with x, it is defined in the code builder. If you find anything to modify my equation or code, please let me know.
Thanks, Kang-Bin.
|
 |
|
Mike Buess
USA
3037 Posts |
Posted - 12/10/2007 : 10:31:48 PM
|
quote: Regarding s, the integral is calculated from 0 to x. As the value of s is varied with x, it is defined in the code builder.
That doesn't make sense because s appears nowhere else in your code. However, if you get reasonable (albeit slow) results I'll take your word for it. The bottleneck probably lies in the numerical integration loop and you'll be better off if you prepare a column for s and another for H(s) as described in this article. You would get the fastest results if you could fill the s column with 0 through xmax before fitting. Then for each n you would recalculate the H column w/vector algebra, set the upper limit to the current x (Curve::SetUpperBound) and integrate (Curve_integrate). You'll find some details in the article cited above.
Mike Buess Origin WebRing Member
Edited by - Mike Buess on 12/10/2007 10:33:31 PM |
 |
|
cellbiophysics
S. Korea
Posts |
Posted - 12/10/2007 : 11:13:04 PM
|
Thanks again, Mike.
I missed and mistook something, regarding s. Sorry for this. In my code, s is dU, which is upper limit. So, I set dU to x in my code. I think new column for s should be the same as x. (is it right?) First of all I'll try it as your suggestion. After that, I'll post new reply.
Thanks, Kang-Bin.
|
 |
|
cellbiophysics
S. Korea
Posts |
Posted - 12/10/2007 : 11:45:50 PM
|
Dear Mike,
I'm afraid I couldn't follow even the article you recommended. To understand the example in the article, first I made a data set and created two columns. And I copied and pasted the equation in the article to the code builder. But I don't know where and how I set the limits of the integral in the example. I am new in Origin. Please explain more in detail.
Thanks,
Kang-Bin.
Edited by - cellbiophysics on 12/11/2007 12:01:11 AM |
 |
|
Mike Buess
USA
3037 Posts |
Posted - 12/11/2007 : 12:51:15 PM
|
quote: I am new in Origin. Please explain more in detail.
Good thing you started with something easy. It turns out the example doesn't exactly match your case because you integrate several times during each iteration. The following code might work but I have no data to test with. A worksheet named 'Integral' is required in which column A contains the same data as the X column associated with the data. You can ensure that the worksheet exists by defining the following LabTalk macro in the Before Fit script (Scripts> Before Fit on the NLSF menu bar). Make sure the Enabled option is checked.
# Before Fit script def fitinit { win -t D; // new wks win -r %H Integral; // name it Integral %A = nlsf.x$; // copy X data Integral_A = %A; };
Now try this in your fitting function...
// fitting function Worksheet wks("Integral"); if( !wks ) LT_execute("fitinit"); Dataset dsX("Integral_A"); Dataset dsH("Integral_B"); using nlsf = LabTalk.NLSF; int i = nlsf.currow; // current row being fitted dsX.SetUpperBound(i - 1); dsH.SetUpperBound(i - 1); double dSum = 0; int ser = nsum; for(int n = 0; n < ser; n++) { dsH = H(n,P2,P3,P4,dsX); // fill H(Y) column Curve crv( dsX, dsH ); double dArea = area(crv); // easy integration dSum += F(n,P2,P3,x) + G(P4,x)*dArea; } y = P1*dSum;
Mike Buess Origin WebRing Member
Edited by - Mike Buess on 12/11/2007 1:01:41 PM
Edited by - Mike Buess on 12/11/2007 1:10:02 PM |
 |
|
cellbiophysics
S. Korea
Posts |
Posted - 12/12/2007 : 8:34:30 PM
|
Thank you, Mike. I followed your suggestion and code. The fitting time was reduced by one-forth. Great! But, I didn't compare the result with the previous one. I believe they would be the same, although I don't understand exactly your code. If you find any free time, please let me know about that or email me. Thank you again!
Kang-Bin.
|
 |
|
cellbiophysics
S. Korea
Posts |
Posted - 12/20/2007 : 02:42:20 AM
|
Dear Mike,
I need your help for fitting the data, because the fitting function became more complex. I tried to modify your code, but I failed. My fitting function includes the summation and the double integral.
F(x)=exp(-P1*x)*sum(n=0 to k){F(n,P2,P3,x)+G(P4,x)*integral[integral[H(n,P1,...,P4,x1,s),s=0 to x1], x1=0 to x]} + P1*P2*exp(-P1*x)*sum(n=0 to k){integral[Q(n,P1...P4,s), s=0 to x]}.
I hope you have a good idea. If you need my data for the test, I can send you it. Please let me know.
Thanks,
Kang-Bin.
Edited by - cellbiophysics on 12/20/2007 02:44:13 AM
Edited by - cellbiophysics on 12/20/2007 02:45:09 AM |
 |
|
|
Topic  |
|
|
|