changeset 83:1b61ce99ee82

derivative calculation: midpoint rule
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 13:51:13 -0800
parents c05ce6b7f941
children 0f3af15bb29a
files tests/test_derivative.py tvii/derivative.py
diffstat 2 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_derivative.py	Sun Dec 17 13:51:13 2017 -0800
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+
+import os
+import shutil
+import tempfile
+import unittest
+from tvii import derivative
+
+class TestDerivatives(unittest.TestCase):
+
+    def test_midpoint(self):
+        def cube(x):
+            return x**3
+
+        exact = 3
+        expected = 3.0001
+        got = derivative.midpoint(cube, 1, 0.01)
+        assert abs(expected - got) < 1e-6, "Expected {}; got {}".format(expected, got)
+
+if __name__ == '__main__':
+    unittest.main()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tvii/derivative.py	Sun Dec 17 13:51:13 2017 -0800
@@ -0,0 +1,4 @@
+def midpoint(f, x, epsilon):
+    """midpoint rule"""
+
+    return (f(x+epsilon) - f(x-epsilon))/(2.*epsilon)