In: Computer Science
USE PYTHON TO SOLVE THIS PROBLEM
A bacteria culture grows exponentially. After 2 hours, the bacteria count was 400 cells and after 6 hours, the bacteria count was 20,000 cells.
a) Solve a system of equations to find (approximately) k and y0 (HINT: If will help to assume K is real in the symbols command)
b) Use this to determine when the bacteria count reaches 2,000,000 (exact and approximate).
c) Suppose 400 was the “initial” amount and 20,000 the count after 4 hours. Find k and the approximate amount of bacteria 2 hour BEFORE the “initial” time. In a print statement, explain what you notice when comparing these answers to part a)
Here's the Python Code for the same:
import numpy as np
from scipy.optimize import fsolve
# bacterial growth eqn is of the form y0 * (e^(k * t)) = n
t1, n1, t2, n2 = 0, 0, 0, 0
# f(x) returns the functions for using in fsolve
def f(x):
y0 = x[0]
k = x[1]
f1 = y0 * np.exp(k * t1) - n1
f2 = y0 * np.exp(k * t2) - n2
return np.array([f1, f2])
# function to find t for given n, y0 & k
def findT(n, y0, k):
return (np.log(n / y0) / k)
# function to find n for given t, y0 & k
def findN(t, y0, k):
return (y0 * np.exp(k * t))
# driver code
if __name__ == '__main__':
# for part a)
t1, n1, t2, n2 = 2, 400, 6, 20000
# guessed solution
xGuess = np.array([1, 2])
# calculating actual soln using xGuess
sol = fsolve(f, xGuess)
print("Part a)\ny0 = {}, k = {}\n".format(sol[0], sol[1]))
# for part b)
# finding time for bacteria count to reach 2 mn using above sol
t = findT(2000000, sol[0], sol[1])
print("Part b)\nTime required =", t, "hours\n")
# for part c)
# setting the given values
t1, n1, t2, n2 = 0, 400, 4, 20000
xGuess = np.array([1, 2])
# solving the equations for new y0 & k using xGuess
sol = fsolve(f, xGuess)
# finding n for newly gotten y0 & k
n = findN(-2, sol[0], sol[1])
print("Part c)\nBacteria count 2 hours prior initial = ", n)
print("For comparison to Part a)\ny0 = {}, k = {}".format(sol[0], sol[1]))
Code Screenshot (for indentation lookup in case any issue while running the pgm):
Output Screenshot:
Added comments in the code for better understanding, do refer them. Also, for any doubt, feel free to reach back.
Hope it helps, consider hitting a like if it did :)
Cheers!