Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- from sympy import Symbol, Integral, exp, log
- from scipy.special import roots_legendre
- def function(x):
- f = 4 * np.sin(x * x) - np.log(x + 4)
- return f
- def foo():
- _x = Symbol('x')
- return Integral(exp(_x) * log(_x), (_x, 1, 2))
- def method_Gaussian(a, b, n):
- b_a_half_size = (b - a) / 2
- a_b_half_sum = (a + b) / 2
- roots, weights = roots_legendre(n)
- _sum = 0
- for i in range(n):
- _sum += weights[i] * function(a_b_half_sum + b_a_half_size * roots[i])
- _sum *= b_a_half_size
- print(str('Gaussian quadrature: ') + str(_sum))
- def draw_function():
- x = np.arange(0.1, 2.5, 0.07)
- f = function(x)
- line = plt.plot(x, f, color='limegreen')
- plt.fill_between(x, f, color='palegreen', where=(x < 2) & (f > 0))
- plt.grid()
- plt.setp(line, color="limegreen", linewidth=1.5)
- plt.gca().spines["left"].set_position("zero")
- plt.gca().spines["bottom"].set_position("zero")
- plt.gca().spines["top"].set_visible(False)
- plt.gca().spines["right"].set_visible(False)
- plt.show()
- def main():
- _a = 1
- _b = 2
- _n = 6
- method_Gaussian(_a, _b, _n)
- draw_function()
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment