In [1]:
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
%matplotlib inline


/home/holla/anaconda2/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

In [9]:
# reference
# http://www.etcs.ipfw.edu/~lin/ECET307/Fall2006/Lectures/4_ECET307Week4_F06.html
    
def f(x):
    v = np.sin(x) + 0.0
    if v > 0.0:
        return v
    else:
        return 0.0

x = np.linspace(0,2*np.pi,1000)
fv = np.vectorize(f,otypes=[float])
y = fv(x)

plt.xkcd()
plt.figure(figsize=(14,10))
plt.plot(x,y)
plt.fill_between(x,0,y,facecolor='lightgrey')
plt.show()

i = integrate.quad(f, 0, 2*np.pi)
print "integral:",i[0]

# http://www.electronics-tutorials.ws/accircuits/rms-voltage.html
rms = np.sqrt(np.sum(fv(x)**2)/len(x))
print "rms:     ","%.2f"%rms


integral: 2.0
rms:      0.50

In [ ]: