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

ovince

Yugoslavia
Posts

Posted - 08/01/2008 :  1:22:54 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi

I am trying to make 11 graphs in a loop and overplot their linear fit like:

%I="data";
for(i=1;i<=11;i++){
%A = "new$(i)";
create %A -w 10;

%(%A,1)=%(%I,1);
%(%A,2)=%(%I,i+1);

win -a %A;

wks.colSel(1);
wks.colSel(2);

//search "wks.colSel" for numbers like 200, 201 ...
worksheet -p 200 ORIGIN.OTP;

//fit linear
stat.data$ = %(%A,2);
stat.lr();
label -s -d 2500 700 -n myLabel1 "slope=$(stat.lr.b)"; //bold
label -s -d 2500 1000 -n myLabel2 "intercept=$(stat.lr.a)"; //bold
myLabel1.fsize = 36;
myLabel2.fsize = 36;
win -a "Graph$(i)"; //make active
run.section(lr.ogs,SimpleFit);
label -r Legend; //remove legend
}

Not sure why this does not work properly i.e. fit to the last graph is overplotted to all other graphs.


thanks
oliver

cpyang

USA
1406 Posts

Posted - 08/01/2008 :  11:10:31 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
the following code will work with active book with 1st sheet having data in XYYYY form, to fit col(1) as X and Y on all other columns.
This will work with Origin 8 SR2.


string mybk$=%H;
newsheet name:=FitLines c:=1;// add new sheet to hold fit lines
range aa = 1!;//1st sheet, assume we start with just one data sheet
range bb = FitLines!;// the newly added sheet called "FitLines"
bb.ncols = aa.ncols;//  Add new columns in FitLines sheet
// loop all cols and fit lr and put results into line
// the FitLR X-Function is used
dataset slopes, intercepts; // to hold fitting result parameters
slopes.SetSize(aa.ncols-1); // number of Ys in our data wks
intercepts.SetSize(slopes.GetSize());// not needed for now, but may be want this later
loop(ii,2,aa.ncols) {
	range dd = 1!(1,$(ii));
	fitlr i:=dd o:=FitLines!(1, wcol(ii));
	slopes[ii-1] = fitlr.b;//ii-1 because we loop from 2
	intercepts[ii-1] = fitlr.a;
}
// now we can make a plot with all the source data and fit and add a lable to show name and slope
loop(ii,2,aa.ncols) {
	range data = [mybk$]1!(1,wcol(ii));
	range fit = [mybk$]FitLines!(1,wcol(ii));
	win -t plot;
	plotxy data plot:=201 o:=<active>;//plot source data as scatter
	plotxy fit plot:=200 rescal:=0 color:=color(red) o:=<active>;
	label -r legend;// we dont really need a legend
label -s -q 2 -n myLabel1 "Linear Fit to %(1,@LM)
slope=$(slopes[ii-1])";
} 



I have added this example to

http://wiki.originlab.com/index.php?title=LabTalk:Curve_Fitting

and you can find more LabTalk examples there, using new Origin 8 syntax.

CP
Go to Top of Page

ovince

Yugoslavia
Posts

Posted - 08/02/2008 :  03:06:43 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
thanks for examples. Unfortunately I do not have version 8 of Origin :(
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