#!/usr/bin/env python # coding: utf-8 # In[20]: import numpy as np a = np.array([1, 11, 23, 13, 200, 10, 11]) # find max of a # alogorithm mymax = a[0], # loop through array and if a[i] > a[0], then mymax = a[i] mymax = a[0] N = len(a) for i in range(N): # print(i) if a[i] > mymax: mymax = a[i] print(mymax) np.max(a) # In[9]: import numpy as np #for arrays import matplotlib.pyplot as plt #for plotting from scipy.integrate import odeint get_ipython().run_line_magic('matplotlib', 'inline') plt.style.use('seaborn') # ODE # Batch reactor # -ra = k CA^2 # dCA/dt = -(-ra) = -k CA^2 # step 0 - modules # step 1 : define a function that needs to be solved # dy/dt = f(y,t) def f(y,t): return -k*y**2 # step 2: define arrays # time_init = 0 time_final = 10 N_points = 50 time_array = np.linspace(time_init,time_final, N_points) k = 0.001 C_A0 = 10. #step 3: solving the stuff # odeint(function, initial value, time array) C_A_calc = odeint(f, C_A0, time_array) #step 4: plotting # plt.plot(x, y, 'go-') plt.plot(time_array, C_A_calc, 'ro--') plt.xlabel('t, s') plt.ylabel('$C_{A}$, moles') plt.show() # In[10]: # REGRESSION import numpy as np time_array = np.array([ 0., 125., 250., 375., 500., 625., 750., 875., 1000., 1125., 1250., 1375., 1500., 1625., 1750., 1875., 2000., 2125., 2250., 2375., 2500., 2625., 2750., 2875.]) C_A_array = np.array([10.28148623, 8.92676003, 7.47370739, 7.20498621, 6.01448975, 4.85174028, 4.68868558, 4.01410753, 3.94055544, 3.04186757, 2.40544184, 2.62290428, 2.18704979, 1.31502537, 1.03730156, 2.28837979, 0.45062549, 1.43886825, 1.26808295, 1.24125421, 0.28353151, 0.90117414, 0.74132173, 0.73710597]) # In[11]: #step 1 : plot the data: plt.plot(time_array, C_A_array, 'o') # In[13]: # step 2: define the function (that we think will fit the data) # and define parameters def f(x, k, C_A0): return C_A0*np.exp(-k*x) k_test_1 = 0.01 C_A0 = 10. #step 3: plotting stuff plt.plot(time_array, C_A_array, 'o') plt.plot(time_array, f(time_array, k_test_1, C_A0), 'r-') # In[14]: # step 2: define the function (that we think will fit the data) # and define parameters def f(x, k, C_A0): return C_A0*np.exp(-k*x) k_test_1 = 0.001 C_A0 = 10. #step 3: plotting stuff plt.plot(time_array, C_A_array, 'o') plt.plot(time_array, f(time_array, k_test_1, C_A0), 'r-') # In[19]: import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit time_array = np.array([ 0., 125., 250., 375., 500., 625., 750., 875., 1000., 1125., 1250., 1375., 1500., 1625., 1750., 1875., 2000., 2125., 2250., 2375., 2500., 2625., 2750., 2875.]) C_A_array = np.array([10.28148623, 8.92676003, 7.47370739, 7.20498621, 6.01448975, 4.85174028, 4.68868558, 4.01410753, 3.94055544, 3.04186757, 2.40544184, 2.62290428, 2.18704979, 1.31502537, 1.03730156, 2.28837979, 0.45062549, 1.43886825, 1.26808295, 1.24125421, 0.28353151, 0.90117414, 0.74132173, 0.73710597]) #LEts start fitting the data # Step 1: function def f(x, k, C_A0): return C_A0*np.exp(-k*x) #Step 2: init parameters: k_guess = 0.005 C_A0_guess = 11. #step 3: fitting the data: # curve_fit(function, time_array, C_A_array initial parameters) popt, pcov = curve_fit(f, time_array,C_A_array, p0=(k_guess, C_A0_guess)) print(popt) print("k = {0}".format(popt[0])) # print(popt[0]) print("C_A0 = {0}".format(popt[1])) # print(popt[1]) C_A_final = f(time_array, popt[0], popt[1]) # In[ ]: