# Data Fitting Exercises

# 1. Linear Regression

A common part of data analysis is **fitting** a mathematical model to some experimental data. This might be to determine how well a particular model describes your experimental data, or to extract a numerical parameter; for example, fitting rate laws to reactant concentrations over time to obtain rate constants. Another example is the semester 1 exercise, where you fitted CO$_2$ vapour pressures to the Clausius-Clapeyron equation to obtain phase transition enthalpies.

A third example where fitting to simple exerimental data can give chemical information about a particular reaction involves the **Van't Hoff equation**. This relates the change in the equilibrium constant, $K$, for a reaction, to a change in temperature, providing the enthalpy change for the reaction, $\Delta H$, is constant:

\begin{equation}
\frac{\mathrm{d}\,\ln{K}}{\mathrm{d}\,T} = \frac{\Delta H}{RT^2}.\tag{1}
\end{equation}

The derivation of this equation comes from combining two standard equations for the Gibbs free energy change, $\Delta G$, for a reaction:

\begin{equation}
\Delta G = \Delta H - T \Delta S\tag{2}
\end{equation}

\begin{equation}
\Delta G = -RT \ln{K}\tag{3}
\end{equation}

Combining these two equations, we get

\begin{equation}
\ln{K} = -\frac{\Delta H}{RT} + \frac{\Delta S}{R}.\tag{4}
\end{equation}

This is the **linear** form of the Van't Hoff equation. Differentiating with respect to $\frac{1}{T}$ (keeping $\Delta H$ and $\Delta S$ constant) gives equation 1.

Because the linear form of the Van't Hoff equation gives the equation for a straight line (hence &ldquo;linear&rdquo;) a plot of $\ln{K}$ against $\frac{1}{T}$ should give a straight line, as long as the enthalpy and entropy of reaction are approximately constant over the temperature range of interest. Furthermore, a straight line fitted to these data will have a slope of $-\frac{\Delta H}{R}$ and an intercept of $\frac{\Delta S}{R}$. By performing a linear fit, it is therefore possible to extract values for $\Delta H$ and $\Delta S$.

In the semeseter 1 lab, you learned how to use the `linregress()` function (from `scipy.stats`) to perform a linear regression (fit a straight line to a set of data). This exercises will work through model fitting in a bit more detail, divided into three parts. 
1. In the first part, you will have the opportunity to revise and practice working with data, and using `scipy.stats.linregress` to fit a straight line, to obtain the experimental reaction enthalpy and entropy for an equilibrium. 
2. The second part will go into more detail about what we mean when we talk about &ldquo;fitting&rqduo; a straight line, and will show you a different, more general, approach to solving the same problem, that involves writing your own functions. 
3. In the third part, you will apply what you have learned to fit a **non-linear** model to some experimental data, to analyse a flash-photolysis experiment.

<div class="alert alert-success">
Review the basics of using <span style='font-family:monospace'>matplotlib</span> to plot graphs, and using <span style='font-family:monospace'>scipy.stats.linregress</span> for linear regression, using your notebooks from last semester.
</div>

## Exercise

The file `data/equilibrium_constant.dat` contains a set of equilibrium constants measured at different temperatures for the reaction

\begin{equation}
2\mathrm{NO}_2 \rightleftharpoons \mathrm{N}_2\mathrm{O}_4.
\end{equation}

By performing a linear regression with these data, calculate the enthalpy and entropy changes for this reaction.

### Instructions

The first thing you will need to do is set up your notebook for working with data, by importing `numpy` and the plotting functions from `matplotlib`.
>```python
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
```


```python

```

### Process

The data from this experiment are stored in a text file in `data/equilbirium_constant.dat`, which looks like

```
# equilibrium constant data for 2 NO2 => N2O4  
# columns are: temperature (degrees Celsius), K
  
9   34.3
20  12
25  8.79
33  4.4
40  2.8
52  1.4
60  0.751
70  0.4
```

To be able to work with the temperature and equilibrium constant data, you want to load them into `numpy` arrays. You can read the data from the file using the `np.loadtxt()` function, e.g.

>```python
    data = np.loadtxt('data/equilibrium_constant.dat')
```

<div class="alert alert-success">
Load the dataset from the file into a numpy array called <span style='font-family:monospace'>data</span>.  
Check that this is a 2D array containing both the temperatures and equilibrium constants.
</div>


```python

```

<div class="alert alert-success">
To make it easier to work with the temperature and equilibrium constant data separately, copy the columns from your <span style='font-family:monospace'>data</span> array into a pair of 1D arrays, named <span style='font-family:monospace'>temperature</span> and <span style='font-family:monospace'>k_eq</span>.  
Remember that you can refer to a particular column or row in a 2D array using **array slicing**.  
Convert your temperature array from Celsius into Kelvin.
</div>


```python

```


```python

```

<div class="alert alert-success">
Plot $\ln{K}$ against $1/T$, and check that this gives a straight line relationship.  
Label your axes.
</div>


```python

```

<div class="alert alert-success">
Using <span style='font-family:monospace'>scipy.stats.linregress</span> fit a straight line to this dataset, and print the slope and intercept from your fit.  
</div>


```python

```

<div class="alert alert-success">
Generate another $\ln{K}$ versus $1/T$ plot, showing the experimental data as points, and your &ldquo;line of best fit&rdquo; as a line.
</div>

A simple way to generate points that lie along a straight line is to define a `line` function:
>```python
def line( m, c, x ):
    y = m * x + c
    return y
```

This function takes three arguments; $m$, $c$, and $x$; and returns the corresponding $y$ values. By passing in `slope` as $m$, `intercept` as $c$, and `1/temperature` as $x$, you can calculate the $y$ coordinates to plot on your figure.


```python
# Define your function here
```


```python
# Plot your figure.
# You will need to use `plt.plot()` twice to plot the original data, and your fitting line.
```

<div class="alert alert-success">
Use your fitted values for the slope and intercept to calculate the enthalpy and entropy of this reaction.  
Remember that you can import the gas constant, $R$, using  <br/><br/>
 <span style='font-family:monospace'>from scipy.constants import R</span>
 
Add more code cells as necessary to organise your calculation.
</div>


```python

```

<div class="alert alert-success">
Create a markdown cell below, and use it to comment on the signs of $\Delta H$ and $\Delta S$ for this reaction.
</div>

8e3a87e6ba40baa901cfb7696d1ccfee9cef3060
ipynb
Jupyter Notebook
Data_Fitting_Exercise_S2_1.ipynb
pythoninchemistry/chem_data_analysis_jupyter
4af545f1a8acdded28d96508bb5adc8929da92cc
CC-BY-4.0
2019-05-05T00:21:55.000Z
2021-09-16T14:15:15.000Z
Data_Fitting_Exercise_S2_1.ipynb
pythoninchemistry/chem_data_analysis_jupyter
4af545f1a8acdded28d96508bb5adc8929da92cc
CC-BY-4.0
Data_Fitting_Exercise_S2_1.ipynb
pythoninchemistry/chem_data_analysis_jupyter
4af545f1a8acdded28d96508bb5adc8929da92cc
CC-BY-4.0
Qwen/Qwen-72B
1. YES
2. YES