# ndsolve with multiple initial conditions

I am trying to solve a PDE using the NDsolve with initial and boundary conditions,

NDSolve[{D[u[t, x], t] - D[ D[u[t, x], x]] + D[u[t, x], x] == 10,
u[0, x] == 2,
u[t, 0] == 1,
u[t, 2] == 1
},
u,
{t, 0, 5}, {x, 0, 2}, MaxStepSize -> 0.01]


it worked. But when I trying to assign a initial condition: u[0,x]=2 when 0.5<=x<=1, u[0,x]=1 elsewhere in [0,2] by means of If

NDSolve[{D[u[t, x], t] - D[ D[u[t, x], x]] + D[u[t, x], x] == 10,
If[0.5 <= x <= 1, u[0, x] == 2, u[0, x] == 1],
u[t, 0] == 1,
u[t, 2] == 1
},
u,
{t, 0, 5}, {x, 0, 2}, MaxStepSize -> 0.01]


the system returned me

NDSolve::deqn: Equation or list of equations expected instead of If[0.5<=x<=1,u[0,x]==2,u[0,x]==1] in the first argument {(u^(1,0))[t,x]==10,If[0.5<=x<=1,u[0,x]==2,u[0,x]==1],u[t,0]==1,u[t,2]==1}.


I know something is wrong with my initial conditions expression, But how I assign the initial conditions with an if condition. Or a better way to deal with it?

UPDATE:

I followed the advice of @bbgodfrey by using DirichletCondition. it worked.

NDSolve[{D[u[t, x], t] - D[ D[u[t, x], x]] + D[u[t, x], x] == 0,
DirichletCondition[u[t, x] == 2, 0.5 <= x <= 1],
DirichletCondition[u[t, x] == 1, {2 >= x > 1, 0 <= x < 0.5}],
u[t, 0] == 1,
u[t, 2] == 1
},
u,
{t, 0, 5}, {x, 0, 2}, MaxStepSize -> 0.01]


But comparing with the results of @kglr, the results are different. this is the results using DirichletCondition, which also with an alert

NDSolve::femcscd: The PDE is convection dominated and the result may not be stable. Adding artificial diffusion may help. this is the result using Boole Thank you!

• Check the documentation for DirichletCondition. – bbgodfrey Nov 29 '18 at 6:03
• Thank you @bbgodfrey, I have tried the DirichletCondition and it works! – Rick Nov 29 '18 at 6:16
• Any f[x] that computes the correct values would be fine, if used with an IC in the form u[0, x] == f[x] (à la Bill Watt's, Alex Trounev's, or kglr's solutions), because NDSolve computes these values in the initial ProcessEquations[] phase to generate an IC vector (over the spatial grid) that starts the time-integration in the method of lines. – Michael E2 Nov 30 '18 at 15:39

I like UnitStep.

NDSolve[{D[u[t, x], t] - D[D[u[t, x], x]] + D[u[t, x], x] == 10,
u[0, x] == 1 + UnitStep[x - 0.5] - UnitStep[x - 1], u[t, 0] == 1,
u[t, 2] == 1}, u[t, x], {t, 0, 5}, {x, 0, 2},
MaxStepSize -> 0.01] // Flatten

u[t_, x_] = u[t, x] /. %

Plot3D[u[t, x], {t, 0, 5}, {x, 0, 2}] Plot[u[0, x], {x, 0, 2}, PlotRange -> All] Piecewise probably works too.

The third opinion, a simple modification of the author's code immediately leads to a solution. Note the solution near t=0

s = NDSolve[{D[u[t, x], t] - D[D[u[t, x], x]] + D[u[t, x], x] == 10,
u[0, x] == If[0.5 <= x <= 1, 2, 1], u[t, 0] == 1, u[t, 2] == 1},
u, {t, 0, 5}, {x, 0, 2}]

{Plot3D[u[t, x] /. s, {t, 0, 5}, {x, 0, 2}, PlotRange -> All,
Mesh -> None, ColorFunction -> Hue, AxesLabel -> {"t", "x", ""}],
Plot3D[u[t, x] /. s, {t, 0, 1}, {x, 0, 2}, PlotRange -> All,
Mesh -> None, ColorFunction -> Hue, AxesLabel -> {"t", "x", ""}],
Plot3D[u[t, x] /. s, {t, 0, .05}, {x, 0, 2}, PlotRange -> All,
Mesh -> None, ColorFunction -> Hue, AxesLabel -> {"t", "x", ""}],
Plot[Evaluate[Table[u[t, x] /. s, {t, 0, .1, .01}]], {x, 0, 2},
AxesLabel -> {"x", "u"}]} We check the solution at t->= 0, and see that everything is not smooth there. We need a special numerical method to solve this problem. To eliminate the oscillations at t-> 0, add the option

s = NDSolve[{D[u[t, x], t] - D[D[u[t, x], x]] + D[u[t, x], x] == 10,
u[0, x] == If[0.5 <= x <= 1, 2, 1], u[t, 0] == 1, u[t, 2] == 1},
u, {t, 0, 5}, {x, 0, 2}, MaxStepSize -> 0.01] • Thank you so much! – Rick Dec 6 '18 at 8:02
• @Rick, you're welcome! – Alex Trounev Dec 6 '18 at 15:30
sol = NDSolve[{D[u[t, x], t] - D[D[u[t, x], x]] + D[u[t, x], x] == 10,
u[0, x] == 1 + Boole[0.5 <= x <= 1], u[t, 0] == 1,
u[t, 2] == 1}, u, {t, 0, 5}, {x, 0, 2}, MaxStepSize -> 0.01][];

{u[0, .75], u[0, .1 .5]} /. sol


{2., 1.}

Plot3D[Evaluate[u[t, x] /. sol], {t, 0, 5}, {x, 0, 2}] • Thank you so much! – Rick Nov 29 '18 at 6:15
• @Rick, my pleasure. – kglr Nov 29 '18 at 6:16