Linear combinations using the fundamental solution

Start by defining the fundamental solution:

g[x_,t_,k_]=Exp[-x^2/4/k/t]/Sqrt[4 Pi k t]

We can build new solutions by shifting and scaling.  Below is an example and some related plots.

u[x_,t_,k_]=3g[x-2,t,1]-5g[x+4,t,1]

Plot[u[x,0.0001,1],{x,-6,6},PlotRange->All]

Manipulate[Plot[u[x,t,1],{x,-6,6},PlotRange->{-10,10}],{t,0.0001,5}]

Piecewise linear initial condition

We define an initial condition and look at its plot.

ϕ1[x_]=Piecewise[{{1+x,-1<x≤0},{1-x,0<x<1}}]

Plot[ϕ1[x],{x,-1.5,1.5}]

Now, we build an approximation using a linear combination of scaled/shifted fundamental solutions.  The code below allows us to pick the number n of terms in the finite sum we use as an approximation.  The approximation is given by (Subscript[u, 1]).  We also define a list of the individual terms so we can plot them to see how each contributes to the sum.

a=-1;
b=1;
n=15;
Δy=(b-a)/n;
u1[x_,t_,k_]=Sum[ϕ1[y]g[x-y,t,k]Δy,{y,a+Δy/2,b-Δy/2,Δy}]
u1terms[x_,t_,k_]=Table[ϕ1[y]g[x-y,t,k]Δy,{y,a+Δy/2,b-Δy/2,Δy}]

Plot[{ϕ1[x],u1[x,0.005,1],u1terms[x,0.005,1]},{x,-1.5,1.5},PlotRange->All]

Plot[{ϕ1[x],u1[x,0.1,1]},{x,-1.5,1.5},PlotRange->All]

Manipulate[Plot[{u1[x,t,1],u1terms[x,t,1]},{x,-1.5,1.5},PlotRange->{0,2}],{t,0.0001,1,0.001}]

Sine function initial condition

ϕ2[x_]=Piecewise[{{Sin[x],-Pi<x<Pi}}]

a=-5;
b=5;
n=40;
Δy=(b-a)/n;
u2[x_,t_,k_]=Sum[ϕ2[y]g[x-y,t,k]Δy,{y,a+Δy/2,b-Δy/2,Δy}];
u2terms[x_,t_,k_]=Table[ϕ2[y]g[x-y,t,k]Δy,{y,a+Δy/2,b-Δy/2,Δy}];

Plot[{ϕ2[x],u2[x,0.01,1],u2terms[x,0.01,1]},{x,a,b},PlotRange->All]

Manipulate[Plot[{u2[x,t,1],u2terms[x,t,1]},{x,a,b},PlotRange->{-2,2}],{t,0.0001,1,0.001}]