Hello I am using python to do a best fit the the data and im trying to fit it against a decay function and it is producing just a straight line.
here is my git folder with the data inside of it link. https://github.com/DeAngelo-neutrino/artie
just down load the pythonprogram folder there are pictures in there you can just ignore them.
the important part of the code is
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
import math
import sys
from scipy.interpolate import UnivariateSpline
np.set_printoptions(threshold=sys.maxsize)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
os.chdir("/Users/dwooley/Desktop/Python_programs/")
#we need to create varibles that are assoicated with each channel so we can plot, (This has been complete.)
#This creates an array/ column of data
time_argon = np.genfromtxt("log_all_chan_5_10-14_17_44.txt",skip_header=9, delimiter=",", usecols=(0))
target_top = np.genfromtxt("log_all_chan_5_10-14_17_44.txt",skip_header=9, delimiter=",", usecols=(1))
target_side = np.genfromtxt("log_all_chan_5_10-14_17_44.txt",skip_header=9, delimiter=",", usecols=(2))
target_bottom = np.genfromtxt("log_all_chan_5_10-14_17_44.txt",skip_header=9, delimiter=",", usecols=(3))
front_plate= np.genfromtxt("log_all_chan_5_10-14_17_44.txt",skip_header=9, delimiter=",", usecols=(4))
N2_level= np.genfromtxt("log_all_chan_5_10-14_17_44.txt",skip_header=9, delimiter=",", usecols=(9))
here i am just setting up my columns
next i create my dataframe in pandas
df = pd.read_csv("log_all_chan_5_10-14_17_44.txt", skiprows=9, sep=',' ) # we have to use , as delimitter and we use skiprows to ignore the first rows before the data
Then i create a window with 900 objects in it then take the mean of it
window_size = 900 #900 was intial value
time_column = df['Time (s)']
N2_level_dataseries = df['N2 Level']
N2_average = N2_level_dataseries.rolling(window = window_size).mean() #creates an average of 30 ( look up what is a rolling average do?)
#print(time_column)
#print("This is the average with a window of 30" , N2_average)
I then proceed to do some calculations that are irrelavent
but here they’re
lower = .98 #inches
upper = 4.812 #inches
h_ = upper
R_2 = upper
R_2_cm = 12.22248 #cm
L=67.7 #inches ^2
v_h_0 = 961.4 #inches ^3
R_1 = 4 #inches
R_1_cm = 10.16 #cm
h__lower = R_1
#what is v_R_1
twice_length = 2*L
h_zero = .98 #inches
upper_limitcalculation = ((1/2)* (np.sqrt(R_2*R_2 - h_*h_))*h_) + ((1/2)*R_2*R_2*np.arcsin(h_/R_2))
lower_limit_calculation = ((1/2)* (np.sqrt(R_2*R_2 - h__lower*h__lower))*h__lower) + (1/2)*R_2*R_2*np.arcsin(h__lower/R_2)
full_integration = upper_limitcalculation-lower_limit_calculation
only_R2 = 2*L *full_integration # this is correct we cross check with wolfram and excel
print("only R_2 in integral calculation",only_R2,"inches^3")
# we redefine varibles
h_ = R_1
h__lower =lower
upper_limitcalculation_1 = ((1/2)* (np.sqrt(R_2*R_2 - h_*h_))*h_) + (1/2)*R_2*R_2*np.arcsin(h_/R_2) #the reason we are getting an error is because of thr R_2^2 - h_ ^2 part this is giving us a negitive number
lower_limit_calculation_1= ((1/2)* (np.sqrt(R_2*R_2 - h__lower*h__lower))*h__lower) + (1/2)*R_2*R_2*np.arcsin(h__lower/R_2)
part_integration_1 = upper_limitcalculation_1-lower_limit_calculation_1
Part_1_volume = 2*L *part_integration_1
#print(Part_1_volume,"part_1 volume")
h_ = R_1
h__lower =lower
upper_limitcalculation_2 = ((1/2)* (np.sqrt(R_1*R_1 - h_*h_))*h_) + (1/2)*R_1*R_1*np.arcsin(h_/R_1) #the reason we are getting an error is because of thr R_2^2 - h_ ^2 part this is giving us a negitive number
lower_limit_calculation_2= ((1/2)* (np.sqrt(R_1*R_1 - h__lower*h__lower))*h__lower) + (1/2)*R_1*R_1*np.arcsin(h__lower/R_1)
part_integration_2 = upper_limitcalculation_2-lower_limit_calculation_2
Part_2_volume = 2*L *part_integration_2
#print(part_integration_2,"part 2 volme")
i then create my function
def all_togeterRs(h_n2):
# h = N2_level
#now we need to create a equation that takes h as the input
#the sqrt functinos need to be np and not math.
return (((1/2)* (np.sqrt(R_2**2 - h_n2**2))*h_n2) + ((1/2)*R_2**2 * np.arcsin(h_n2/R_2))) - ((((1/2)* (np.sqrt(R_2**2 - h_zero**2))*h_zero) + ((1/2)*R_2**2 * np.arcsin(h_zero/R_2))) ) - ( (((1/2)* (np.sqrt(R_1**2 - h_n2**2))*h_n2) + ((1/2)*R_1**2 * np.arcsin(h_n2/R_1))) - (((1/2)* (np.sqrt(R_1**2 - h_zero**2))*h_zero) + ((1/2)*R_1**2 * np.arcsin(h_zero/R_1))) )
#above calculation is in inches
I then add some columns to my existing dataframe
df['test'] = twice_length * all_togeterRs(N2_average)
#print("hi",new_way.head(50))
df['volume(Argon)'] = v_h_0+(df['test']) + twice_length*full_integration #this is in inches^3
volume_function_as_height = df['volume(Argon)']
I then parsed the data only selecting a the data set i want
lower_parsed_data = 8000 # this is the lower limit of where the data starts #previous value was 3000, for spline this only works for 7000 to 10000 with window size 900
upper_parsed_data = 10000 #this is the upper limit of where the data ends
data_we_use = volume_function_as_height.iloc[lower_parsed_data:upper_parsed_data]
I then create my expoential decay function
then declare my x and y data
After i have my intial guess for the intial state of the expoential decay
I proceed to do my curve fit after and plot it
def exponential_decay(x, A, B, C):
return A* np.exp(-B * x) + C
x_data = parsed_time_average
y_data = data_we_use
initial_guess = [max(y_data),1, min(y_data)]
# Perform the curve fitting
popt, pcov = curve_fit(exponential_decay, x_data, y_data, p0=initial_guess)
# Extract the optimal parameters
A_opt, B_opt, C_opt = popt
# Generate y values using the fitted parameters
y_fit = exponential_decay(x_data, *popt)
# Plot data and fit
plt.figure(figsize=(10, 6))
plt.plot(x_data, y_data, 'g-',label='Data')
plt.plot(x_data, y_fit, label='Exponential Decay Fit data', color='blue')
plt.xlabel('X')
plt.ylabel('Y')
#plt.plot(parsed_time_average,data_we_use, 'r-', label='Original')
plt.title('Exponential Decay Fit')
plt.legend()
plt.show()
And here lies the dilemma my best fit is just producing a striaght line, how can i fix this? Thank you
I cant upload the picture for some reason but i’ve uploaded it on imgur here is the link
https://imgur.com/a/BvKt2xj