# The problem u_xx = u_t with constant nonhomogeneous boundary 
# conditions can be solved by subtacting from the solution a time 
# independent solution which solves the PDE and BC. Thus, for the 
# problem
# 
#           u_xx = u_t   0 < x < pi,   0 < t
#      u(0,t) =1       u(pi,t) = 2,   0 < t
#          u(x,0) = 1,  0 < x < pi
# 
# We subtract off the solution to the problem v_xx =0,  v(0)=1,    v(pi) = 
# 2.  Note: the function v is commonly referred to as the steady state 
# solution of the problem.
# 
# Setting w= u -v, we find that w satisfies the problem:
#          w_xx = w_t,        0 < x < pi,  
#         w(0,t) =0       w(pi,t) = 0
#      w(x,0) = 1-v = 1 - (x/pi +1)  = -x/pi
#
# 
#      
--------------------------------------------------------------------------------
> v:='v';v:=rhs(dsolve({diff(v(x)=0,x$2)=0,v(0)=1,v(Pi)=2},v(x)));

                                     v := v

                                         x
                                  v := ---- + 1
                                        Pi
> f:=-x/Pi;

                                            x
                                   f := - ----
                                           Pi

                                            x
                                   f := - ----
                                           Pi
--------------------------------------------------------------------------------
> (2/Pi)*Int(f*sin(n*x),x=0..Pi);b[n]:=value(");

                                 Pi
                                 /
                                |     x sin(n x)
                                |   - ---------- dx
                                |         Pi
                               /
                               0
                             2 --------------------
                                        Pi

                                 sin(n Pi) - n cos(n Pi) Pi
                     b[n] := - 2 --------------------------
                                             2  2
                                           Pi  n
--------------------------------------------------------------------------------
#  
# We can now write an analytic expression for the solution to the 
# original problem.
# 
--------------------------------------------------------------------------------
> u:=1+x/Pi+Sum(2*(-1)^n/(n*Pi)*sin(n*x)*exp(-n^2*t),n=1..infinity);

                             /infinity                             \
                             | -----         n                 2   |
                        x    |  \        (-1)  sin(n x) exp(- n  t)|
             u := 1 + ---- + |   )     2 --------------------------|
                       Pi    |  /                   n Pi           |
                             | -----                               |
                             \ n = 1                               /
# 
# Note that the limiting value of the solution as t tends to infinity is 1 + 
# x/pi, the steady state solution.
# 
--------------------------------------------------------------------------------
> w:=1+x/Pi+Sum(2*(-1)^n/(n*Pi)*sin(n*x)*exp(-n^2*t),n=1..10);

                              /  10                              \
                              |-----       n                 2   |
                         x    | \      (-1)  sin(n x) exp(- n  t)|
              w := 1 + ---- + |  )   2 --------------------------|
                        Pi    | /                 n Pi           |
                              |-----                             |
                              \n = 1                             /
--------------------------------------------------------------------------------
> with(plots):
> t1:=textplot([1,3,`10th Partial Sums for`]):t2:=textplot([1,2.8,`t=0, t=1, t=20`]):
> t3:=textplot([1,2.6,`u_xx = u_t`]):t4:=textplot([1,2.45,`u(0,t) = 1, u(pi,t) = 2`]):t5:=textplot([1,2.3,`u(x,0)=1`]):
--------------------------------------------------------------------------------
> p3:=plot({subs(t=0,w),subs(t=1,w),subs(t=20,w)},x=0..Pi):
> display({t1,t2,t3,t4,t5,p3});

   ** Maple V Graphics **

--------------------------------------------------------------------------------
# 
# Let us now consider the problem:
#              u_xx = u_t  + texp(-t), 0 < x  < pi,   0 < t
#        u(0,t) =1       u(pi,t) = 2,   0 < t
#                u(x,0) = 1,  0 < x < pi
# 
# The addition of this  forcing term to the partial differential equations 
# complicates the situation. The problem would be much simpler if the 
# forcing term was time independent. In that case we could look for a 
# steady state solution as we did in the previous problem. That is a 
# solution to:
# 
#            v_xx = F(x),   F(x) represents the forcing term.
#        v(0) = 1,   v(pi) = 2
# 
# We would then subtract this function from the unknown solution u 
# and proceed as before. Instead of this, we will look for a steady state 
# solution to the heat equation with the given boundary conditions.
# 
# As before we see that the steady state solution is 1+ x/pi. Setting w = 
# u - v, we see that w satisfies the problem:
# 
#                w_xx = w_t + texp(-t),   0  < x < pi,   0 < t
#          w(0,t) =0       w(pi,t) = 0
#          w(x,0) = 1-v = 1 - (x/pi +1)  = -x/pi
#
# 
# Since this problem is really the heat equation with homogeneous 
# boundary conditions we look for a solution of the form:
# 
# 
#                                                          
> b[n]:='b[n]':w:=(x,t)->Sum(b[n](t)*sin(n*x)*exp(-n^2*t),n=1..infinity);

                             infinity
                              -----
                               \                              2
               w := (x,t) ->    )     b[n](t) sin(n x) exp(- n  t)
                               /
                              -----
                              n = 1
# Placing w into the partial differential equation we derive the following 
# equations for the functions b_n.
# 
> L:=diff(w(x,t),x$2)-diff(w(x,t),t);

     /infinity                                  \
     | -----                                    |
     |  \                          2        2   |
L := |   )     - b[n](t) sin(n x) n  exp(- n  t)| - (
     |  /                                       |
     | -----                                    |
     \ n = 1                                    /

infinity
 -----
  \      //  d         \                 2                        2        2   \
   )     ||---- b[n](t)| sin(n x) exp(- n  t) - b[n](t) sin(n x) n  exp(- n  t)|
  /      \\ dt         /                                                       /
 -----
 n = 1

)
# 
# Expanding the forcing term as a sum of sines we see that texp(-t) = F 
# can be written as 
--------------------------------------------------------------------------------
> F:=t*exp(-t)*Sum(c[n]*sin(n*x),n=1..infinity);

                                    /infinity              \
                                    | -----                |
                                    |  \                   |
                    F := t exp(- t) |   )     c[n] sin(n x)|
                                    |  /                   |
                                    | -----                |
                                    \ n = 1                /
# 
# Note that the infinite sum is just the Fourier sine series expansion of 
# the constant 1.  Setting L = F, we have for each n
--------------------------------------------------------------------------------
> b:='b':eq1:=-diff(b[n](t),t)*exp(-n^2*t)=t*exp(-t)*c[n];

                       /  d         \        2
              eq1 := - |---- b[n](t)| exp(- n  t) = t exp(- t) c[n]
                       \ dt         /
# 
# The solution of these differential equations has a unique solution 
# once the initial values b_n(0) are specified. These values are given by 
# the initial data w(x,0).
-------------------------------------------------------------------------------