Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: pde
Views: 178

Various interacts for plotting solutions to certain partial differential equations

Instruction: to start an interact (or to start it again), press the button. To change an input, make the change and press enter. To change a function (say f) to a constant function (say 1), enter 1+x-x into the f box and press enter. Some time may elapse, so be patient.

The 1-d global heat equation:

ut(x,t)Duxx(x,t)=f(x,t)u_t(x,t)-D\,u_{xx}(x,t)=f(x,t), (x,t)R×(0,)(x,t) \in \mathbb{R}\times (0,\infty), with initial condition u(x,0)=g(x)u(x,0)=g(x) for xRx\in\mathbb{R}.

Duhamel says the solution can be obtained by integration: u(x,t)=RΓD(xy,t)g(y)dy+0tRΓD(xy,ts)f(y,s)dyds,u(x,t)=\displaystyle\int_{\mathbb{R}}\Gamma_D(x-y,t)\,g(y)\,dy + \int_0^t\,\int_{\mathbb{R}}\Gamma_D(x-y,t-s)\,f(y,s)\,dy\,ds, where ΓD(x,t)=14πDtexp(x24Dt)\Gamma_D(x,t)=\displaystyle\frac{1}{\sqrt{4\,\pi\,D\,t}}\exp(-\frac{x^2}{4\,D\,t}) is the fundamental solution to ut(x,t)Duxx(x,t)=0u_t(x,t)-D\,u_{xx}(x,t)=0, (x,t)R×(0,)(x,t)\in \mathbb{R}\times (0,\infty)

What does the graph of uu look like? This depends on the functions f(x,t)f(x,t), g(x)g(x) (which are assumed 0 when x∉[0,1]x \not\in [0,1]), and the parameter DD. We can use numerical integration methods (Gauss rules, Romberg table) to compute the values of u(x,t)u(x,t). The interact below shows the graph of r(x)=u(x,tmax)r(x) = u(x,tmax) over the interval [xmin,xmax]\text{[xmin,xmax]} or of q(t)=u(0,t)q(t)=u(0,t) over the interval [tmin,tmax]\text{[tmin,tmax]}. Play with this interact by changing any of the values of f, g, xmin, xmax, tmin, tmax, and D.

y,s,t=var('y,s,t') @interact(layout={'top': [['D','f','g','c'],['xmin','xmax','tmin','tmax']]}) def getD(D=1/100,f=x+t,g=(x-1/2)^2+1,xmax=3/2,xmin=-1/2,tmax=1/10,tmin=1/1000,c=selector(['t constant', 'x constant','3d'])): p=pi.n() def romberg_step(R0,a,h,er,f): n=len(R0) R1=[.5*R0[0]+.5*h*sum([f(a+(2*i+1)*.5*h) for i in range(2^(n-1))])] for m in range(1,n+1): R1+=[R1[m-1]+1/(4^m-1)*(R1[m-1]-R0[m-1])] er=(R1[-1]-R1[-2]) return R1,er def romberg(f,a,b,ER): h=b-a R0=[.5*h*(f(a)+f(b))] k=0 er=ER while k <10 and abs(er)>= ER: R0,er=romberg_step(R0,a,h,er,f) h=h/2 k+=1 return R0,er def g1(x): return g(x=x) def f1(x,t): return f(x=x,t=t) def GD(x,t): return 1/sqrt(4*D*p*t)*exp(-x^2/(4*D*t)) def u1(x,t): return numerical_integral(GD(x-y,t)*g1(y),0,1)[0] def u2(x,t): def w(s): def h(y): return GD(x-y,t-s)*f1(y,s) return numerical_integral(h(y),0,1)[0] bil=romberg(w,tmin,t-.000001,.00001); return bil[0][-1] def u(x,t): return u1(x,t)+u2(x,t) def r(x): return u(x,tmax) x0=(xmax-xmin)/2 def q(t): return u(0,t) G=Graphics() if c == 't constant': mess='time constant at t = '+str(tmax) dx=.01*(xmax-xmin) pts=([(xmin+i*dx,r(xmin+i*dx)) for i in range(1,99)]) p=spline(pts) G+=plot(p,(xmin,xmax)) t0=tmax tab=[['x'],['u(x,'+str(tmax.n(digits=4))+')']] pts2=[] x0=xmin dx=(xmax-xmin)/4 for j in range(5): pts2+=[pts[j*24]] tab[0]+=[pts2[-1][0].n(digits=6)] tab[1]+=[pts2[-1][1].n(digits=6)] x0+=dx G+=point(pts2) mess=table(tab) elif c=='x constant': mess='location constant at x = '+str(x0) dt=.01*(tmax-tmin) pts=[(tmin+i*dt,q(tmin+i*dt)) for i in range(1,99)] p=spline(pts) G+=plot(p,(tmin,tmax-.00001))+points(pts) else: mess='3d plot of u(x,t)' G+=plot3d(u,(x,xmin,xmax),(t,tmin+.00001,tmax-.00001),color='grey',opacity=.6,alpha=.5) show(G) print mess
Interact: please open in CoCalc
︠2741e208-86bf-4516-a760-a2cfc4bb54d8︠ y,s,t=var('y,s,t') @interact(layout={'top': [['D','f','g','c'],['xmin','xmax','tmin','tmax']]}) def getD(D=1/100,f=x+t,g=(x-1/2)^2+1,xmax=3/2,xmin=-1/2,tmax=1/10,tmin=1/1000,c=selector(['t constant', 'x constant'])): p=pi.n() def romberg_step(R0,a,h,er,f): n=len(R0) R1=[.5*R0[0]+.5*h*sum([f(a+(2*i+1)*.5*h) for i in range(2^(n-1))])] for m in range(1,n+1): R1+=[R1[m-1]+1/(4^m-1)*(R1[m-1]-R0[m-1])] er=(R1[-1]-R1[-2]) return R1,er def romberg(f,a,b,ER): h=b-a R0=[.5*h*(f(a)+f(b))] k=0 er=ER while k <10 and abs(er)>= ER: R0,er=romberg_step(R0,a,h,er,f) h=h/2 k+=1 return R0,er def g1(x): return g(x=x) def f1(x,t): return f(x=x,t=t) def GD(x,t): return 1/sqrt(4*D*p*t)*exp(-x^2/(4*D*t)) def u1(x,t): return numerical_integral(GD(x-y,t)*g1(y),0,1)[0] def u2(x,t): def w(s): def h(y): return GD(x-y,t-s)*f1(y,s) return numerical_integral(h(y),0,1)[0] bil=romberg(w,tmin,t-.000001,.00001); return bil[0][-1] def u(x,t): return u1(x,t)+u2(x,t) def r(x): return u(x,tmax) x0=(xmax-xmin)/2 def q(t): return u(0,t) G=Graphics() if c == 't constant': mess='time constant at t = '+str(tmax) dx=.01*(xmax-xmin) pts=([(xmin+i*dx,r(xmin+i*dx)) for i in range(1,99)]) p=spline(pts) G+=plot(p,(xmin,xmax)) t0=tmax tab=[['x'],['u(x,t0)']] pts2=[] x0=xmin dx=(xmax-xmin)/4 for j in range(5): pts2+=[pts[j*24]] tab[0]+=[pts2[-1][0].n(digits=6)] tab[1]+=[pts2[-1][1].n(digits=6)] x0+=dx G+=point(pts2) mess=table(tab) elif c=='x constant': mess='location constant at x = '+str(x0) dt=.01*(tmax-tmin) pts=[(tmin+i*dt,q(tmin+i*dt)) for i in range(1,99)] p=spline(pts) G+=plot(p,(tmin,tmax-.00001))+points(pts) else: G+=plot3d(u,(x,xmin,xmax),(t,tmin+.00001,tmax-.00001),color='grey',opacity=.6,alpha=.5) show(G) print mess
Interact: please open in CoCalc
︠c4eb26e6-7a6c-440d-bb87-b61e7886f371is︠ %md #### The Transport Equation: $u_t(x,t) + v\ u_x (x,t)= f(x,t), u(x,0)=g(x)$ If $v$ is constant, the solution is $u(x,t)=g(x-v\ t)+\displaystyle\int_0^t f(x-v(t-s),s)\ ds$. We can plot it for any nice $g$ and $f$. Use the interact below to experiment with changing the right hand side f and the initial condition g to see the effect on u. The graphs shown are of $u_{t_i}(x)=u(x,t_i)$ for $t_i=t_0+i\ step$ for $i=0, \cdots ,5$. They are color-coded black, red, blue, grey, magenta, turquoise in order of increasing time.

The Transport Equation: ut(x,t)+v ux(x,t)=f(x,t),u(x,0)=g(x)u_t(x,t) + v\ u_x (x,t)= f(x,t), u(x,0)=g(x)

If vv is constant, the solution is u(x,t)=g(xv t)+0tf(xv(ts),s) dsu(x,t)=g(x-v\ t)+\displaystyle\int_0^t f(x-v(t-s),s)\ ds. We can plot it for any nice gg and ff.

Use the interact below to experiment with changing the right hand side f and the initial condition g to see the effect on u. The graphs shown are of uti(x)=u(x,ti)u_{t_i}(x)=u(x,t_i) for ti=t0+i stept_i=t_0+i\ step for i=0,,5i=0, \cdots ,5. They are color-coded black, red, blue, grey, magenta, turquoise in order of increasing time.

x,t=var('x t') @interact(layout={'top':[['f','g','v','frames'],['xmin','xmax','t0','step']]}) def getem(f=sin(x+5*t),g=1/exp(x^2),v=1,frames=1,xmin=-1.,xmax=1.,t0=.1,step=.2): def f1(x,t): return f(x=x,t=t) def g1(x): return g(x=x) clrs=['black','red','blue','grey','magenta','turquoise'] def u(x,t): def h(s): return f1(x-v*(t-s),s).n() return numerical_integral(h,(0,t))[0]+g1(x-v*t) G=Graphics() for i in range(frames): def w(x): return u(x,t0) G+=plot(w,(xmin,xmax),color=clrs[i]) if frames==1: tab=[[('x','t0')],['u(x,t0)']] pts=[] x0=xmin dx=(xmax-xmin)/4 for j in range(5): tab[0]+=[(x0.n(digits=6),t0.n(digits=6))] tab[1]+=[u(x0,t0).n(digits=6)] pts+=[(x0,w(x0))] x0+=dx G+=point(pts) t0+=step show(G) if frames==1: print table(tab)
Interact: please open in CoCalc

The 1-d wave equation uttc2uxx=f(x,t),xR,t>0u_{tt}-c^2\,u_{xx} = f(x,t), x\in \mathbb{R}, t>0 with initial conditions u(x,0)=g(x),ut(x,0)=h(x)u(x,0)=g(x), u_t(x,0)=h(x)

By D'Alambert and Duhamel, when cc is constant, the solution is u(x,t)=12(g(x+ct)+g(xct))+12c(xctx+cth(y)dy+0txc(ts)x+c(ts)f(y,s)dyds)u(x,t)=\displaystyle\frac{1}{2}\,(g(x+ct)+g(x-ct))+\frac{1}{2c}\,(\int_{x-ct}^{x+ct}h(y)dy +\int_0^t\int_{x-c(t-s)}^{x+c(t-s)}f(y,s)dy\,ds) We can evaluate this using quadrature rules (gauss and romberg). The interact below allows you plot the solution for given ff, gg, and hh.

Interact: please open in CoCalc
y,s,t=var('y,s,t') html('<h4>Patience! Movie in production....</h4>') @interact(layout={'left': [['f','g','h','c','xrng','yrng','tmax','Nframes','delay']]}) def getD(c=1,f=x/(10*(x^2+1)),g=sin(4*x),h=x,xrng=vector([-2,4]),yrng=vector([-2,10]),tmax=4,delay=selector(['1/10 sec','1/2 sec','1 sec']), Nframes=selector([5,10,20])): xmin=xrng[0];xmax=xrng[1] ymin=yrng[0];ymax=yrng[1] p=pi.n() dly={'1/10 sec':20,'1/2 sec':50,'1 sec':100} dly=dly[delay] def romberg_step(R0,a,h,er,f): n=len(R0) R1=[.5*R0[0]+.5*h*sum([f(a+(2*i+1)*.5*h) for i in range(2^(n-1))])] for m in range(1,n+1): R1+=[R1[m-1]+1/(4^m-1)*(R1[m-1]-R0[m-1])] er=(R1[-1]-R1[-2]) return R1,er def romberg(f,a,b,ER): h=b-a R0=[.5*h*(f(a)+f(b))] k=0 er=ER while k <10 and abs(er)>= ER: R0,er=romberg_step(R0,a,h,er,f) h=h/2 k+=1 return R0,er def g1(x): return g(x=x) def f1(x,t): return f(x=x,t=t) def h1(x): return h(x=x) def u1(x,t): return 1/2*(g1(x+c*t)+g1(x-c*t))+1/(2*c)*numerical_integral(h1(y),x-c*t,x+c*t)[0] def u2(x,t): def w(s): def h(y): return f1(y,s) return numerical_integral(h(y),x-c*t,x+c*t)[0] bil=romberg(w,0,t,.00001); return bil[0][-1] def u(x,t): return u1(x,t)+u2(x,t) def r(x): return u(x,tmax) x0=(xmax-xmin)/2 def q(t): return u(x0,t) dt=tmax/Nframes p=[] k=1 for t in xsrange(0,tmax,dt): def r(x): return u(x,t) if k==Nframes: t0=tmax tab=[['x'],['u(x,'+str(tmax.n(digits=4))+')']] pts2=[] x0=xmin dx=(xmax-xmin)/4 for j in range(5): pts2+=[(x0,r(x0))] tab[0]+=[pts2[-1][0].n(digits=6)] tab[1]+=[pts2[-1][1].n(digits=6)] x0+=dx mess=table(tab) p+=[plot(r,(x,xmin,xmax),ymin=ymin,ymax=ymax,color='red')+point(pts2)] else: k+=1 p+=[plot(r,(x,xmin,xmax),ymin=ymin,ymax=ymax,figsize=6)] p+=[sum(p)] a=animate(p) a.show(delay=dly,iterations=4) print mess

Patience! Movie in production....

Interact: please open in CoCalc
pts=[(-2,0),(4,5)] point(pts,ymin=1,ymax=2,xmin=1,xmax=2)
︠6fca41c2-1c53-4e5d-8a6e-87f3ef339ae8︠ y,s,t=var('y,s,t') html('<h4>Patience! Movie in production....</h4>') @interact(layout={'top': [['f','g','h'],['c','xrng','yrng','tmax'],['Nframes','delay']]}) def getD(c=1,f=x/(10*(x^2+1)),g=sin(4*x),h=x,xrng=vector([-2,4]),yrng=vector([-2,10]),tmax=4,delay=selector(['1/10 sec','1/2 sec','1 sec']), Nframes=selector([5,10,20])): xmin=xrng[0];xmax=xrng[1] ymin=yrng[0];ymax=yrng[1] p=pi.n() dly={'1/10 sec':20,'1/2 sec':50,'1 sec':100} dly=dly[delay] def romberg_step(R0,a,h,er,f): n=len(R0) R1=[.5*R0[0]+.5*h*sum([f(a+(2*i+1)*.5*h) for i in range(2^(n-1))])] for m in range(1,n+1): R1+=[R1[m-1]+1/(4^m-1)*(R1[m-1]-R0[m-1])] er=(R1[-1]-R1[-2]) return R1,er def romberg(f,a,b,ER): h=b-a R0=[.5*h*(f(a)+f(b))] k=0 er=ER while k <10 and abs(er)>= ER: R0,er=romberg_step(R0,a,h,er,f) h=h/2 k+=1 return R0,er def g1(x): return g(x=x) def f1(x,t): return f(x=x,t=t) def h1(x): return h(x=x) def u1(x,t): return 1/2*(g1(x+c*t)+g1(x-c*t))+1/(2*c)*numerical_integral(h1(y),x-c*t,x+c*t)[0] def u2(x,t): def w(s): def h(y): return f1(y,s) return numerical_integral(h(y),x-c*t,x+c*t)[0] bil=romberg(w,0,t,.00001); return bil[0][-1] def u(x,t): return u1(x,t)+u2(x,t) def r(x): return u(x,tmax) x0=(xmax-xmin)/2 def q(t): return u(x0,t) dt=tmax/Nframes p=[] for t in xsrange(0,tmax,dt): def r(x): return u(x,t) p+=[plot(r,(x,xmin,xmax),ymin=ymin,ymax=ymax)] p+=[sum(p)] a=animate(p) a.show(delay=dly,iterations=4) ︠f32f237e-3c32-4587-9551-b2be70915b32︠ plot(x^2,(x,0,1),ymax=.5)
def r(x): return u(x,.2)
p=[] for t in xsrange(0,2,.1): def r(x): return u(x,t) p+=[plot(r,(x,0,5))] p+=[sum(p)] a=animate(p,xmin=0,ymin=-1)
p[-2]
p+=[l]
a=animate(p,xmin=0,ymin=-1)
a='a'
︠01741f4e-54ce-4e64-a141-46b38c30ed92s︠ a.show(delay=10,iterations=4)
Error in lines 1-1 Traceback (most recent call last): File "/projects/e8a71479-4823-40aa-b321-7ed263b3058c/.sagemathcloud/sage_server.py", line 879, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> NameError: name 'a' is not defined
plot(r,(x,0,1))