Calculation of areas between two curves

两盒软妹~` 提交于 2019-12-01 11:56:16
pylang

Adapted from the scipy.integrate.quad docs example for a different function, y = x^2:

from scipy import integrate

def f(x):
    return x**2

integrate.quad(f, 0, 4)
# (21.333333333333332, 2.3684757858670003e-13)

print(4**3 / 3.)  # analytical result
# 21.3333333333

The result and an error for the numerical calculation is returned.

If you want an exact or symbolic answer, consider sympy. Here is a similar example applied to y = πx^2 (Note: leading underscores were used here to distinguish Sympy objects).

import sympy as sym

sym.init_printing()

_x = sym.symbols("x")
_f = sym.pi * _x**2
sym.integrate(_f, (_x, 0, 2))

Apply either of these techniques to your problem.

This is called . There's a bunch of standard methods. As @pylang said they're already implemented in scipy.integrate.* . scipy.integrate.quad is Gaussian quadrature.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!