I have the following code for my theoretical analysis using experimental data. I am trying to fit a curve and extrapolate S_u0 values to determine the initial temperatures.
# Determination of laminar burning velocities from experimental data using constant volume pressure rise method.
import numpy as np
import openpyxl as xl
import matplotlib.pylab as plt
from lmfit import Model
plt.rc('font',family='Book Antiqua',size=12)
p_i=float(2.0) # Initial pressure
c=float(2.0265) # Constant
V_b=float(1.94e-3) # Volume of combustion bomb
k_u=float(1.3) # Specific heat ratio of unburnt mass
k_b=float(1.27) # Specific heat ratio of burnt mass
# Extracting experimental data from excel.
wb = xl.load_workbook("G:/Laminar Paper/Data/T0444CH1.xlsx")
ws = wb.active
# Calculation of initial voltage from experimental values.
num_cells=list(ws['B2':'B1500'])
V_initial=[]
for row in num_cells:
for cell in row:
V_initial.append(cell.value)
V_av=np.absolute(sum(V_initial)/len(V_initial)) # Average voltage
# Determining the final voltage from the experimental values.
num_cells=list(ws['B3':'B13252'])
V_final=[] # Experimental testing voltages
for row in num_cells:
for cell in row:
V_final.append(cell.value)
p=(V_final/np.floor(c))+(p_i-(V_av/np.floor(c))) # Combustion pressure
# The time elapse.
num_cells=list(ws['A3':'A13252'])
time=[]
for row in num_cells:
for cell in row:
time.append(cell.value)
# Curve fitting.
Func_1=np.polyfit(time, p, 15)
Func_2=np.poly1d(Func_1)
time_new=np.linspace(time[0], time[-1], 50) # New time values
p_new=Func_2(time_new) # New pressure values/filtered pressure
dp_dt=np.gradient(p_new,time_new) # Derivative of pressure wrt time
# Linear approximation method.
x_linear=(p_new-p_i)/(max(p_new)-p_i) # Burnt mass fraction
R_b=(pow(((V_b*3)/(4*np.pi)), 1.0/3))
S_u1=((1-(1-x_linear)*(p_i/p_new)**1/k_u)**-2/3)
S_u2=S_u1*((p_i/p_new)*1/k_u)
S_u3=S_u2*(1/(max(p_new)-p_i))*dp_dt
S_u=(R_b/3)*S_u3 # Linear approximation laminar flame speed
# Two-zone approach.
func_p=(k_b-1)/(k_u-1)+(((k_u-k_b)/(k_u-1))*(p_new/p_i)**((k_u-1)/k_u))
x_2zone=(p_new-p_i*func_p)/(max(p)-p_i*func_p) # Burnt mass fraction
S_uz1=((1-(1-x_2zone)*(p_i/p_new)**1/k_u)**-2/3)
S_uz2=S_uz1*((p_i/p_new)*1/k_u)
S_uz3=S_uz2*(1/(max(p_new)-p_i))*dp_dt
S_uz=(R_b/3)*S_uz3 # Two-zone laminar flame speed
# Model function.
def mod_m(n,su0=1,alpha=1):
return su0*(n)**alpha
# S_u and pressure ratio array data for fitting and calculating S_u0 and T_0.
n=np.array([1.78759326,1.91173221,2.05673713,2.22911249,2.4360555,2.68433232,2.97885016])
m=np.array([0.20791161,0.20332239,0.20206572,0.20260075,0.20292399,0.20115307,0.19601662])
# Fitting model.
model=Model(mod_m)
# Making a set of parameters (for 'su0' and 'alpha'):
params=model.make_params(s_u0=10)
# Setting min/max bounds on parameters:
params['alpha'].min=0.0
params['su0'].min=0.0
params['su0'].max=1e2
# Run the fit with Model.fit(Data_Array, Parameters, independent vars).
result=model.fit(m,params,n=n)
# Plotting.
plt.plot(n,result.best_fit,'b*-', label='Fit')
plt.plot(p_new/p_i,S_u,'k--',linewidth='2',label='Linear')
plt.plot(p_new/p_i,S_uz,'r-',label='Two-zone')
plt.xlabel('Pressure ratio $p/p_i$')
plt.ylabel('Laminar flame speed $(S_u)$ [m/s]')
plt.legend(loc='lower right')
plt.savefig('Laminar.png')
plt.show()
# Printing report with results and fitting statistics.
print(result.fit_report())
After plotting I obtained figure 1 but I want to get a plot like figure 2. Please how can I get a figure like the figure 2. Thanks in advance.
Figure 2
If I understand the question, you're looking to plot the predicted values of the model outside the range used for the fit.
With your limited range for the n
and m
arrays (at least in comparison to your other data)
result = model.fit(m, params, n=n)
plt.plot(n, result.best_fit, 'b*-', label='Fit')
will plot only over the limited range that you used for the fit.
You can evaluate the model with a set of Parameters (you probably want the best-fit parameters) and any values of the independent variable n
with result.eval()
:
new_m = result.eval(result.params, n=new_n)
It seems that what you want might be something like
new_n = np.linspace(0, 5, 51)[1:] # to avoid 0, where your model fails
plt.plot(new_n, result.eval(result.params, n=new_n), label='Predicted')