Homework 19¶
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.integrate import quad
from scipy.interpolate import interp1d
from scipy.optimize import curve_fit
Problem 1¶
- heat capacity data is often given in two separate temperature ranges, like $300\le T (K)\le 1000$, and $1000\le T (K)\le 3000$.
- It is annoying that we have two temperature ranges for each species.
- For the given array of temperatures (K) below, the corresponding cp/Rg array is given for CH4.
Fit a single 4th order polynomial to the whole range of temperature data.
Report the average and maximum relative errors that occurs over the list of temperatures. That is, at each T, you have the old and new values of cp/Rg, so you can compute the relative error. Treat the old fit as "exact." Then report the maximum and average values.
- The relative error at a point the absolute value of the ratio of (the difference between the point and the fit at that point) to (the point).
Plot the two versions to visually compare the original data and your polynomial fit.
# THE FOLLOWING CODE PROVIDES ARRAYS T AND cpRg, which you can treat as the "given" data
T = np.linspace(300.,3000.,100)
a_lo = np.array([ 5.149876130,-1.367097880E-02,4.918005990E-05,-4.847430260E-08,1.666939560E-11])
a_hi = np.array([7.485149500E-02,1.339094670E-02,-5.732858090E-06,1.222925350E-09,-1.018152300E-13])
cpRg = np.zeros(len(T))
for k in range(5): cpRg[np.logical_and(T>=300,T<1000)] += a_lo[k]*T[np.logical_and(T>=300,T<1000)]**k
for k in range(5): cpRg[np.logical_and(T>=1000,T<=3000)] += a_hi[k]*T[np.logical_and(T>=1000,T<=3000)]**k
#------------------
Problem 2¶
The following kinetic data for r versus T were collected. We want to fit this to the following model function: $r = kT^me^{-E_a/T}$. Here, $k$, $m$, and $E_a$, are adjustable parameters that we want to find so that our model best fits the data.
Find the best parameters and plot the data and the model function together on the same graph.
T = np.linspace(500.,1000.,8)
r = [105.598, 89.700, 70.768, 66.996, 60.711, 58.992, 55.8328, 53.420]