Jupyter notebook Week 3-4/Homework 2- Exponential Functions, Curve Fitting, and Ordinary Differential Equations.ipynb
#Earth Systems Modeling ##Lab 2: Exponential Functions, Ordinary Differential Equations & Simulations
Problem 1. Regression Analysis of a Population Model
In this exercise, we will be using a linear regression to fit our data with our simple exponential model. The data of column one represents time measurements, while column 2 is for individual bacterium in the units individuals. You will then use the same analysis to fit data for a degrading compound in problem 2 below.
Part 1. Loading data as a numpy array. In the next cell we are using numpy to load a data file called expodata.txt. The file rests in the same directory as our notebook, which is why we use the notation Why is skiprows set to 1? Be sure to check your datafile by opening it. Documentation for loadtxt may be found here.
Part 2. Write a python function called exponential.py for the expression: . Your function should have three arguments, the first should be an array of your independent variable. See the documentation of numpy arrays here. Your larger array of data has two columns. NumPy arrays may be address as expodata[rows, columns]. To collect all of the values for x[ind], you would type expodata[0:,1].
Be sure to save your function in a file that will be imported in your final plotting script.
Part 3. Our next step is to create a linear regression in python. The procedure is relatively simple, once your data has been loaded. The first step is to linearize your data. Think about how you would perform this step mathematically. Hint: look back to the derivation of the exponential relationship, equation (4) in your reading holds a major clue. You can read the SciPy documentation here.
Once you have the slope and intercept for your linear fit, you will have to perform the inverse mathematical operation to convert your data back into an exponential function. Plot this "exponential model" found by linear regression against your data. The model should appear as a solid line, and the data as points. Documentation may be found here.
Part 4. Since we have a good idea our data follows an exponential relationship, we can use a nonlinear fit. In SciPy the optimize module has a curve fit function. The advantage of a non-linear fit is that our data does not need to be linearized in advance.
Once you have the estimated parameters for you nonlinear fit, plot this "exponential model" against your data. The model should appear as a solid line, and the data as points.
For your notebook, describe your data and reason for making a exponential fit. In your description you should your plots with a figure caption. Finally, make a prediction (extrapolation) concerning the time it will take for your population to double it's final measured size. When presenting your prediction, be sure to state 1 or 2 reasons why the colony might not reach this ultimate size in the predicted time.
Problem 2. Regression Analysis of Chemical Fate Model
Follow the steps enumerated above for the dataset expodata2.txt provided in your sagemath folder. This time the dataset represents the radioactive decay of Polonium-210 (becquerel) over a period of days. Your notebook description should at a minimum introduce the data and the two plots, but may be much more concise. Finally, use the rate constant and initial condition from your nonlinear fit to calcuate the half-life of the unknown chemical from your dataset.