t,x=var('t','x')
gam,A,B=var('gam','A','B')
k,L=var('k','L')
gam=15;A=1;B=2;k=1;L=1;
v=function('v')(x)
v=-(1/2)*(gam/k)*x^2+(1/L)*(B-A+gam*L^2/(2*k))*x+A
P1=plot(v,[x,0,L],color='red',linestyle='--')
N=5
bbn=[(2*gam)/(n^3*pi^3)*((-1)^n-1)-2/(n*pi)*(A-B*(-1)^n) for n in range(1,N+1)]
u=v+sum(bbn[n]*exp(-k*((n+1)*pi/L)^2*t)*sin(((n+1)*pi/L)*x) for n in range(0,N))
P2=plot(u.substitute(t==0),[x,0,L]);
P3=plot(u.substitute(t==0.01),[x,0,L]);
P4=plot(u.substitute(t==0.08),[x,0,L]);
P5=plot(u.substitute(t==0.2),[x,0,L]);
P6=plot(u.substitute(t==0.6),[x,0,L],color='green');
(P1+P2+P3+P4+P5+P6).show()
plot3d(u,[t,0,1],[x,0,L],plot_points=[20,20],aspect_ratio=(1,1,0.2),mesh=True)
P=[]
P.append(plot(u.substitute(t==0),[x,0,L]))
for i in range(1,20):
intt=0.03*i
P.append(plot(u.substitute(t==intt),[x,0,L]))
a=animate(P)
a.show(delay=50)