%pylab inline
x = linspace(0,2*pi,11)
y = sin(x)
plot(x,y);
We draw smooth-looking curves by drawing polygonal curves with very many points:
x = linspace(0,2*pi,211)
y = sin(x)
plot(x,y);
x = linspace(0,2*pi,5)
x
100*x
y = sin(x)
y
L = 2.
t = .01
n = 12
x = linspace(0,L,200)
y = sin(n*pi*x/L)*exp(-(n*pi/L)**2*t )
plot(x,y)
for t in [1,2,5]:
print(t)
for t in linspace(0,.1,6):
print(t)
First plot just one of our elementary solutions:
n = 12
for t in linspace(0,.01,6):
y = sin(n*pi*x/L)*exp(-(n*pi/L)**2*t )
plot(x,y)
print(t)
Then another one, with a smaller value of n:
n = 4
for t in linspace(0,.01,6):
y = sin(n*pi*x/L)*exp(-(n*pi/L)**2*t )
plot(x,y)
print(t)
A linear combination of two of the elementary solutions:
for t in linspace(0,.01,6):
y = 1.3*sin(4*pi*x/L)*exp(-(4*pi/L)**2*t ) + 0.5*sin(12*pi*x/L)*exp(-(12*pi/L)**2*t )
plot(x,y)
print(t)
Now we will build the solution of the IBVP we figured out in class last time.
M = 4
x = linspace(0,L,5)
n = arange(1,M+1)
n
We will build a table of values of $\sin\frac{n\pi x}{L}$, in stages ...
outer(x,n)
If matrix o = outer(x,n), then $$o_{ij} = x_i n_j$$
10*outer(x,n)
outer(x,n)*pi/L
sin(outer(x,n)*pi/L).round(5)
B = 200*(1-cos(n*pi))/(n*pi)
B
(B*sin(outer(x,n)*pi/L)).round(5)
for t in linspace(0,.01,6):
Q = (B*sin(outer(x,n)*pi/L))*exp(-(n*pi/L)**2*t)
u = Q.sum(axis=1)
print(u.round(5))
for t in linspace(0,.01,6):
Q = (B*sin(outer(x,n)*pi/L))*exp(-(n*pi/L)**2*t)
u = Q.sum(axis=1)
plot(x,u)
Now increase the number of x values to make the curves smooth, and increase the number of included terms, M, to make the approximations more accurate:
M = 40
tfinal = 1.
x = linspace(0,L,200)
n = arange(1,M+1)
B = 200*(1-cos(n*pi))/(n*pi)
for t in linspace(0,tfinal,100)[1:]:
Q = (B*sin(outer(x,n)*pi/L))*exp(-(n*pi/L)**2*t)
u = Q.sum(axis=1)
plot(x,u,'b',alpha=0.5)
xlabel('x')
ylabel('u(x,t)');