T O P I C R E V I E W |
aeolian1 |
Posted - 01/22/2014 : 10:07:53 AM Origin Ver. and Service Release (Select Help-->About Origin): 9.0.0 Operating System: Windows 7
I have placed a nlsf routine (I'm not using x-functions, since pasting the parameters to the plot is far more convenient for my application if I use the nlsf commands) inside a double loop. I give nlsf.p4 and nlsf.p5 initial values that depend on the index variable of the inner loop. Unfortunately, the fit results are incorrect (I verified this for one value of the loop variable p, namely p = 6), and I can only achieve correct results by inserting values for nlsf.p4 and nlsf.p5 manually. I would greatly appreciate any help! Thank you.
The outline of the code is
... ts=0.02;te=0.10;nums=9;
for(int p=1;p<=nums;p++) // this is the inner fit loop { nlsf.begin(); nlsf.init(); nlsf.cleanUpFitData(); nlsf.fitWksName$=data1; nlsf.fitData$=%(data1,6); nlsf.funcx$=%(data1,5); nlsf.xmode=0; nlsf.funcCol$=%(data1,9); nlsf.dataBegin=t5; nlsf.dataEnd=t6; nlsf.func$="ExpDecay1md"; // a user-defined function nlsf.constraints=1;nlsf.constr$="A1<=0;t1>0;t2>0;t1>=t2"; nlsf.p1=limit.ymax; nlsf.p2=limit.ymin-limit.ymax; nlsf.p3=%(data1,5,limit.imax); double z1=ts+(p-1)*(te-ts)/(nums-1);double z2=6*z1/7; // For p = 6, z1 = 0.07 and z2 = 0.06. nlsf.p4=z1;nlsf.p5=z2; NLSF.V1=0;NLSF.V2=0;NLSF.V3=0; nlsf.iterate(5); // ***** // The fit result for p = 6 after these 5 iterations is // incorrect: it differs from the result that I get if I do the // fit for p = 6 manually (i.e. not in a script). (I didn't check // other values of p; I assume that those results are also wrong.) // But if I use "nlsf.p4=0.07;nlsf.p5=0.06;" instead, the fit results // for p = 6 are correct! Unfortunately, since these starting // parameters need to be p-dependent, I need to find another solution. // // I also tried using // dataset s2={ts:0.01:te}; // dataset s3=s2; // for(int p2=1;p2<=nums;p2++) {s3[p2]=6*s2[p2]/7;} // prior to the loop, // followed by "nlsf.p4=s2[p];nlsf.p5=s3[p];" in the loop, // but the results for p = 6 are still incorrect. // ***** NLSF.V1=1;NLSF.V2=1;NLSF.V3=1; nlsf.iterate(200); if ((nlsf.e4/nlsf.p4)>0.5||(nlsf.p4-nlsf.p5)/nlsf.p4<=1E-5) {temp1=nlsf.p1;temp2=nlsf.p2;temp3=nlsf.p3;temp4=nlsf.p4; // Under certain circumstances, I change the fit function and refit. // The part that follows may not be relevant to my above question. nlsf.begin(); nlsf.init(); nlsf.cleanUpFitData(); nlsf.fitWksName$=data1; nlsf.fitData$=%(data1,6); nlsf.funcx$=%(data1,5); nlsf.xmode=0; nlsf.funcCol$=%(data1,9); nlsf.dataBegin=t5; nlsf.dataEnd=t6; nlsf.func$="ExpDecay1crit";nlsf.constraints=1;nlsf.constr$="A<=0;tau>0"; nlsf.p1=temp1;nlsf.p2=temp2;nlsf.p3=temp3; nlsf.p4=temp4; NLSF.V1=0;NLSF.V2=0;NLSF.V3=0; nlsf.iterate(5); NLSF.V1=1;NLSF.V2=1;NLSF.V3=1; nlsf.iterate(200);}; nlsf.makecurve(func);
... more stuff, omitted for clarity ...
nlsf.pasteparams(P); label -r Fit.P; doc -u; %M=nlsf.func$; %Z=""; loop(ii,1,nlsf.npara) { %N=nlsf.n$(II)$; %P=$(nlsf.p$(II),*5); %Q=$(nlsf.e$(II),*5); %Z=%Z\r\n%N %P \(177)%Q; }; label -s -sa -p 100 55 -n FITP2 "Data: %C Second half (LHS) Model: %M Chi^2 = $(nlsf.chiSqr,*5) R^2 = $(nlsf.cod,*5) %Z"; FITP2.fsize=12; nlsf.unInit(); nlsf.end(); win -a data1; work -s 9 t5 9 t6; menu -e 57634; work -s 8 t5 8 t6; menu -e 57637; ... } //end of inner fit loop |
8 L A T E S T R E P L I E S (Newest First) |
lkb0221 |
Posted - 01/24/2014 : 10:48:03 AM Hi,
You can send them to <tech@originlab.com> if they are not too big. Or click the "Send File to Tech support" link at the upper-right corner of this page to see how to send file through our FTP.
Zheng OriginLab |
aeolian1 |
Posted - 01/24/2014 : 10:43:31 AM Dear Zheng,
Thank you - silly mistake of mine with not highlighting "p=1".
It would be great if you could look at the data/script/function files - where shall I send them? |
lkb0221 |
Posted - 01/24/2014 : 10:33:57 AM Hi, Henrietta
For #3, when you type "p=1;<Enter>", actually this line has not been executed yet, and that's why "p=<Enter>" immediately will return what p is before. You should highlight the line and press Enter or press Enter without semicolon to execute.
"p == 1" is an invalid assignment by the way, will throw error message if you run this.
For #1, it's hard to tell what goes wrong. Looks like all value calculation and passing works just fine. Maybe I will need your whole file (Data, Formula and Script) to run some tests.
Zheng OriginLab |
aeolian1 |
Posted - 01/24/2014 : 08:46:39 AM P.S. to Zheng: I solved problem #4 by placing
nlsf.begin(); nlsf.init(); nlsf.cleanUpFitData(); nlsf.fitWksName$=data1; nlsf.fitData$=%(data1,6); nlsf.funcx$=%(data1,5); nlsf.xmode=0; nlsf.funcCol$=%(data1,9); nlsf.dataBegin=t5; nlsf.dataEnd=t6;
prior to (and outside of) the p loop, and by placing
nlsf.unInit(); nlsf.end();
after (and outside of) the p loop. I also deleted the second appearance of
nlsf.begin(); nlsf.init(); nlsf.cleanUpFitData(); nlsf.fitWksName$=data1; nlsf.fitData$=%(data1,6); nlsf.funcx$=%(data1,5); nlsf.xmode=0; nlsf.funcCol$=%(data1,9); nlsf.dataBegin=t5; nlsf.dataEnd=t6;
within the p loop. With these changes, I don't get an extra plot per iteration of the p loop.
I'll be very grateful for your help with problems #1 and #3! |
aeolian1 |
Posted - 01/24/2014 : 07:01:06 AM Dear Zheng,
Thank you very much for answering the second question and continuing to work on the first one! I really appreciate it - I would never have figured out the answer to #2 on my own.
Perhaps I could throw in a third question, if you don't mind?
I am trying to debug the inner loop (it is producing one new graph per loop iteration, which I don't want - if it's obvious to you why, I'd be grateful for help there too). In doing the debug, I would like to type (for example) "p=1" in the Script Window. I would then highlight just the inner portion of the loop and press "Enter" to run the loop once (to see where the extra graph is coming from). The problem is that if I type "p=1;<ENTER>" or even "p==1;<ENTER>" in the Script Window and then do "p="<ENTER>, I get "p= --" as the result. This is only the case for the loop variable: if I try "a=1;"<ENTER>, then "a="<ENTER> yields "a=1". How can I force the loop variable p to accept an integer value for purposes of debugging? Thank you very much again! |
lkb0221 |
Posted - 01/23/2014 : 1:02:47 PM Hi, Henrietta
For the second problem, that is because in %(workbookName, column, row) notation, column can be both string or numeric (in your case, because there is no column named p3 so it is treated as numeric instead), but row can only be a number. You should use %(InitParams,1,$(p3)) instead of just %(InitParams,1,p3).
I'm still trying to address the first problem...
Zheng OriginLab |
aeolian1 |
Posted - 01/23/2014 : 09:38:47 AM I don't understand why the following works, but at least it does:
Code added prior to the loop: newbook result:=BkName3$; window -r %H InitParams; win -a InitParams; ts=0.02;te=0.10;int nums=9;double nums1=nums; worksheet -a (nums-2); for(int p3=1;p3<=nums;p3++) {double p3p=p3; %(InitParams,p3,1)=ts+(p3p-1)*(te-ts)/(nums1-1); %(InitParams,p3,2)=6.0*%(InitParams,p3,1)/7.0; }
Code changed within loop: nlsf.p4=%(InitParams,p,1);nlsf.p5=%(InitParams,p,2);
This produces correct fit results!
For reasons I don't understand, the slightly different code
newbook result:=BkName3$; window -r %H InitParams; win -a InitParams; ts=0.02;te=0.10;int nums=9;double nums1=nums; for(int p3=1;p3<=nums;p3++) {double p3p=p3; %(InitParams,1,p3)=ts+(p3p-1)*(te-ts)/(nums1-1); %(InitParams,2,p3)=6.0*%(InitParams,1,p3)/7.0; }
does *not* produce any values in the worksheet InitParams. I wish I understood why...?!?
Also, using (within the p loop):
double p1=p;double nums1=nums; double z1=ts+(p1-1)*(te-ts)/(nums1-1);double z2=6.0*z1/7.0; nlsf.p4=z1;nlsf.p5=z2;
does *not* work, which presumably means that the success of the code involving the new worksheet InitParams is *not* due to having redefined the integers in the calculation of the values of nlsf.p4 and nlsf.p5 as doubles.
I would be very grateful for any insights as to what causes the above two problems! (The second being why I can't use e.g. (InitParams,1,p3) instead of (InitParams,p3,1).) Thank you very much. |
aeolian1 |
Posted - 01/22/2014 : 11:23:20 AM P.S. The problem persists if I use "6.0" and "7.0" instead of "6" and "7" in the definition of z2 (or, alternatively, of s3[p]). |
|
|