And yet another way.
pde = D[u[x, t], t] == D[u[x, t], {x, 2}]
ic = u[x, 0] == Sin[x]
bc = {Derivative[1, 0][u][0, t] == 1, Derivative[1, 0][u][Pi, t] == -1}
Separate variables, first with times separation.
u[x_, t_] = X[x] T[t]
pde/u[x, t] // Expand
T'[t]/T[t] == X''[x]/X[x]
Each side must be equal to a constant. First try 0.
t0eq = T'[t]/T[t] == 0;
DSolve[t0eq, T[t], t] // Flatten
(*{T[t] -> C[1]}*)
t0 = 1
since we will combine with constants from the x equation.
x0eq = X''[x]/X[x] == 0
DSolve[x0eq, X[x], x] // Flatten
(*{X[x] -> C[2] x + C[1]}*)
x0 = X[x] /. % /. {C[1] -> c1, C[2] -> c2}
Now use a negative constant.
t1eq = T'[t]/T[t] == -α^2
DSolve[t1eq, T[t], t] // Flatten
(*{T[t] -> C[1] E^(α^2 (-t))}*)
t1 = T[t] /. % /. C[1] -> 1
x1eq = X''[x]/X[x] == -α^2
DSolve[x1eq, X[x], x] // Flatten
(*{X[x] -> C[2] Sin[α x] + C[1] Cos[α x]}*)
x1 = X[x] /. % /. {C[1] -> c3, C[2] -> c4}
We don't have all the necessary pieces yet, so do plus separation.
u[x_, t_] = X[x] + T[t]
pde
(*T'[t] == X''[x]*)
Again, each side must be equal to a constant. Call it δ.
xpeq = X''[x] == δ
DSolve[xpeq, X[x], x] // Flatten
(*{X[x] -> C[2] x + C[1] + (δ x^2)/2}*)
xp = X[x] /. % /. {C[1] -> c5, C[2] -> c6}
tpeq = T'[t] == δ
DSolve[tpeq, T[t], t] // Flatten
(*{T[t] -> C[1] + δ t}*)
tp = T[t] /. % /. C[1] -> 0
Put all the pieces together.
Clear[c1, c2, c3, c4, c5, c6, α, δ]
u[x_, t_] = x0 t0 + x1 t1 + xp + tp
(*c1 + c2 x + E^(α^2 (-t)) (c3 Cos[α x] + c4 Sin[α x]) + c5 +
c6 x + δ t + (δ x^2)/2*)
c1 and c5 can be combined, c2 and c6 can be combined.
c5 = 0;
c6 = 0;
Look at the first bc
bc[[1]]
(*c2 + α c4 E^(α^2 (-t)) == 1*)
from which we can say
c2 = 1;
c4 = 0;
and the next bc.
bc[[2]]
-(α c3 Sin[π α] E^(α^2 (-t))) + π δ + 1 == -1
Make the Sin 0
Solve[α π == n π, α] // Flatten
from which
α = n;
$Assumptions = n ∈ Integers && n >= 0
Use the rest of the equation to solve for δ
c26eq2 = π δ + 1 == -1
δ = δ /. Solve[π δ + 1 == -1, δ][[1]]
So far we have
u[x, t]
c1 + c3 E^(-n^2 t) Cos[n x] - (2 t)/π - x^2/π + x
Now for the ic.
ic
(*c1 + c3 Cos[n x] - x^2/π + x == Sin[x]*)
We are going to have an infinite series for n
and the n=0
will be a constant term. Since we don't need two of them we can throw out c1.
c1 = 0
ic - (-(x^2/π) + x)
(*c3 Cos[n x] == x^2/π - x + Sin[x]*)
To solve for c3
, multiply by Cos[n x]
and integrate.
c3*Integrate[Cos[n*x]^2, {x, 0, Pi}] == (1/Pi)*Integrate[x^2*Cos[n*x], {x, 0, Pi}] -
Integrate[x*Cos[n*x], {x, 0, Pi}] + Integrate[Sin[x]*Cos[n*x], {x, 0, Pi}]
c3 = c3 /. Solve[%, c3][[1]] // Simplify
(*-((2 ((-1)^n + 1))/(\[Pi] n^2 (n^2 - 1)))*)
The constant term for n = 0
needs to be done separately.
c30*Integrate[1, {x, 0, Pi}] == (1/Pi)*Integrate[x^2*1, {x, 0, Pi}] -
Integrate[x*1, {x, 0, Pi}] + Integrate[Sin[x]*1, {x, 0, Pi}]
c30 = c30 /. Solve[%, c30][[1]] // Expand
2/π-π/6
Without the sum on n we have
u[x_, t_] = u[x, t] + c30
-((2 ((-1)^n + 1) E^(-n^2 t) Cos[n x])/(π n^2 (n^2 - 1))) - (
2 t)/π - x^2/π + x - π/6 + 2/π
Check against the pde.
pde
(*True*)
We can see that odd n the terms are zero, so change to.
-((2 ((-1)^n + 1) E^(-n^2 t) Cos[n x])/(π n^2 (n^2 - 1))) /.
n -> 2 n // Simplify
(*-((E^(-4 n^2 t) Cos[2 n x])/(π n^2 (4 n^2 - 1)))*)
We have an infinite series of n starting from 1, but lets make a finite series.
u[x_, t_, mm_] := 2/Pi - Pi/6 - (2*t)/Pi - x^2/Pi + x -
(1/Pi)*Sum[Cos[2*n*x]/(E^(4*n^2*t)*(n^2*(4*n^2 - 1))), {n, 1, mm}]
Check against xzczd's numerical solution.
gifs = Table[
Plot[{u[x, t, 20], solnum[x, t]}, {x, 0, π},
PlotRange -> {-1, 1}], {t, 0, 1, .01}];
ListAnimate[%]
This solution matches Maple's and Nasser's solution and the numerical solution. I could not get xzczd's analytic solution to satisfy the pde, which is I think is the cause for the little tails at the end points.
Update on showing the equivalence in the separate variable solution and xzczd's fourier transform solution. The separate variable solution simplified by letting the original n go to 2n to eliminate the odd n zero's:
usep[x_, t_] := -((2*(t - 1))/Pi) - Pi/6 - x^2/Pi + x -
(1/Pi)*Sum[Cos[2*n*x]/(E^(4*n^2*t)*(n^2*(4*n^2 - 1))), {n, 1, Infinity}]
Rewrite as
usep[x, t] := 2/Pi - (2*t)/Pi - (1/Pi)*Sum[Cos[2*n*x]/(E^(4*n^2*t)*(n^2*(4*n^2 - 1))),
{n, 1, Infinity}] + Sum[An*Cos[2*n*x], {n, 1, Infinity}]
where we find a series such that for 0 <= x <= π
x - x^2/Pi - Pi/6 == Sum[An*Cos[2*n*x], {n, 1, Infinity}];
Find An
by multiplying by Cos[2 n x]
and integrating.
Integrate[(x - x^2/Pi - Pi/6)*Cos[2*n*x], {x, 0, Pi}] ==
An*Integrate[Cos[2*n*x]^2, {x, 0, Pi}]
An = An /. Solve[%, An][[1]]
(*-(1/(π n^2))*)
Add to the existing sum
-(1/π) (E^(-4 n^2 t) Cos[2 n x])/(n^2 (4 n^2 - 1)) +
An Cos[2 n x] // Simplify
(*((E^(-4 n^2 t)/(1 - 4 n^2) - 1) Cos[2 n x])/(π n^2)*)
And we end up with the same solution as the fourier series solution.
bc = Sequence[Derivative[1, 0][u][0, t] == 1, Derivative[1, 0][u][Pi, t] == -1]
to fix the error indicated by the message. $\endgroup$