# Manipulate NDsolve 1D-reaction diffusion equation

I want to solve the following 1D-reaction diffusion equation: $$\frac{\partial}{\partial t}u(t,x)=\alpha\,\frac{\partial^{2}}{\partial x^{2}}u(t,x)+a\,u(t,x)$$ $$u(t,0)=0\,\,\,\,\,\text{et}\,\,\,\,\,u(t,\pi)=0$$ $$u(0,x)=x(\pi-x)$$

with $$t>0$$ and $$0\leq x\leq\pi$$, where I can manipulate the parameters $$a$$ and $$\alpha$$.

• The downvote is not from me, but I imagine that the user would have been more content with a bit of Mathematica code to start with. – Henrik Schumacher Jan 11 at 9:04

Look carefully at the documentation for NDSolve and Manipulate and see if you can understand how every character of this is working. You can even click on the orange Details and Options for each of those to get additional information. And then

Manipulate[
sol=u/.NDSolve[{D[u[t,x],t]==α D[u[t,x],{x,2}]+a u[t,x],
u[t,0]==0,u[t,Pi]==0,u[0,x]==x(Pi-x)},u,{t,0,1},{x,0,Pi}][[1]];
Plot3D[sol[t,x],{t,0,1},{x,0,Pi}],
{α,1/2,2},{a,1/2,2}]


Move one of the sliders and then pause for a moment to let it complete the calculations and update the plot, then repeat. If you need to make changes to the code then make one small change, see if it still works, if not then back out of that and try something else, if it does work then make one more small change. Fortunately, perhaps even surprisingly, your formulation of the problem worked on the first try without any errors or failures. Many do not do that well on their first try.

Since this can be readily solved analytically using seperation of variables, another option is to use the analytical solution, and replace the paramaters in the solution without the need to solve it each time.

Mathematica can't solve this analytically, but the analytical solution is

$$u \left( x,t \right) =\sum _{n=1}^{\infty }-4\,{\frac {\sin \left( nx \right) {{\rm e}^{t \left( -\alpha\,{n}^{2}+a \right) }} \left( \left( -1 \right) ^{n}-1 \right) }{\pi\,{n}^{3}}}$$

Using the above you could do

ClearAll[u, t, x, alpha, a];
nTerms = 20; (*more than enough terms*)
u = Sum[- 4 Sin[n x] Exp[t (-alpha*n^2 + a)] ((-1)^n - 1)/(Pi*n^3), {n, 1, nTerms}];

Manipulate[
Quiet@Plot[Evaluate[u /. {t -> t0, alpha -> alpha0, a -> a0}], {x, 0,Pi},
PlotRange -> {{0, Pi}, {0, 3}}, GridLines -> Automatic,
GridLinesStyle -> LightGray,
PlotStyle -> Red
],
{{t0, 0, "time"}, 0, 1, .001, Appearance -> "Labeled"},
{{alpha0, 1.68, "alpha"}, 0, 2, .01, Appearance -> "Labeled"},
{{a0, 0.5, "a"}, 0, 2, .01, Appearance -> "Labeled"}
]