1# coding: utf-8
2# Script to test linear and polynomial fits
3
4import random
5# set seed to ensure replicability
6random.seed(1)
7
8
9dp=1.5 #noise's stddev
10
11#generate a table t1 with linear data points with some noise and random y-errors
12t1Name="Linear-data"
13t1=newTable(t1Name,3,100)
14for i in range(1,t1.numRows()+1):
15 t1.setCell(1,i,i+random.uniform(0,dp))
16 t1.setCell(2,i,2+3*i+random.uniform(0,dp))
17 t1.setCell(3,i,random.uniform(0,dp))
18
19g1=plot(t1,'2',1) #plotting the linear data
20
21l1=g1.activeLayer()
22curve1=t1Name+"_2"
23l1.addErrorBars(curve1,t1,'3')
24
25g1.confirmClose(False)
26t1.confirmClose(False)
27
28f1=LinearFit(l1,curve1) #linear fit
29f1.fit()
30
31f2=PolynomialFit(l1,curve1,5) #polynomial fit of degree 5
32f2.setColor("blue")
33f2.fit()
34
35print("#","linear coef. =",f1.results()[0]," ± ",f1.errors()[0],"\n# angular coef. =",f1.results()[1]," ± ",f1.errors()[1])
36g1.exportImage("linearAndPolyFits.png")
37app.exit()
38