Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

#! /usr/bin/env python 

# -*- coding: utf-8 -*- 

""" 

Examples of several algorithm for numerical integration problems. 

See the `integrals.py <integrals.html>`_ file for more details. 

 

 

- *Date:* Saturday 18 June 2016, 18:59:23. 

- *Author:* `Lilian Besson <https://bitbucket.org/lbesson/>`_ for the `CS101 course <http://perso.crans.org/besson/cs101/>`_ at `Mahindra Ecole Centrale <http://www.mahindraecolecentrale.edu.in/>`_, 2015. 

- *Licence:* `MIT Licence <http://lbesson.mit-license.org>`_, © Lilian Besson. 

""" 

 

from __future__ import division, print_function, absolute_import # Python 2/3 compatibility 

import math 

from integrals import * 

 

 

# Do some quick tests (it should be in the 'tests.py' file): 

19 ↛ exitline 19 didn't exit the module, because the condition on line 19 was never falseif __name__ == '__main__': 

print("\nSome examples of numerical integrations:\n") 

 

# # %% Example 1 

f = math.cos 

print("Plotting and computing the integral of cos(x) from 0 to pi.") 

print("Formally, the value is about 0 (= sin(pi) - sin(0)).") 

print("With 15 left rectangles:", riemann_left(f, 0, math.pi, n=15)) 

print("With 15 right rectangles:", riemann_right(f, 0, math.pi, n=15)) 

print("With 15 center rectangles:", riemann_center(f, 0, math.pi, n=15)) 

print("With 3 trapezoides:", trapez(f, 0, math.pi, n=3)) 

 

# print("With 15 left rectangles:", plot_riemann_left(f, 0, math.pi, 

# namef=r"$x \mapsto \cos(x)$",n=15, figname="riemannleft.png")) 

# print("With 15 right rectangles:", plot_riemann_right(f, 0, math.pi, 

# namef=r"$x \mapsto \cos(x)$",n=15, figname="riemannright.png")) 

# print("With 15 center rectangles:", plot_riemann_center(f, 0, math.pi, 

# namef=r"$x \mapsto \cos(x)$",n=15, figname="riemanncenter.png")) 

# print("With 3 trapezoides:", plot_trapez(f, 0, math.pi, 

# namef=r"$x \mapsto \cos(x)$",n=3, figname="trapezoides.png")) 

 

# # %% Example 2 

# f = lambda x: x**2 

# print("Plotting and computing the integral of x**2 from 0 to 3.") 

# print("Formally, the value is about 9 (= 3**3 / 3).") 

# print("With 8 left rectangles:", plot_riemann_left(f, 0, 3, 

# namef="$x \mapsto x^2$", n=8, figname="riemannleft2.png")) 

# print("With 8 right rectangles:", plot_riemann_right(f, 0, 3, 

# namef="$x \mapsto x^2$", n=8, figname="riemannright2.png")) 

# print("With 8 center rectangles:", plot_riemann_center(f, 0, 3, 

# namef="$x \mapsto x^2$", n=8, figname="riemanncenter2.png")) 

# print("With 3 trapezoides:", plot_trapez(f, 0, 3, 

# namef="$x \mapsto x^2$", n=3, figname="trapezoides2.png")) 

 

# %% Example 3 with Monte-Carlo 

n = 10000 

 

xmin, xmax = 1, 6 

 

def f1(x): 

""" f1(x) = x""" 

return x 

 

# For this particular example, its easy: 

ymin, ymax = xmin, xmax 

 

print("\nFor this function f1: x → x, on I = [{}, {}], we will pick {} random points.".format(xmin, xmax, n)) 

print("Manually, I chose ymin = {}, ymax = {}.".format(ymin, ymax)) 

 

random.seed(1) 

intf = montecarlo(f1, xmin, xmax, n, ymin, ymax) 

print("This leads to an approximated integral value of {}.".format(intf)) 

print("Error % is", 100 * abs(17.5 - intf)) 

 

ymin, ymax = yminmax(f1, xmin, xmax, n) # guess! 

print("But experimentally, I found a possible ymin, ymax to be {}, {}.".format(ymin, ymax)) 

intf = montecarlo(f1, xmin, xmax, n, ymin, ymax) 

print("This leads to a second approximated integral value of {}.".format(intf)) 

print("Error % is", 100 * abs(17.5 - intf)) 

 

# plot_montecarlo(f, xmin, xmax, 1500, ymin, ymax, namef=r"$x \mapsto x$", figname="montecarlo.png") 

 

print("Formally, we compute the integral as 17.5 (36/2 - 1/2).") 

 

# %% Example 4 with Monte-Carlo 

xmin, xmax = 0.0, 1.0 

 

def f2(x): 

""" f1(x) = x""" 

return x ** 3 

 

# For this particular example, its easy: 

ymin, ymax = xmin, xmax 

 

print("\nFor this function f2: x → x^3, on I = [{}, {}], we will pick {} random points.".format(xmin, xmax, n)) 

print("Manually, I chose ymin = {}, ymax = {}.".format(ymin, ymax)) 

intf = montecarlo(f2, xmin, xmax, n, ymin, ymax) 

print("This leads to an approximated integral value of {}.".format(intf)) 

print("Error % is", 100 * abs(0.25 - intf)) 

 

ymin, ymax = yminmax(f2, xmin, xmax, n) # guess! 

print("But experimentally, I found a possible ymin, ymax to be {}, {}.".format(ymin, ymax)) 

intf = montecarlo(f2, xmin, xmax, n, ymin, ymax) 

print("This leads to a second approximated integral value of {}.".format(intf)) 

print("Error % is", 100 * abs(0.25 - intf)) 

 

# plot_montecarlo(f2, xmin, xmax, ymin, ymax, 1500, namef=r"$x \mapsto x^3$", figname="montecarlo2.png") 

print("Formally, we compute the integral as 0.25 (1/4).") 

 

# %% Example 5 with Monte-Carlo 

xmin, xmax = 1e-8, 3. 

 

def f3(x): 

""" f3(x) = x""" 

return 1.0 / (1 + (math.sinh(2 * x)) * (math.log(x)**2)) 

 

# For this particular example, its easy: 

ymin, ymax = 0, 1 

 

print("\nFor this function f3: x → 1/(1+sinh(2x)log(x)^2) on I = [{}, {}], we will pick {} random points.".format(xmin, xmax, n)) 

print("Manually, I chose ymin = {}, ymax = {}.".format(ymin, ymax)) 

intf = montecarlo(f3, xmin, xmax, n, ymin, ymax) 

print("This leads to an approximated integral value of {}.".format(intf)) 

 

ymin, ymax = yminmax(f3, xmin, xmax, n) # guess! 

print("But experimentally, I found a possible ymin, ymax to be {}, {}.".format(ymin, ymax)) 

intf = montecarlo(f3, xmin, xmax, n, ymin, ymax) 

print("This leads to a second approximated integral value of {}.".format(intf)) 

 

# plot_montecarlo(f3, xmin, xmax, ymin, ymax, 1500, namef=r"$x \mapsto 1/(1+\sinh(2x)\log(x)^2)$", figname="montecarlo3.png") 

 

# plot_montecarlo(f3, xmin, xmax, ymin, ymax, 10000, namef=r"$x \mapsto 1/(1+\sinh(2x)\log(x)^2)$", figname="montecarlo4.png") 

 

 

# End of tests.py