def test_euler(): from solver.algorithms import euler def f(x, y): return x**5 / y assert np.allclose( np.array(euler(.3, .9, 4, 4, f, False)), np.array([(.3, 4), (.5, 4.00012), (.7, 4.00168), (.9, 4.01008)])) def g(x, y): return -2 + 2 * x - 2 * y assert euler(1, 2, 5, 2, g, False) == [(1, 2), (1.25, 1), (1.5, .625), (1.75, 0.5625), (2, 0.65625)]
def problem4(t0, tf, NA0, NB0, tauA, tauB, n, returnlist=False): """Uses Euler's method to model the solution to a radioactive decay problem where dNA/dt = -NA/tauA and dNB/dt = NA/tauA - NB/tauB. Args: t0 (float): Start time tf (float): End time NA0 (int): Initial number of NA nuclei NB0 (int): Initial number of NB nuclei tauA (float): Decay time constant for NA tauB (float): Decay time constant for NB n (int): Number of points to sample at returnlist (bool) = Controls whether the function returns the list of points or not. Defaults to false Returns: solution (list): Points on the graph of the approximate solution. Each element in the list has the form (t, array([NA, NB])) In the graph, NA is green and NB is blue """ print( "Problem 4: ~Radioactive Decay~ dNA/dt = -NA/tauA & dNB/dt = NA/tauA - NB/tauA - NB/tauB" ) N0 = np.array([NA0, NB0]) A = np.array([[-1 / tauA, 0], [1 / tauA, -1 / tauB]]) def dN_dt(t, N): return A @ N h = (tf - t0) / (n - 1) print("Time step of %f seconds." % h) solution = alg.euler(t0, tf, n, N0, dN_dt) if returnlist: return solution
def problem3(t0, tf, n, a, b, returnlist=False): """Uses Euler's method to graph velocity of an object subject to friction and approximate terminal velocity Assumes that the the objects starts at the origin. Args: t0 (float): Start time tf (float): End time n (float): Number of points to sample at a (float): A constant such that dv/dt = a - bv b (float): A constant such that dv/dt = a - bv returnlist (bool) = Controls whether the function returns the list of points or not. Defaults to false Returns: solution (list): Points on the graph of the approximate solution as a list of tuples terminalvelocity (float): An approximation of the terminal velocity """ print("Problem 3: ~Terminal Velocity~ dv/dt = a - bv") def vel(t, v): return a - b * v h = (tf - t0) / (n - 1) print("Graph of approximate solution") print("Time step of %f seconds." % h) solution = alg.euler(t0, tf, n, 0, vel) terminalvelocity = solution[-1][1] print("Conclusion: Terminal velocity is about %f." % terminalvelocity) if returnlist: return solution, terminalvelocity else: return terminalvelocity
def problem2(t0, tf, x0, v): """Uses Euler's method to graph position as function of time, with 2, 5, and 10 sample points Args: t0 (float): Start time tf (float): End time x0 (float): Initial position v (float): Velocity (assumed to be a constant) Returns: twopoints (List): Two points on the solution curve. fivepoints (List): Five points on the solution curve. tenpoints (List): Ten points on the solution curve. """ print("Problem 2: ~Position~ dx/dt = -v") def vel(t, x): return v print("Approximate solutions:") h = (tf - t0) / 2 print("Time step of %f seconds." % h) threepoints = alg.euler(t0, tf, 3, x0, vel) print(threepoints) h = (tf - t0) / 5 print("Time step of %f seconds." % h) sixpoints = alg.euler(t0, tf, 6, x0, vel) print(sixpoints) h = (tf - t0) / 10 print("Time step of %f seconds." % h) elevenpoints = alg.euler(t0, tf, 11, x0, vel) print(elevenpoints) print("Conclusion: x = x0 + v * t") return threepoints, sixpoints, elevenpoints
def problem1(t0, tf, v0): """Uses Euler's method to graph velocity subject to Earth's gravity run as function of time, with 2, 5, and 10 sample points Args: t0 (float): Start time tf (float): End time v0 (float): Initial velocity Returns: twopoints (List): Two points on the solution curve. fivepoints (List): Five points on the solution curve. tenpoints (List): Ten points on the solution curve. """ print("Problem 1: ~Gravity~ dv/dt = -g") def accel(t, v): return -9.8 print("Approximate solutions:") h = (tf - t0) / 2 twopoints = alg.euler(t0, tf, 3, v0, accel) print("Time step of %f seconds." % h) print(twopoints) h = (tf - t0) / 5 fivepoints = alg.euler(t0, tf, 6, v0, accel) print("Time step of %f seconds." % h) print(fivepoints) h = (tf - t0) / 10 tenpoints = alg.euler(t0, tf, 11, v0, accel) print("Time step of %f seconds." % h) print(tenpoints) print("Conclusion: v = v0 - g * t") return twopoints, fivepoints, tenpoints