#!/usr/bin/env python # coding: utf-8 # # Homework 19 # In[2]: import numpy as np import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') from scipy.integrate import quad from scipy.interpolate import interp1d from scipy.optimize import curve_fit # ### Problem 1 # # * In a previous assignment we used thermo data for heat capacities of species. # * 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. This is computed using the equations and the coefficients for the two temperature ranges given in previous assignments. # # 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. # # Plot the two versions to visually compare the original data and your polynomial fit. # In[5]: # 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. # # In[6]: T = np.linspace(500.,1000.,8) r = [105.598, 89.700, 70.768, 66.996, 60.711, 58.992, 55.8328, 53.420] # In[ ]: