def testStableODE2(self): iv = np.array([1.0]) h: np.float = 0.01 y = odesolvers.ExplicitEulerSolver(stableode, iv, self.t0, self.tn, h) self.assertEqual(y[0], iv) self.assertEqual(y[1], (y[0] - h))
def testConstantODE2(self): iv = np.array([1.0]) h: np.float = 0.1 y = odesolvers.ExplicitEulerSolver(constantode, iv, self.t0, self.tn, h) self.assertEqual(y[0], iv) self.assertEqual(y[1], (y[0] + h))
def testStiffODE2(self): iv = np.array([1.0, 2.0]) h: np.float = 0.05 N: np.uint = np.uint(np.ceil((self.tn - self.t0) / h)) # final step y = odesolvers.ExplicitEulerSolver(stiffode, iv, self.t0, self.tn, h) self.assertEqual(y[0, 0], iv[0]) self.assertEqual(y[0, 1], iv[1]) self.assertGreater(np.absolute(y[N, 1] - y[0, 1]), 3)
def testMultivariableODE(self): iv = np.array([1.0, 2.0]) h: np.float = 0.01 y = odesolvers.ExplicitEulerSolver(multivariableode, iv, self.t0, self.tn, h) self.assertEqual(y[0, 0], iv[0]) self.assertEqual(y[0, 1], iv[1]) self.assertEqual(y[1, 0], (iv[0] + h * (-iv[0] + iv[1]))) self.assertEqual(y[1, 1], (iv[1] - h * iv[1]))
:type t: np.float :type x: np.array[float] :return: Derivative of state at current time :rtype: np.array[float] """ xprime = np.empty([2], float) xprime[0] = -x[0] xprime[1] = -100 * (x[1] - np.sin(t)) + np.cos(t) return xprime if __name__ == '__main__': iv = np.array([1.0, 2.0]) t0: np.float = 0.0 tn: np.float = 1.0 h = np.array([0.01, 0.05]) for hi in np.nditer(h): # solving the ODE y = odesolvers.ExplicitEulerSolver(hw2ex4ode, iv, t0, tn, hi) # plotting the results odesolvers.plotODEsol(y[:, 0], t0, hi, 'y1(t)') tikzplotlib.save(f'y1-{hi}.tex') odesolvers.plotODEsol(y[:, 1], t0, hi, 'y2(t)') tikzplotlib.save(f'y2-{hi}.tex')