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 for Programming
 LabTalk Forum
 nlfit into a loop becomes exponentially slow
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

U Acevedo

France
2 Posts

Posted - 10/06/2020 :  08:21:38 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 2020
Operating System:Win

Hello,

My script becomes exponentially slower when increasing the number of columns to fit with nlfit X-function.

I need to fit several columns (more than 3000) with the same fitting function but with different starting parameters depending on a certain condition.

I used the nlfit X-function inside a loop which sets different starting parameters depending on the condition variable: Wtype. My script takes about 10 min fitting the first 500 cols, then for the next 500 it takes more than 30 min and so on. I think that the problem comes from the memory management after each nlfit loop but i am a newbie and I dont know how to optimize it...

Here is my code:

//Declaration of start/end columns to fit
win -a Book1;
int start = 2;
int end =wks.nCols;
range Fitted_X_ROIii=col(Fitted_X_ROIii);
range Fitted_Y_ROIii=col(Fitted_Y_ROIii);

// Create a sheet to collect the fitted curves
newsheet name:="FitCurves" labels:="Fitted X" active:=0;
range xfit = FitCurves!wcol(1);
xfit = {0:0.36036036036036:360};
int i;
loop(i, start, end) {
range cc = FitCurves!wcol(i);
cc[L]$ = "Fitted Y";
cc[C]$ = "ROI$(i-1)";
}

// Create a result sheet to collect the fit result
newsheet name:="Results" labels:="Plot|delta|error_delta|d15|error_d15|c_param|Rsq|Wtype|Alpha_max" active:=0;
range rPlot = Results!col(Plot);
range rd = Results!col(delta);
range rerror_d = Results!col(error_delta);
range rd15 = Results!col(d15);
range rerror_d15 = Results!col(error_d15);
range rc = Results!col(c_param);
range rRsq = Results!col(Rsq);
range rWtype = Results!col(Wtype);
range alphamax = Results!col(Alpha_max);

// Do the fit
loop(ii, start, end) {
int rr = ii-1;
range rngInput = (1, $(ii));
range yfit = FitCurves!wcol(ii);
if (rWtype[rr]==1){
nlbegin iy:=rngInput func:=GralDeltaFunctionbNeel1bBloch4mmAscan nltree:=tt;
tt.d15 = 0;
tt.d = 0;
tt.lb_d = -5;
tt.ub_d = 5;
tt.c = 0;
nlfit;
// Generate the fit curve
Fitted_X_ROIii = {0:0.36036036036036:360};
Fitted_Y_ROIii = fit(col(Fitted_X_ROIii));
yfit = col(Fitted_Y_ROIii);
nlend;
}
if (rWtype[rr]==2){
nlbegin iy:=rngInput func:=GralDeltaFunctionbNeel1bBloch4mmAscan nltree:=tt;
tt.d15 = 0;
tt.lb_d = 85;
tt.ub_d = 95;
tt.d = 90;
tt.c = 0;
nlfit;
// Generate the fit curve
Fitted_X_ROIii = {0:0.36036036036036:360};
Fitted_Y_ROIii = fit(col(Fitted_X_ROIii));
yfit = col(Fitted_Y_ROIii);
nlend;
}
if (rWtype[rr]==3){
nlbegin iy:=rngInput func:=GralDeltaFunctionbNeel1bBloch4mmAscan nltree:=tt;
tt.d15 = -0.41;
tt.d = 40.5;
tt.lb_d = 20;
tt.ub_d = 70;
tt.c = 0;
nlfit;
// Generate the fit curve
Fitted_X_ROIii = {0:0.36036036036036:360};
Fitted_Y_ROIii = fit(col(Fitted_X_ROIii));
yfit = col(Fitted_Y_ROIii);
nlend;
}
if (rWtype[rr]==4){
nlbegin iy:=rngInput func:=GralDeltaFunctionbNeel1bBloch4mmAscan nltree:=tt;
tt.d15 = -0.11;
tt.d = -48;
tt.lb_d = -70;
tt.ub_d = -20;
tt.c = 0;
nlfit;
// Generate the fit curve
Fitted_X_ROIii = {0:0.36036036036036:360};
Fitted_Y_ROIii = fit(col(Fitted_X_ROIii));
yfit = col(Fitted_Y_ROIii);
nlend;
}
// Put result to Results worksheet
if (rWtype[rr]==0){
rPlot[rr]$ = "--";
rd[rr] = "--";
rerror_d[rr] = "--";
rd15[rr] = "--";
rerror_d15[rr] = "--";
rc[rr] = "--";
rRsq[rr] = "--";
}
else {
rPlot[rr]$ = rngInput[L]$;
rd[rr] = tt.d;
rerror_d[rr] = tt.e_d;
rd15[rr] = tt.d15;
rerror_d15[rr] = tt.e_d15;
rc[rr] = tt.c;
rRsq[rr] = tt.r;
}
}

YimingChen

1665 Posts

Posted - 10/06/2020 :  3:11:58 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

It is normal the processing speed gets slow when more columns get involved. Can you split your worksheet and process 500 columns in one project each time?

James

Edited by - YimingChen on 10/06/2020 3:12:17 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