Menu

[r3178]: / trunk / py4science / examples / skel / trapezoid_skel.py  Maximize  Restore  History

Download this file

44 lines (31 with data), 1.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
"""Simple trapezoid-rule integrator."""
import numpy as N
def trapzf(f,a,b,npts=100):
"""Simple trapezoid-based integrator.
Inputs:
- f: function to be integrated.
- a,b: limits of integration.
Optional inputs:
- npts(100): the number of equally spaced points to sample f at, between
a and b.
Output:
- The value of the trapezoid-rule approximation to the integral."""
raise NotImplementedError
if __name__ == '__main__':
# Simple tests for trapezoid integrator, when this module is called as a
# script from the command line. From ipython, run it via:
#
# run -e trapezoid
#
# so that ipython ignores the SystemExit exception automatically raised by
# the unittest module at the end.
import unittest
import numpy.testing as ntest
def square(x): return x**2
class trapzfTestCase(unittest.TestCase):
def test_square(self):
ntest.assert_almost_equal(trapzf(square,0,1),1./3,4)
def test_square2(self):
ntest.assert_almost_equal(trapzf(square,0,3,350),9.0,4)
unittest.main()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.