# Integral simbólica usando SymPy





```python
import numpy as np
import sympy as sy
from sympy.utilities.lambdify import lambdify
from scipy.integrate import quad
from scipy.misc import derivative
```


```python
x = sy.Symbol('x')
f = sy.exp(-x)*sy.sin(3.0*x)
res_symbolic = sy.integrate(f)
res = sy.integrate(f, (x, 0, 2*sy.pi))

print(res.evalf())
```

    0.299439767180488



```python
f
```




$\displaystyle e^{- x} \sin{\left(3.0 x \right)}$




```python
res_symbolic
```




$\displaystyle - 0.1 e^{- x} \sin{\left(3.0 x \right)} - 0.3 e^{- x} \cos{\left(3.0 x \right)}$




```python
%timeit sy.integrate(f, (x, 0, 2*sy.pi))
```

    207 ms ± 9.18 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)



```python
# Lambdfy integrals
res_symbolic = sy.integrate(f)
integ = lambda x0, x1: res_symbolic.evalf(subs={x: x1}) - res_symbolic.evalf(subs={x: x0})
%timeit integ(0, 2*np.pi)
```

    1.53 ms ± 88.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)



```python
integ = lambda x0, x1: float(sy.integrate(f, (x, x0, x1)))
%timeit integ(0, 2*np.pi)
```

    171 ms ± 12.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)


## Multi-varible


```python
x, y = sy.symbols('x, y')
f = x**2
h = y**2
g = f + h
g1 = g.subs(x,1)

```




$\displaystyle 5$




```python
# Integrate g(x,y)*dx
sy.integrate(g,x)
```




$\displaystyle \frac{x^{3}}{3} + x y^{2}$




```python
# Integrate g(x,y)*dy
sy.integrate(g,y)
```




$\displaystyle x^{2} y + \frac{y^{3}}{3}$




```python
# Double integral g(x,y)*dx*dy
sy.integrate(sy.integrate(g,x),y)
```




$\displaystyle \frac{x^{3} y}{3} + \frac{x y^{3}}{3}$




```python
# Double integral g(x,y)*dx*dy, xfrom 0 to 1, y from zero to 1
sy.integrate(sy.integrate(g,(x,0,1)),(y,0,1))
```




$\displaystyle \frac{2}{3}$




```python
# Evaluating the results
sy.integrate(sy.integrate(g,(x,0,1)),(y,0,1)).evalf()
```




$\displaystyle 0.666666666666667$




```python
# Show the symbolic
sy.Integral(sy.Integral(g,(x,0,1)),(y,0,1))
```




$\displaystyle \int\limits_{0}^{1}\int\limits_{0}^{1} \left(x^{2} + y^{2}\right)\, dx\, dy$



# Using Scipy to Numerical integrate defined functions


```python
def f(x):
    return np.exp(-x)*np.sin(3.0*x)

i, err = quad(f, 0, 2*np.pi)
print(i)
```

    0.29943976718048754



```python
%timeit i, err = quad(f, 0, 2*np.pi)
```

    9.72 µs ± 1.04 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)



```python
def foo(x, y):
  return(x**2 + x*y**3)

from scipy.misc import derivative
derivative(foo, 1, dx = 1e-6, args = (2, ))
```




    9.999999999621423



# Derivada simbólica


```python
x = sy.Symbol('x')
f = 3*x**2 + 1
f
```




$\displaystyle 3 x^{2} + 1$




```python
# Lambdfy derivatives
ddx = lambdify(x, sy.diff(f, x)) # creates a function that you can call
%timeit ddx(2)
```

    91.8 ns ± 10.2 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)



```python
dx = sy.diff(f, x)
ddx = lambda x0: dx.subs(x, x0)
%timeit ddx(2)
```

    14.2 µs ± 345 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)



```python
# Derivada de segunda ordem
sy.diff(f, (x, 2))
```




$\displaystyle 6$



# Derivada numérica usando Scipy


```python
def f(x):
    return 3*x**2 + 1

%timeit derivative(f, 2)
```

    13.3 µs ± 401 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)


d1457471f33e9560edf18424c66baf2c4be21e2a
ipynb
Jupyter Notebook
VA/work1/numerical_and_symbolic_integration.ipynb
lucas-schroeder/Master_program_UFSC
4a1cecfa1ebcd57968449d0650abd11d782df71c
MIT
VA/work1/numerical_and_symbolic_integration.ipynb
lucas-schroeder/Master_program_UFSC
4a1cecfa1ebcd57968449d0650abd11d782df71c
MIT
VA/work1/numerical_and_symbolic_integration.ipynb
lucas-schroeder/Master_program_UFSC
4a1cecfa1ebcd57968449d0650abd11d782df71c
MIT
Qwen/Qwen-72B
1. YES
2. YES