# Script to test non-linear and Gaussian fits
import random
from math import *

random.seed(1)
def exponential_curve(x):
    return exp(-0.1*x)

dp=0.003 #noise's stddev
    
#generate a table t1 with Gaussian data points with some noise and random y-errors
t1Name="Gaussian-curve"
t1=newTable(t1Name,3,100)
for i in range(1,t1.numRows()+1):
    xx=float(i)
    t1.setCell(1,i,xx)
    t1.setCell(2,i,exponential_curve(xx)+random.uniform(0,dp))
    t1.setCell(3,i,random.uniform(0,dp))

g1=plot(t1,'2',1) #plotting the curve
        
l1=g1.activeLayer()
curve1=t1Name+"_2"
l1.addErrorBars(curve1,t1,'3')
    
g1.confirmClose(False)
t1.confirmClose(False)
    
f1=ExponentialFit(l1,curve1)
f1.setInitialValues(.8,.5,1.1)
f1.fit()
assert f1.rSquare() > 0.99,"f1.rSquare() >= 0.99"
assert f1.formula()=="y0+A*exp(-x/t)"

app.exit()
