So far this morning I’ve

- Gotten jupyter notebooks running natively on my workstation rather than using the linux subsystem as a workaround
- Learned how to integrate functions in python using scipy
- Got exporting of jupyter notebooks to html working properly
- Built a scale model of the terrain around my home town in Unity
- Gained three levels on my warlock

Now I’m going to put my pants on.

## Integrating Functions

Quick example on integrating functions with jupyter, matplotlib, numpy, and scipy

### Import the required libraries

1 2 3 4 |
%matplotlib inline import numpy as np from scipy.integrate import quad from matplotlib import pyplot as plt |

### Define your function

For this example we’ll be using \(f(x) = e^{-x}sin(3x)\) as our function.

1 2 3 |
def f(x): """Sample equation""" return(np.exp(-x) * np.sin(3.0*x)) |

### Set the range for your plot and integration

To plot \(f(x)\) we get an array of linearly spaced values from numpy, pass that array and our function to the plot function, and show the plot.

Evalating the integral \(\int_{0}^{2\pi}e^{-x}sin(3x)\) simply requires passing our function and the range of the intgral to scipy’s quad function which will return a tuple of the integral and the estimated error.

1 2 3 4 5 6 7 |
x = np.linspace(0, 2*np.pi, 100) plt.plot(x, f(x)) plt.show() i, err = quad(f, 0, 2*np.pi) print("Integral:",i) print("Estimated error", err) |

Integral: 0.29943976718048754

Estimated error 5.05015300411582e-13