Hi there,
I have a set of coordinates (data points) that I want to use Python3 to fit an exponential decay curve to. I've used this resource here as a base for building my program.
The problem is, no matter what the x-value I put in is, the y-value ALWAYS comes up as 1.0!
My code is below.
# curve fitting algorithm
# least squares fit method
import math
n = 800
p0 = (5*(10**6))
decay = (1.16*(10**-3))
def calculate_a_fit():
numerator = 0
numeratorPos = 0
numeratorNeg = 0
denominator = 0
denominatorPos = 0
denominatorNeg = 0
# calculate top half of sum/algorithm
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += ((i**2)*y)
numeratorPos += sigma
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += (y*(math.log(y)))
numeratorPos *= sigma
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += (i*y)
numeratorNeg += sigma
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += (i*y*(math.log(y)))
numeratorNeg *= sigma
numerator = numeratorPos - numeratorNeg
# calculate bottom half of sum/algorithm
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += (y)
denominatorPos += sigma
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += ((i**2)*y)
denominatorPos *= sigma
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += ((i*y)**2)
denominatorNeg += sigma
denominator = denominatorPos - denominatorNeg
a = (numerator/denominator)
print(a)
return a
calculate_a_fit()
def calculate_b_fit():
numerator = 0
numeratorPos = 0
numeratorNeg = 0
denominator = 0
denominatorPos = 0
denominatorNeg = 0
# calculate top half of sum/algorithm
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += (y)
numeratorPos += sigma
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += (i*y*(math.log(y)))
numeratorPos *= sigma
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += (i*y)
numeratorNeg += sigma
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += (y*(math.log(y)))
numeratorNeg *= sigma
numerator = numeratorPos - numeratorNeg
# calculate bottom half of sum/algorithm
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += (y)
denominatorPos += sigma
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += ((i**2)*y)
denominatorPos *= sigma
sigma = 0
for i in range(0, n):
y = (p0 * math.e)**(-(decay * n))
sigma += ((i*y)**2)
denominatorNeg += sigma
denominator = denominatorPos - denominatorNeg
b = (numerator/denominator)
print(b)
return b
a = calculate_a_fit()
b = calculate_b_fit()
# maybe y = (a ** (b * 800))?
for i in range(0, n):
y = (math.exp(a) * math.e) ** (b * n)
print (y)
I used a for loop for each sigma.
What am I doing wrong?
Thanks in advance :)