-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path002_integrate.py
More file actions
executable file
·61 lines (42 loc) · 1.16 KB
/
002_integrate.py
File metadata and controls
executable file
·61 lines (42 loc) · 1.16 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# FileName: 002_integrate.py
#
# Description:
#
# Version: 1.0
# Created: 2018-06-13 09:26:33
# Last Modified: 2019-09-03 10:39:24
# Revision: none
# Compiler: gcc
#
# Author: zt ()
# Organization:
import numpy as np
from scipy.integrate import quad, dblquad
from scipy.special import jn
def f(x):
return x
x_lower = 0
x_upper = 1
val, abserr = quad(f, x_lower, x_upper)
print("integral value=", val, ",absolute error=", abserr)
def integrand(x, n):
return jn(n, x)
x_lower = 0
x_upper = 10
val, abserr = quad(integrand, x_lower, x_upper, args=(3, ))
print("integral value=", val, ",absolute error=", abserr)
val, abserr = quad(lambda x: np.exp(-x**2), -np.Inf, np.Inf)
print("integral value=", val, ",absolute error=", abserr)
analytical = np.sqrt(np.pi)
print("analytical=", analytical)
def integrand(x, y):
return np.exp(-x**2 - y**2)
x_lower = 0
x_upper = 10
y_lower = 0
y_upper = 10
val, abserr = dblquad(integrand, x_lower, x_upper, lambda x: y_lower,
lambda x: y_upper)
print("integral value=", val, ",absolute error=", abserr)