In [1]:
get_ipython().magic(u'pylab inline')
from IPython.html.widgets import interact
def armonico(x, n, amp_fun):
L = x[-1]
km = pi * n / L
y = amp_fun(n, L) * sin(km* x)
return y
def fourier(x, n, amp_fun):
y = zeros_like(x)
for i in xrange(1, n+1):
y += armonico(x, i, amp_fun)
return y
In [2]:
def amp4(n, L):
return 8*L*sin(n * pi / 2) / ((n**2) * pi**2)
n = range(10)
a = [amp4(x, 1) for x in n]
vlines(n, zeros(len(a)), a)
plot(n, a, '.')
plot([0, n[-1]], [0, 0], 'k')
grid(True)
xlabel('Armonico')
ylabel('Amplitud');
In [8]:
@interact(armonicos=(1, 40, 2))
def f(armonicos=1):
L = 1
x = linspace(0, L, 100)
y = fourier(x, armonicos, amp_fun=amp4)
y0 = concatenate([2*x[:50], -2*(x[50:] - L)])
ylim([0, 1])
plot(x, y0, 'k--', label='triangulo')
plot(x, y, 'b', label='fourier '+str(armonicos))
plot(x, y0 - y, 'r', label='residuos')
legend()
In [10]:
def amp5(n, L):
return 16.*(sin(3*n*pi/4)-sin(n*pi/4))/(n**2 * pi**2)
n = range(20)
a = [amp5(x, 1) for x in n]
vlines(n, zeros(len(a)), a)
plot(n, a, '.')
plot([0, n[-1]], [0, 0], 'k')
grid(True)
xlabel('Armonico')
ylabel('Amplitud')
Out[10]:
In [15]:
@interact(armonicos=(1, 40, 2))
def f(armonicos=3):
L = 1.0
x = linspace(0, L, 100)
y0 = concatenate([-4*x[:25], 4*(x[25:75] - L/2), -4*(x[75:]-L)])
y = fourier(x, armonicos, amp_fun=amp5)
ylim([-1, 1])
plot(x, y0,'k--', label='condicion inicial')
plot(x, y,'b', label='fourier ' + str(armonicos))
plot(x, y0 - y,'r', label='residuos')
legend(loc='upper left')